I am experimenting with logo designs in Mathematica (8 or above) using the notebook interface and I want to change the generating formula A LOT. The problem is I only know SHIFT + ENTER which makes me jump outside my current cell and lose my position.
Is there a way to evaluate the current cell without leaving it and losing your position within it? So: Evaluate a cell and land back inside the same cell at the exact position I pressed SHIFT + ENTER at.
What I once came to use was along the lines:
a = Exp[1.994 \[Pi]/(3 I)];
b = Nest[{#, a #, a\[Conjugate] #, #} &, 1, 5];
{Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, a b, a\[Conjugate] b, -b}]] // {\[Rho]\[Chi], # //
Line} & // Graphics
NotebookFind[EvaluationNotebook[], "E" <> "xp", Previous];
but I hope there is just another key combination or sth. that I am not aware of.
Edit:
a = Exp[(*1*)1.994 \[Pi]/(3 I)];
b = Nest[{#, a #, a\[Conjugate] #, #} &, (*2*)1, (*3*)5];
{Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, a b, a\[Conjugate] b, -b}]] // {\[Rho]\[Chi], # //
Line} & // Graphics
Do[NotebookFind[EvaluationNotebook[], "(*1*)", Previous], {2}];
is a nice little improvement to my solution as partly suggested by Chris Dengen.
Edit 2:
NotebookFind[EvaluationNotebook[], "(*1*)", EvaluationCell]
This solves:
EvaluationCell is sparsely documented so hard to find in fact I just stumbled upon it. Using the EvaluationCell the search starts from above (eliminating the Do twice construct) as we do not come from an output cell further down the screen.
What remains to be solved:
Note This question is about not losing current position when one presses SHIFT + ENTER inside a Mathematica notebook cell. It is not about content or Manipulate. Sorry for not making that clear at the beginning.
Temporary Note
Now trying the following:
I internally hope some function might just take care of the store/restore by chance being optimistic. Being realistic though, I first hope I can even find a way to get at the current keyboard position. Will follow up here if anything materializes out of my attempt.
You can run this cell over and over:
If[NumberQ[x], x++, x = 1];
Print[x]
Do[SelectionMove[EvaluationNotebook[], Previous, Cell], {2}]
This version places the cursor after the Exp in line 1:
a = Exp[1.994 \[Pi]/(3 I)];
b = Nest[{#, a #, a\[Conjugate] #, #} &, 1, 5];
{Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, a b, a\[Conjugate] b, -b}]] // {# //
Line} & // Graphics
Do[NotebookFind[EvaluationNotebook[], "Exp", Previous], {3}];
SelectionMove[EvaluationNotebook[], "Exp", After];
You could also put Evaluate & move on a key combination, e.g. ctrl+r
in the example below. This assignment only lasts for the current session but it can be loaded with pre-initialisation so that it is always present. (The key combo item is added to the Insert menu.)
FrontEndExecute[
FrontEnd`AddMenuCommands[
"DuplicatePreviousOutput", {Delimiter,
MenuItem["Evaluate and return",
FrontEnd`KernelExecute[Module[{nb}, nb = SelectedNotebook[];
SelectionMove[nb, Previous, Cell];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
Do[SelectionMove[EvaluationNotebook[], Previous, Cell], {2}]]],
MenuKey["r", Modifiers -> {"Control"}],
System`MenuEvaluator -> Automatic]}]]
To set pre-initialisation save the above code to a package .m
file as EvaluteReturn.m
in your Documents directory and evaluate the following.
InitializationValue[$PreInitialization] = Hold[Get[
FileNameJoin[{$HomeDirectory, "Documents", "EvaluateReturn.m"}]]]
To inspect the value
InitializationValue[$PreInitialization]
To clear the value
obj = InitializationObjects[$PreInitialization, {"Local"}]
DeleteObject[obj]
How about using Manipulate
? Something like
Manipulate[
c = a;
b = Nest[First@f, 1,
5]; {Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, c b, c\[Conjugate] b, -b}]] // {Red, # // Line} & //
Graphics, {a, Exp[1.994 \[Pi]/(3 I)],
ControlType -> InputField}, {f,
HoldForm[{#, c #, c\[Conjugate] #, #} &], ImageSize -> Full,
ControlType -> InputField}]
You can just edit the values, and upon hitting enter get a new version of the plot. You can save examples you like by using the '+' button in the top-right corner of the Manipulate
to either set bookmarks or paste a snapshot.
You can use dynamic to make things really nice.
The cell to display results in:
Dynamic[b = Nest[{#, a #, a\[Conjugate] #, #} &, 1, 5];]
Dynamic[{Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, a b, a\[Conjugate] b, -b}]] // {\[Rho]\[Chi], # //
Line} & // Graphics]
The cell to edit in:
k = 1.994;
a = Exp[k \[Pi]/(2 I)];
NotebookFind[EvaluationNotebook[], ToString[k], Previous];
You can then just edit the cell below the display one. It automatically puts you right where you entered your value. If you'd like to make things even easier on yourself you can use Manipulate:
Manipulate[a = Exp[k \[Pi] / (f I)];
b = Nest[{#, a #, a\[Conjugate] #, #} &, 1, 5];
{Re[#], Im[#]} & /@
Accumulate[
Flatten[{0, a b, a\[Conjugate] b, -b}]] // {\[Rho]\[Chi], # //
Line} & // Graphics,
{{k, 1.994}, -3, 3},
{{f, 3}, -3, 3}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With