内置函数的使用消息
具有嵌入的内联格式。例如:
In[1]:= ActionMenu::usage // FullForm
Out[1]//FullForm=
"\!\(\*RowBox[{\"ActionMenu\", \"[\", RowBox[{StyleBox[\"name\", \"TI\
\"], \",\", RowBox[{\"{\", \
RowBox[{RowBox[{SubscriptBox[StyleBox[\"lbl\", \"TI\"], \
StyleBox[\"1\", \"TR\"]], \":>\", SubscriptBox[StyleBox[\"act\", \"TI\
\"], StyleBox[\"1\", \"TR\"]]}], \",\", \
RowBox[{SubscriptBox[StyleBox[\"lbl\", \"TI\"], StyleBox[\"2\", \
\"TR\"]], \":>\", SubscriptBox[StyleBox[\"act\", \"TI\"], \
StyleBox[\"2\", \"TR\"]]}], \",\", StyleBox[\"\[Ellipsis]\", \
\"TR\"]}], \"}\"}]}], \"]\"}]\) represents an action menu with label \
\!\(\*StyleBox[\"name\", \"TI\"]\), and with items labeled \
\!\(\*SubscriptBox[StyleBox[\"lbl\", \"TI\"], StyleBox[\"i\", \
\"TI\"]]\), that evaluates the expression \
\!\(\*SubscriptBox[StyleBox[\"act\", \"TI\"], StyleBox[\"i\", \
\"TI\"]]\) if the corresponding item is chosen."
可以看到,这种内联格式基于 Core.nb 样式表的“内联格式的样式”部分中定义的一组样式。但我还没有找到这些样式的任何文档,也没有找到任何向 usage
Message
添加格式的便捷算法的描述。
在 Mathematica 中向用户定义的 usage
Message
添加内联格式的便捷方法是什么? Core.nb样式表中定义的内联格式默认样式的使用规则是什么?我想仅使用 Mathematica 将内联格式添加到包中的 usage
Message
中,而无需安装 Workbench 等其他组件。
PS String
中嵌入的内联格式的语法部分记录在教程页面“框的字符串表示。"官方新闻组中有关此语法的相关问题:“ (任何文档)线性语法?”在前端中显示此类字符串是由选项 ShowStringCharacters->False
控制的单元格
。
Usage Messages
of built-in functions have embedded in-line formatting. For example:
In[1]:= ActionMenu::usage // FullForm
Out[1]//FullForm=
"\!\(\*RowBox[{\"ActionMenu\", \"[\", RowBox[{StyleBox[\"name\", \"TI\
\"], \",\", RowBox[{\"{\", \
RowBox[{RowBox[{SubscriptBox[StyleBox[\"lbl\", \"TI\"], \
StyleBox[\"1\", \"TR\"]], \":>\", SubscriptBox[StyleBox[\"act\", \"TI\
\"], StyleBox[\"1\", \"TR\"]]}], \",\", \
RowBox[{SubscriptBox[StyleBox[\"lbl\", \"TI\"], StyleBox[\"2\", \
\"TR\"]], \":>\", SubscriptBox[StyleBox[\"act\", \"TI\"], \
StyleBox[\"2\", \"TR\"]]}], \",\", StyleBox[\"\[Ellipsis]\", \
\"TR\"]}], \"}\"}]}], \"]\"}]\) represents an action menu with label \
\!\(\*StyleBox[\"name\", \"TI\"]\), and with items labeled \
\!\(\*SubscriptBox[StyleBox[\"lbl\", \"TI\"], StyleBox[\"i\", \
\"TI\"]]\), that evaluates the expression \
\!\(\*SubscriptBox[StyleBox[\"act\", \"TI\"], StyleBox[\"i\", \
\"TI\"]]\) if the corresponding item is chosen."
One can see that this in-line formatting is based on the set of styles defined in "Styles for Inline Formatting" section of the Core.nb stylesheet. But I have not found any documentation for these styles as well as any description of the convenient algorithm of adding formatting to usage
Message
s.
What is the convenient way to add in-line formatting to user-defined usage
Message
s in Mathematica? What are the usage rules of default styles for in-line formatting defined in the Core.nb stylesheet? I would like to add in-line formatting to the usage
Message
s in my package just with Mathematica, without installing additional components like Workbench etc.
P.S. The syntax of the embedded in-line formatting in String
s is partially documented on the tutorial page "String Representation of Boxes." Related question in the official newsgroup on this syntax: "(any documentation for) linear syntax?" Displaying of such strings in the FrontEnd is controlled by the option ShowStringCharacters->False
of Cell
.
发布评论
评论(3)
处理此类字符串的问题之一是,Mathematica 中的大多数字符串操作都会自动将反斜杠 (
\
) 替换为转义的反斜杠 (\\
)。如果您尝试以下操作:
您可能会认为您拥有要查找的字符串(减去
\!
使其成为一个表达式),但实际上它是:"\\(x\\_\\(1, 2\\) \[Equal] \\(\\( -b\\) \[加减] \\@\\(b\\^2 -\\(4\\a\\c\\)\\)\\)\\/\\(2\\a\\)\\)"
我的解决方案远非优雅,但它有效。
Cell >转换为>输入表单
。结果:\!
放在其前面,然后周围引用:"\!\(x \_ \(1, 2\) == \(\(-b\) \[PlusMinus] \@\(b \^2 - \(4\ a\ c\) \)\) \\/ \(2\ a\)\)"
如果您在外部编辑器中执行步骤 3,请去掉引号,只需将
\!
放在前面并粘贴返回 MMA 中的结果,直接转换为格式化表达式One of the problems of handling strings like that is that most operations with strings in Mathematica automatically replace the backslash (
\
) with the escaped backslash (\\
).If you try this:
you may think you have the string you're looking for (minus the
\!
to make it an expression), but in fact it is:"\\(x\\_\\(1, 2\\) \[Equal] \\(\\(-b\\) \[PlusMinus] \\@\\(b\\^2 -\\(4\\ a\\ c\\)\\)\\)\\/\\(2\\ a\\)\\)"
My solution is far from elegant, but it works.
Cell > Convert to > InputForm
. Result:\!
in front of it and quotes around it:"\!\(x \_ \(1, 2\) == \(\(-b\) \[PlusMinus] \@\(b \^2 - \(4\ a\ c\)\)\) \/ \(2\ a\)\)"
If you perform step 3 in an external editor, leave away the quotes and just have
\!
in front and paste back the result in MMA it is directly converted to the formatted expression我认为最简单的方法是使用前端来格式化字符串。
如果您正在编写一个包,则可以使用“自动保存包”(即当笔记本的初始化单元的内容成为包时)。
如果你使用文本编辑器来编写包那么使用格式化可能会很麻烦......
I think the easiest way is to just use the Front End to format your string.
If you are writing a package, you can use "auto-save packages" (i.e. when the contents of the initialization cells of a notebook become the package).
If you use a text editor to write the package then it might just be too much trouble to use formatting ...
这个答案是对 Sjoerd 答案的补充。
首先,我们不必使用FrontEnd命令
Cell>;转换为> InputForm
获取框的线性形式。我们只需将InputForm
应用于MakeBoxes
的输出即可获得:其次,我们可以通过以下两种方式导出最终的排版内联字符串表示形式,效果等效结果(唯一的区别是
Put
包裹了文本):在这两种情况下,我们都会得到一个只有一行的文件:
\!\(x\_\(1, 2\) == \(\(-b\) ± \@\(b\^2 - \(4\ a\ c\)\)\)\/\(2\ a\)\)
。在前端的笔记本中插入此行会给出原始表达式(您自己尝试一下!):
This answer is an addition to Sjoerd's answer.
First of all, we do not have to use the FrontEnd command
Cell > Convert to > InputForm
to get the linear form of boxes. We can get it just by applyingInputForm
to the output ofMakeBoxes
:Secondly, we can export the final typesetting in-line string representation of boxes by the following two ways with equivalent result (the only difference is that
Put
wraps the text):In both cases we get a file with one line:
\!\(x\_\(1, 2\) == \(\(-b\) ± \@\(b\^2 - \(4\ a\ c\)\)\)\/\(2\ a\)\)
.Inserting this line in a Notebook in the FrontEnd gives the original expression (try it by yourself!):