评估当前单元格,而不离开或丢失 Mathematica 中的位置

发布于 2024-12-20 18:05:29 字数 1719 浏览 2 评论 0原文

我正在使用笔记本界面在 Mathematica(8 或更高版本)中尝试徽标设计,我想对生成公式进行很多更改。问题是我只知道 SHIFT + ENTER 这会让我跳出当前单元格并失去我的位置。

有没有一种方法可以评估当前单元格而不离开它并失去您在其中的位置?因此:评估一个单元格并返回内部同一单元格在我按 SHIFT + ENTER 的确切位置

我曾经使用的是这样的:

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];

但我希望有另一个组合键或某物。我不知道。

编辑:

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}];

是对我的解决方案的一个很好的小改进,部分是由 Chris Dengen 建议的。

编辑2:

NotebookFind[EvaluationNotebook[], "(*1*)", EvaluationCell]

这解决了:

  • 这些仅适用于产生一个或已知数量的输出单元的单元。

评估单元的记录很少,很难找到,事实上我只是偶然发现了它。使用EvaluationCell,搜索从上面开始(消除了Do两次结构),因为我们不是来自屏幕下方的输出单元。

仍有待解决的问题:

  • 需要使用
  • 基于注释的着陆标记进行手动单元格保存(单元格级别的簿记)以降落在感兴趣的地方,这会使输入单元格变得混乱

注意 这个问题是关于当在 Mathematica 笔记本单元格内按 SHIFT + ENTER 时不会丢失当前位置。这与内容或操纵无关。抱歉一开始没有说清楚。

临时说明

现在尝试以下操作:

  1. 使用事件处理程序定义
  2. 触发时的替代评估键组合,找到一种方法来存储当前输入单元格中的确切键盘位置
  3. 评估当前输入单元格
  4. 恢复位置当前的输入单元格

我内部希望某些功能可能只是乐观地偶然处理存储/恢复。但现实的是,我首先希望我什至可以找到一种方法来到达当前的键盘位置。如果我的尝试有任何成果,我会在这里跟进。

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:

  • these only work with cells that produce one or a known number of output cells

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:

  • one needs to do manual cell-keeping (bookkeeping at cell level) to land at places of interest
  • using comment-based landing markers clutters the input cells

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:

  1. use event handlers to define an alternative evaluation key combination
  2. when triggered, find a way to store the exact keyboard position in the current input cell
  3. evaluate the current input cell
  4. restore the position in the current input cell

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时间海 2024-12-27 18:05:29

您可以一遍又一遍地运行此单元格:

If[NumberQ[x], x++, x = 1];
Print[x]
Do[SelectionMove[EvaluationNotebook[], Previous, Cell], {2}]

此版本将光标放在第 1 行中的 Exp 之后:

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];

您还可以将 Evaluate &移动组合键,例如下例中的 ctrl+r。该分配仅持续当前会话,但可以通过预初始化加载它,以便它始终存在。 (组合键项已添加到“插入”菜单中。)

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]}]]

要设置预初始化,请将上述代码保存到文档目录中的包 .m 文件中,命名为 EvaluteReturn.m并评估以下内容。

InitializationValue[$PreInitialization] = Hold[Get[
   FileNameJoin[{$HomeDirectory, "Documents", "EvaluateReturn.m"}]]]

检查值

InitializationValue[$PreInitialization]

清除值

obj = InitializationObjects[$PreInitialization, {"Local"}]

DeleteObject[obj]

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]
烂柯人 2024-12-27 18:05:29

使用Manipulate怎么样? 你可以

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}]

编辑这些值,然后按 Enter 键即可获得新版本的绘图。您可以使用Manipulate右上角的“+”按钮来保存您喜欢的示例,以设置书签或粘贴快照。

在此处输入图像描述

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.

enter image description here

似最初 2024-12-27 18:05:29

您可以使用动态使事情变得非常美好。

要显示结果的单元格:

Dynamic[b = Nest[{#, a #, a\[Conjugate] #, #} &, 1, 5];]
Dynamic[{Re[#], Im[#]} & /@ 
    Accumulate[
     Flatten[{0, a b, a\[Conjugate] b, -b}]] // {\[Rho]\[Chi], # // 
      Line} & // Graphics]

要编辑的单元格:

k = 1.994;
a = Exp[k \[Pi]/(2 I)];
NotebookFind[EvaluationNotebook[], ToString[k], Previous];

然后,您可以只编辑显示单元格下方的单元格。它会自动将您定位到您输入值的位置。如果您想让事情变得更轻松,您可以使用 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}]

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}]
西瑶 2024-12-27 18:05:29

您可以使用低级笔记本编程和称为“停靠单元”的功能将单元添加到笔记本顶部,该单元将执行当前单元。这(理论上,还没有尝试过)不应该将光标位置移出单元格。要以编程方式创建停靠单元格,请创建一个具有执行所需操作的按钮的单元格,然后执行以下代码:SetOptions[SelectedNotebook[],"DockedCells"->{Cell[ BoxData[ToBoxes[...您的执行按钮代码...]] , "DockedCell"]}];

我一直使用公司徽标作为固定单元格或“复制为纯文本”/“复制为位图”按钮来执行此操作。在严重扰乱内核的笔记本上,我添加了用于内存管理甚至重新启动内核的按钮。只是一系列的快捷方式。您可以将多个 Cell[] 对象堆叠为停靠单元。

You can use low level notebook programming and a feature called docked cells to add a cell to the top of your notebook which will execute the current cell. This (in theory, haven't tried it) shouldn't take the cursor position out of the cell. to programmatically create the docked cell, create a cell which has the button that does what you want, then execute this code: SetOptions[SelectedNotebook[],"DockedCells"->{Cell[ BoxData[ToBoxes[...your code for execute button...]] , "DockedCell"]}];

I do this all the time with corporate logo as a fixed cell or "Copy as Plain Text"/"Copy as Bitmap" buttons. On notebooks which mess up the kernel heavily, I add buttons for mem management or even restarting the kernel. Just series of shortcuts. You can stack more than one Cell[] object as docked cells.

笑脸一如从前 2024-12-27 18:05:29

有一个名为“evaluate in place”的函数,它允许您选择单元格的一部分,然后对其进行评估。在 Mac 上,它是 command-Enter,而不是 control-Enter。

查看“评估”菜单,菜单项应该与绑定的快捷键组合一起存在。

There's a function called 'evaluate in place' which allows you to select a section of a cell, and just evaluate that. On the Mac it's command-Enter, instead of control-Enter.

Look at the Evaluate menu, the menu entry should be there along with the bound shortcut key combination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文