将笔记本集成到 Mathematica 的文档中心
如果您已经使用 Mathematica 一段时间了,您可能已经对文档中心产生了浓厚的兴趣。您总能在这些页面中找到新的东西。让它成为一个函数的选项,或者只是一些在某些时候对您来说似乎没有用的示例。
您可能已经用您一直使用的专用函数编写了包。有时您可能会想到一个与您的函数一起使用的简洁示例,但它最终可能会被遗忘在硬盘中的某个地方。如果您在想到它时就将其写入文档,也许您以后就不会拼命寻找它了。
因此,我想知道如何以编程方式将您自己的函数的文档与 Mathematica 的文档中心集成。这个问题是为了探讨如何调整文档。如果您编写了可以帮助您执行此操作的脚本,请与社区分享。
Wolfram 的工作台不是这个问题的可接受的解决方案。一切都必须通过简单安装 Mathematica 来完成。该解决方案应涵盖以下几点:
- 为函数创建文档(最好是模板)。
- 创建指南和教程(如果它们认为有用)。
- 将笔记本链接到文档中心。
- 创建在不同环境中正确显示的“使用”消息。
- 在 Mathematica Notebook 中
?Symbol
- 在文档中心
搜索:符号
- 在 Mathematica Notebook 中
这是一个非常广泛的主题,我有 1、2 和 3 的解决方案。我缺少第 4 点。请告诉我们,您如何通过文档中心记录您的功能?
更新
我添加了另一个答案。希望这个答案能够更加鼓励 Mathematica 用户用他们的包编写文档页面。我认为编写文档页面对应用程序编写者以及应用程序的用户都有好处。如果您下载我编写的包,我建议您按照教程进行操作,以便您可以看到每一步发生的情况。这将为您未来的项目提供宝贵的经验。
Github(2014 年 5 月 24 日)
自从我编写这个包以来,已经有几个人对此包感兴趣。我已将包上传到Github: https://github.com/jmlopez-rod/ApplicationMaker 。如果您想成为存储库的贡献者,请与我联系。
If you have been using Mathematica for a while you probably have grown attached to the documentation center. There is always something new that you find in those pages. Let it be options for a function or just some examples that at some point did not seem useful to you.
It is likely that you have written packages with your specialized functions that you use all the time. Sometimes you might think of a neat example to use with your function but it is likely that it ends up being forgotten somewhere in your hard disk. If you had written it to the documentation at the moment you thought of it maybe you wouldn't be looking for it desperately later on.
For this reason I would like to know how to integrate documentation for your own functions with Mathematica's documentation center programmatically. This question is here to explore how to adapt documentation. If you have written scripts that help you do this, please share it with the community.
Wolfram's Workbench is not an acceptable solution for this question. Everything must be done with the plain installation of Mathematica. There is a couple of points that the solution should cover:
- Creating documentation for a function (Preferably a template).
- Creating guides and tutorials (If they are deem useful).
- Linking the notebooks to the documentation center.
- Creating "usage" messages that display correctly in different enviroments.
- In Mathematica Notebook
?Symbol
- In Documentation Center
Search: Symbol
- In Mathematica Notebook
This is a really broad topic, I have solutions for 1, 2, and 3. I'm missing point number 4. So tell us, how do you document your functions with the documentation center?
Update
I have added another answer. Hopefully this answer is more encouraging to users of Mathematica to write documentation pages with their packages. I think that writing documentation pages is beneficial to the application writer as well as the users of the application. If you download the package I wrote I suggest you follow the tutorial so that you can see what happens in every step. This will give you valuable experience for future projects.
Github (May 24, 2014)
Since I wrote the package there has been several people interested in this package. I have uploaded the package to Github: https://github.com/jmlopez-rod/ApplicationMaker. Please contact me if you would like to be a contributor to the repository.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了展示如何创建合并到文档中心的文档,我们将创建一个包含非常简单的函数及其文档的包。让我们调用我们的包
SOPackage
。该包将存储在同名的文件夹中,并且该文件夹应存储在$BaseDirectory
或$UserBaseDirectory$
中。SOPakage
文件夹需要具有以下树结构。请注意,根目录是目录
SOPackage
。SOPackage
现在我们将在
SOPackage
中创建一个新的笔记本文件:SOPackage.nb
。这些是笔记本的内容这是您应该看到的屏幕截图
请注意,使用消息通常将参数格式化为一种特殊的方式。获取此格式的快捷方式(由@alexey-popkov指出)是突出显示要格式化的字母,按Command+0(在Windows中为Alt+0)并输入“TI”。对所有需要修改的字母重复此过程。通过
Cell->CellProperties->Initialization Cell
将单元格更改为初始化单元格。现在我们将此笔记本另存为SOPackage.nb
。如果 Mathematica 没有询问您是否要创建包,因为您忘记将单元格更改为初始化单元格,那么您可以转到Format->OptionInspector
。确保您选择“Options for SOPackage.nb”,否则您需要设置为 true 的选项将显示为灰色。现在单击Notebook Options->FileOptions->AutoGeneratePackage
并选择Automatic
。关闭选项窗口并保存文件。每次保存SOPackage.nb
时,文件SOPackage.m
都会更新(不要弄乱这个 m 文件)。Kernel
目录应仅包含一个文件:init.m
。该文件需要包含下一行:在这之后我们就有了一个工作包。您可以尝试以下操作来确保一切正常:
文档
让我们从创建指南页面开始。当您在文档中心输入
SOPackage
时,将显示该页面。首先,我们创建一个笔记本并将其保存在SOPackage/Documentation/English/Guides
下,命名为SOPackage_E.nb
。我给它扩展名“_E”的原因是因为这将是一个可编辑的副本。请注意,您无法编辑文档中心中的任何文档。好吧,你可以,但这些更改永远不会生效。您可能希望在构建包时修改文档,因此最好拥有一个可以编辑的副本。对于此示例,我们可以复制Combinatorica 指南
的内容,在文档中心输入Combinatorica/guide/CombinatoricaPackage
选择所有包含单元格的内容,将其复制并粘贴到您的SOPackage_E.nb
文件(或者,复制文件C:\Program Files\Wolfram Research\Mathematica\10.4\Documentation\English\Packages\Combinatorica\Documentation\English\Guides\CombinatoricaPackage.nb
或其他操作系统上的同等内容)。进行一些更改,以便您知道它们是您正在做出的更改。就我而言,我用 SOPackage 替换 Combinatorica。如果您无法修改文本的某些部分,您需要执行以下操作:1:单击无法修改的文本。
2:转到
Cell->Show Expression
或输入Command+Shift+E
或 Windows 中的等效键。3:在
Cell
表达式中查找第二个参数(就在任何A -> B
形式的选项)。这是一个样式名称。在某些情况下,您会看到“用法”、“注释”、“对象名称”等。找到无法修改的单元格样式后,单击正在编辑的笔记本,然后输入 Command+Shift+E 以显示表达式。4:转到
格式->编辑样式表...
,在输入样式名称:
下输入您之前看到的样式名称。5:出现显示样式的单元格。确保选择此单元格并转到
Format->Object Inspector
。确保显示显示选项值
选择。6:转到
编辑选项
并将Editable
设置为true。7:修改不可修改的。
我建议您首先输入您希望能够编辑的所有样式的名称,就像我一样如屏幕截图所示。到目前为止,我只更改了我在步骤 3 中提到的那些。将它们放入列表后,将它们全部选中并立即将其设置为可编辑。另一个重要的一点是这个文件可以是一个模板。您应该将此文件保存在某个地方,当您需要制作文档时,只需将其打开,用另一个名称将其保存在正确的路径中,然后开始从现有文档笔记本复制单元格。
如何创建本指南取决于您。现在先来说说废话吧。你会看到我的截图。接下来的两个文件是您的函数的文档。将模板文件复制并粘贴到
SOPackage/Documentation/English/ReferencePages/Symbols
并将文件命名为AddTwo_E.nb
和DotTwo_E.nb
。查找一些您喜欢的文档,以Sin
为例,然后将信息复制并粘贴到这些文件中。我将更改名称只是为了展示它们是如何工作的。肯定可以减少模板文件的创建。如果有人知道如何以编程方式执行此操作,请随时在此处编辑此部分并将其替换为代码。您将为我们大家带来很大的帮助。
PacletInfo.m
该文件位于
SOPackage
目录下,包含以下内容:在获得文档准备好了。我们需要使所有函数文档不可编辑,并且必须为其提供与其余文档相同的格式。这次我编写了一个脚本来执行此操作。我将其称为 MakeDoc,因为它还将构建索引。将此文件保存在
OSPackage
下。我将把这个文件分成两部分,以便您能够得到解释。我们应该确保在执行此操作之前重新启动 Mathematica。首先,我们将加载包并设置所有函数文档的根目录。在下一步中,我们基本上将复制粘贴代码,我们将为每个函数执行以下操作。
此代码块打开可编辑的函数文档。它以正确的名称保存它。然后它更改其属性,使其不可编辑,并使其具有与其他文档相同的外观。我们对每个函数都做同样的事情。请注意,“summary”被设置为函数的
usage
消息。这就是我们搜索该函数时会看到的内容。非常重要的是,我们没有修改指南,这就是所需的一切:
最后,我们像这样创建索引:
请注意,我们需要为每个函数添加一行
AddDocumenationNotebook
。运行MakeDoc.nb
后,将创建文件AddTwo.nb
、DotTwo.nb
和SOPackage.nb
。这些文件无法修改。您还将看到一个名为Index
的文件夹。这是包含文档中心信息的所有二进制数据。如果您对文档进行了修改,您应该运行 MakeDoc.nb 以使更改生效。这是我们现在拥有的文档树。之后我们应该重新启动 Mathematica 并查看我们的文档。
这就是当您查找“SOPackage”时会发生的情况。
让我们看看
AddTwo
的用法请注意,带有指向文档页面的链接的箭头已自动添加。
不幸的是,如果我们在文档中心搜索
AddTwo
,我们会得到以下内容:我们怎样才能使函数的摘要不被格式化?
请随意从此处下载我的代码来修改它。
感谢您的阅读。
曼努埃尔
To show how to create documentation that incorporates to the Documentation Center we will create a package that contains very simple functions and its documentation. Lets call our package
SOPackage
. This package will be stored in a folder of the same name and such folder should be stored in either$BaseDirectory
or$UserBaseDirectory$
. TheSOPakage
folder needs to have the following tree structure.Notice that the root is the directory
SOPackage
.SOPackage
Now we will create a new notebook file inside
SOPackage
:SOPackage.nb
. These are the contents of the notebookHere is a screenshot of what you should see
Note that the usage messages usually format parameters in a special way. A shortcut to get this format (pointed out by @alexey-popkov) is to highlight the letter you want to format, press Command+0 (Alt+0 in windows) and enter "TI". Repeat this process for all the letters that need to be modified. Change the cell to an Initialization Cell via
Cell->CellProperties->Initialization Cell
. Now we save this notebook asSOPackage.nb
. In case Mathematica did not ask you if you want to create a package because you forgot to change the cell to an initialization cell then you can go toFormat->OptionInspector
. Make sure you are selecting "Options for SOPackage.nb" otherwise the option that you need to set to true will be grayed out. Now click toNotebook Options->FileOptions->AutoGeneratedPackage
and selectAutomatic
. Close the options window and save the file. Every time you saveSOPackage.nb
the fileSOPackage.m
will get updated (Do not mess with this m file).The
Kernel
directory should contain only one file:init.m
. This file needs to have the next line:After this we have a working package. You can try the following to make sure that everything is working fine:
Documentation
Lets us start by creating the guide page. This will be the page that will show up when you type
SOPackage
in the doc center. Lets start by creating a notebook and saving it underSOPackage/Documentation/English/Guides
asSOPackage_E.nb
. The reason I'm giving it the extension "_E" is because this will be an editable copy. Notice that you cannot edit any of the documents in the documentation center. Well, you can but these changes never take effect. You will probably want to modify your documentation as you build your package so it is a good idea to have a copy that you can edit. For this example we can copy the contents of theCombinatorica guide
, on the Doc center typeCombinatorica/guide/CombinatoricaPackage
select all with cells, copy and paste them in yourSOPackage_E.nb
file (alternatively, copy the fileC:\Program Files\Wolfram Research\Mathematica\10.4\Documentation\English\Packages\Combinatorica\Documentation\English\Guides\CombinatoricaPackage.nb
or something equivalent on other OSes). Make some changes just so that you know they are the ones you are making. In my case I substituted Combinatorica with SOPackage. In the case you can cannot modify some part of a text this is what you need to do:1: Click on the text that you cannot modify.
2: Go to
Cell->Show Expression
or enterCommand+Shift+E
or something equivalent in windows.3: Look for the second argument in the
Cell
expression (right before any options of the formA -> B
). This is a style name. In some cases you will see "Usage", "Notes", "ObjectName" among others. Once you locate the style of the cell that you cannot modify go click on the notebook you are editing and enterCommand+Shift+E
to show back the expression.4: Go to
Format->Edit StyleSheet...
, enter the style name you saw earlier underEnter a style name:
.5: A cell showing the style appears. Make sure this cell is selected and go to
Format->Object Inspector
. Make sure it saysShow option values
Selection.6: Go to
Editing Options
and setEditable
to true.7: Modify the unmodifiable.
I suggest you first enter the name of all the styles that you want to be able to edit as I have shown in the screenshot. So far I have only changed the ones that I mentioned in step 3. Once you have them in the list select them all and set the to editable at once. Another important point is that this file can be a template. You should save this file somewhere and when you need to make a documentation just open it up, save it with another name in the right path and start copying cells from existing documentation notebooks.
It is up to you how you create this guide. For now lets just put nonsense. You'll see my screenshots. The next two files are the documentation for your functions. Copy and paste your template file to
SOPackage/Documentation/English/ReferencePages/Symbols
and name the filesAddTwo_E.nb
andDotTwo_E.nb
. Look for some documentation that you like, takeSin
for instance, and copy and paste the information to those files. I'll change the names just to show how they work.The creation of the template file can surely be reduced. If someone knows how to do this programmatically please feel free to edit this section here and replace it with the code. You'll do us all a huge favor.
PacletInfo.m
This file goes right under the directory
SOPackage
and contains the following:There is one last thing we must do before we can have the documenation ready. We need to make all of the function documentation uneditable and we have to give it the same format as the rest of the documents. This time I wrote a script that does this. I call it MakeDoc because it will also build the index. Save this file under
OSPackage
. I will break this file intwo 4 parts so that you can get an explanation.We should make sure that we restart Mathematica before doing this. First we will load the package and set the root directory of all of the function documentation. On the next step we will basically copy an paste code, we will be doing the following for each function.
This block of code opens the editable function documentation. It saves it with the correct name. And then it changes its attributes so that its not editable and it gives it the same look as the rest of the documents. We do the same for each function. Notice that "summary" is set to the
usage
message of the function. This is what we will see when we search for the function.Very important, we haven't modified the guide, this is all it takes:
And finally, we create the index like this:
Notice that we need to a line with
AddDocumenationNotebook
for each function. After runningMakeDoc.nb
the filesAddTwo.nb
,DotTwo.nb
andSOPackage.nb
will be created. These files cannot be modifed. You will also see a folder namedIndex
. This is all binary data that contains information for the doc center. If you ever make a modification to the documentation you should runMakeDoc.nb
to make the changes take effect. Here is the document tree that we have now.After this we should restart Mathematica and take our documentation for a ride.
This is what happens when you look for "SOPackage".
Lets look for the usage of
AddTwo
Notice that an arrow with a link to the doc page was added automatically.
Unfortunately, if we search for
AddTwo
in the doc center this is what we obtain:How can we make it so that the summary for the function isn't formatted?
Feel free to modify my code by downloading it from here.
Thank you for reading.
Manuel
我花了一段时间,但我终于完成了一个记录的 Mathematica 应用程序的编写,以帮助 Mathematica 用户编写他们记录的包。
该应用程序称为
ApplicationMaker
。它包含三个具有各种功能的包来帮助您创建应用程序。通过使用这些函数,您可以跳过我在之前的答案中描述的所有混乱。如果您从我的网站下载
ApplicationMaker
,您将找到一个详细的教程,向您展示如何创建完整的应用程序及其文档。概述
要创建新应用程序,您可以通过调用
NewApplication
开始。这将创建我在前面的答案中提到的目录树。要了解有关 Mathematica 文件组织的更多信息,请点击此处。下一步是创建包。为此,您可以调用
NewPackage
。此函数创建一个模板,您可以在其中编写代码。编写完代码后,您需要调用
UpdateInit
。这会更新 Mathematica 所需的 init 文件,以便您可以使用函数Get (<<)
。此时您已准备好创建文档。只需调用
CreateReferencePages
,这将创建一个基本文档,您可以编辑该文档以记录应用程序中每个符号的参考页。如果您想为您的应用程序创建指南或教程,则可以调用
NewGuide
和NewTutorial
。完成编辑后,您需要构建应用程序,以便 Mathematica 可以使其适应其文档中心。您可以通过调用
BuildApplication
来完成此操作。至此你就完成了。如果您在包的任何符号上使用
信息
,您应该会看到附加的箭头,引导您进入该符号的参考页面。如果您希望共享此应用程序,您应该首先部署它。当前应用程序包含与文档中心配合使用的参考页面以及您编辑的参考页面。通过部署它,您将获得一个目录,其中仅包含应用程序运行所需的文件。
安装
您所需要做的就是将文件夹
ApplicationMaker
放入$UserBaseDirectory/Applications/
或$BaseDirectory/Applications/
中。要开始使用,请打开文档中心并查找“ApplicationMaker”。这应该向您展示显示该包包含的所有功能的指南。在最底部您应该会看到教程的链接。
结束语
这是我为 Mathematica 构建的第一个应用程序。我将尝试不断更新软件包,以发现新的东西以使软件包变得更好。目前,我希望 ApplicationMaker 的第一个版本对任何试图记录其 Mathematica 应用程序的人都有用。
您可以在此处下载ApplicationMaker。
It took me while but I have finally finished writing a documented Mathematica application to help Mathematica users write their documented packages.
This application is called
ApplicationMaker
. It contains three packages with various functions to help you create the application. By using these functions you can skip going through all the mess I had described in my previous answer.If you download
ApplicationMaker
from my website you will find a detailed tutorial showing you how to create a complete application with its documentation.Overview
To create a new application you start by calling
NewApplication
. This creates the directory tree I mentioned in the previous answer. To find more about Mathematica's file organization click here.The next step is to create the packages. For that you call
NewPackage
. This function creates a template where you write your code.When you finish writing your code you need to call
UpdateInit
. This updates the init file that Mathematica needs so that you can use the functionGet (<<)
.At this point you are ready to create the documentation. Simply call
CreateReferencePages
and this will create a basic document that you can edit to document the reference pages for each symbol in your application.If you want to create a guide or a tutorial for your application then you can call
NewGuide
andNewTutorial
.When you are done doing your edits you need to build your application so that Mathematica can adapt it to its documentation center. You do this by calling
BuildApplication
.At this point you are done. If you use
Information
on any of the symbols of your package you should see an arrow appended that leads you to the reference page for that symbol.If you wish to share this application you should first deploy it. The current application contains the reference pages which work with the documentation center and those that you edit. By deploying it you obtain a directory with only the necessary files for your application to work.
Installation
All you have to do is drop the folder
ApplicationMaker
in$UserBaseDirectory/Applications/
or$BaseDirectory/Applications/
.To get started open up the documentation center and look for "ApplicationMaker". This should show you the guide showing all the functions that the package contains. At the very bottom you should see a link to the tutorial.
Final Words
This is the first application I have ever build for Mathematica. I will try to keep updating the package I as discover new things to make the package better. For now, I hope that this first version of ApplicationMaker is useful to anybody trying to document their Mathematica applications.
You may download ApplicationMaker here.
我已经下载了您的 ApplicationMaker,并正在 Windows 7 64 位上使用 Mathematica 10 对其进行测试。伟大的工作和有据可查!在使用
NewApplication
创建新应用程序时,我发现了一个小错误,需要修复我的设置。看来函数MakeDirectory
中的变量root
不能是长度为零的字符串(导致创建目录时出错)。我通过在原始代码中包含测试来修复此问题:I have downloaded your ApplicationMaker and is testing it out using Mathematica 10 on Windows 7 64 Bit. Great work and well documented! I discovered a small error that needed a fix on my set up when creating a new application using
NewApplication
. It appears that the variableroot
in the functionMakeDirectory
cannot be a string with zero length (causes error in creating the directories). I fixed this by including a test in your original code: