一个笔记本中的两个初始化单元分别自动保存在单独的 .m 文件中?

发布于 2024-10-30 23:51:26 字数 150 浏览 1 评论 0原文

有没有一种方法可以创建笔记本,其中每个初始化Cell将自动保存在其自己的具有任意名称的.m文件中?

PS 这个问题与 Wolfram Research Inc. 开发的 Mathematica 程序有关。它与数学或数学无关。

Is there a way to create Notebook in which each Initialization Cell will be auto-saved in its own .m-file with arbitrary name?

P.S. The question is related to the Mathematica program developed by Wolfram Research Inc. It is not about Mathematics or math.

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

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

发布评论

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

评论(1

濫情▎り 2024-11-06 23:51:26

我不确定以下方法是否能让您满意:我曾经想要一种生成紧凑笔记本的方法,其中仅包含我的开发笔记本中找到的初始化单元;以下代码将当前笔记本的初始化单元写入单个新笔记本中,并自动保存 .m 文件作为副作用,但它可以轻松地进行调整,为每个初始化单元生成单独的笔记本和 .m 文件。

In[162]:= nbToExtract = SelectedNotebook[]

In[163]:= 
extractInitializationCells[nb_] :=
 Block[{nbNew = CreateDocument[], count = 0},
  (SelectionMove[nb, Next, Cell];
   While[NotebookRead[nb] =!= {}, (If[InitializationCell /. 
          Options[NotebookSelection[nb], InitializationCell],
      (count++;
       NotebookWrite[nbNew, NotebookRead[nb]]), {}]; SelectionMove[nb, Next, Cell])];
   Print[ToString[count] <> " initialization cell(s) found"];
   CurrentValue[nbNew, AutoGeneratedPackage] = Automatic;
   NotebookSave[nbNew, fn];
   NotebookClose[nbNew];
   Clear[nbNew](* just in case *))]

extractInitializationCells[nbToExtract]

这只提取调用函数 extractInitializationCells 的单元下面的初始化单元。我同意之前关于使用自动生成包机制的警告。此外,CurrentValue 并没有无限期地受到向后不兼容的保护,但迄今为止它已经在几个主要的 Mathematica 版本中幸存下来。

I'm not sure if the following approach would satisfy you: I once wanted a way of generating compact notebooks containing only the initialisation cells found in my development notebook; the following code writes the initialization cells of the current notebook into a single new notebook and autosaves a .m file as a side-effect but it could easily be adapted to generate a separate notebook and .m file for each initialization cell.

In[162]:= nbToExtract = SelectedNotebook[]

In[163]:= 
extractInitializationCells[nb_] :=
 Block[{nbNew = CreateDocument[], count = 0},
  (SelectionMove[nb, Next, Cell];
   While[NotebookRead[nb] =!= {}, (If[InitializationCell /. 
          Options[NotebookSelection[nb], InitializationCell],
      (count++;
       NotebookWrite[nbNew, NotebookRead[nb]]), {}]; SelectionMove[nb, Next, Cell])];
   Print[ToString[count] <> " initialization cell(s) found"];
   CurrentValue[nbNew, AutoGeneratedPackage] = Automatic;
   NotebookSave[nbNew, fn];
   NotebookClose[nbNew];
   Clear[nbNew](* just in case *))]

extractInitializationCells[nbToExtract]

This only extracts the initialisation cells below the cell in which the function extractInitializationCells is called. And I'd agree with the previous caveats about using the auto generation package mechanism. Also, CurrentValue is not protected indefinitely from backwards incompatibility but it has survived several major Mathematica versions so far.

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