将内联格式添加到使用消息的便捷方法

发布于 2024-11-16 18:00:58 字数 1699 浏览 5 评论 0 原文

内置函数的使用消息具有嵌入的内联格式。例如:

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 Messages.

What is the convenient way to add in-line formatting to user-defined usage Messages 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 Messages in my package just with Mathematica, without installing additional components like Workbench etc.

P.S. The syntax of the embedded in-line formatting in Strings 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.

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

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

发布评论

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

评论(3

梦晓ヶ微光ヅ倾城 2024-11-23 18:00:58

处理此类字符串的问题之一是,Mathematica 中的大多数字符串操作都会自动将反斜杠 (\) 替换为转义的反斜杠 (\\)。

如果您尝试以下操作:

在此处输入图像描述

您可能会认为您拥有要查找的字符串(减去 \! 使其成为一个表达式),但实际上它是: "\\(x\\_\\(1, 2\\) \[Equal] \\(\\( -b\\) \[加减] \\@\\(b\\^2 -\\(4\\a\\c\\)\\)\\)\\/\\(2\\a\\)\\)"

我的解决方案远非优雅,但它有效。

  1. 从格式化表达式生成框:
    在此处输入图像描述
  2. 选择输出,然后转到菜单项 Cell >转换为>输入表单。结果:
    在此处输入图像描述
  3. 您现在可以编辑该字符串,将 \! 放在其前面,然后周围引用:
    "\!\(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:

enter image description here

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.

  1. Generate boxes from your formatted expression:
    enter image description here
  2. Select the output, and go to menu item Cell > Convert to > InputForm. Result:
    enter image description here
  3. You can now edit the string, putting \! 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

£噩梦荏苒 2024-11-23 18:00:58

我认为最简单的方法是使用前端来格式化字符串。
如果您正在编写一个包,则可以使用“自动保存包”(即当笔记本的初始化单元的内容成为包时)。
如果你使用文本编辑器来编写包那么使用格式化可能会很麻烦......

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 ...

初相遇 2024-11-23 18:00:58

这个答案是对 Sjoerd 答案的补充。

首先,我们不必使用FrontEnd命令Cell>;转换为> InputForm 获取框的线性形式。我们只需将 InputForm 应用于 MakeBoxes 的输出即可获得:

In[1]:= InputForm@MakeBoxes[Subscript[x, 1,2]==(-b\[PlusMinus]Sqrt[b^2-4 a c])/(2 a)]
Out[1]//InputForm=
\(x\_\(1, 2\) == \(\(-b\) \[PlusMinus] \@\(b\^2 - \(4\ a\ c\)\)\)\/\(2\ a\)\)

其次,我们可以通过以下两种方式导出最终的排版内联字符串表示形式,效果等效结果(唯一的区别是 Put 包裹了文本):

OutputForm@
  StringInsert[
   ToString[
    InputForm@
     MakeBoxes[
      Subscript[x, 1, 2] == (-b \[PlusMinus] Sqrt[b^2 - 4 a c])/(
       2 a)], OutputForm], "\\!", 1] >> "C:\\input.txt"

Export["C:\\input.txt", 
 StringInsert[
  ToString[InputForm@
    MakeBoxes[
     Subscript[x, 1, 2] == (-b \[PlusMinus] Sqrt[b^2 - 4 a c])/(2 a)],
    OutputForm], "\\!", 1], "String"]

在这两种情况下,我们都会得到一个只有一行的文件: \!\(x\_\(1, 2\) == \(\(-b\) ± \@\(b\^2 - \(4\ a\ c\)\)\)\/\(2\ a\)\)

在前端的笔记本中插入此行会给出原始表达式(您自己尝试一下!):

screenshot

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 applying InputForm to the output of MakeBoxes:

In[1]:= InputForm@MakeBoxes[Subscript[x, 1,2]==(-b\[PlusMinus]Sqrt[b^2-4 a c])/(2 a)]
Out[1]//InputForm=
\(x\_\(1, 2\) == \(\(-b\) \[PlusMinus] \@\(b\^2 - \(4\ a\ c\)\)\)\/\(2\ a\)\)

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):

OutputForm@
  StringInsert[
   ToString[
    InputForm@
     MakeBoxes[
      Subscript[x, 1, 2] == (-b \[PlusMinus] Sqrt[b^2 - 4 a c])/(
       2 a)], OutputForm], "\\!", 1] >> "C:\\input.txt"

Export["C:\\input.txt", 
 StringInsert[
  ToString[InputForm@
    MakeBoxes[
     Subscript[x, 1, 2] == (-b \[PlusMinus] Sqrt[b^2 - 4 a c])/(2 a)],
    OutputForm], "\\!", 1], "String"]

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!):

screenshot

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