A convenient way to add inline formatting to message usage

Using Messages built-in functions has built-in built-in 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." 

You can see that this in-line formatting is based on the set of styles defined in the "Styles for Inline Formatting" section of the Core.nb style sheet. But I did not find any documentation for these styles, as well as a description of a convenient algorithm for adding formatting to usage Message s.

What is a convenient way to add formatting to a string in a custom usage Message in Mathematica? What are the rules for using default styles for in-line formatting defined in the Core.nb stylesheet? I would like to add string formatting to usage Message in my package with Mathematica only, without installing additional components like Workbench etc.

PS The syntax of the built-in inline formatting in String partially documented in the manual page for String Representation of Boxes . "A related question in the official newsgroup on this syntax is:" (any documentation for) linear syntax? "The display of such lines in FrontEnd is controlled by the ShowStringCharacters->False Cell option.

+6
wolfram-mathematica mathematica-frontend
Jun 21 2018-11-11T00:
source share
3 answers

One of the problems with processing such strings is that most string operations in Mathematica automatically replace the backslash ( \ ) with the escaped backslash ( \\ ).

If you try this:

enter image description here

you might think that you have the string you are looking for (minus \! to make it an expression), but actually it is: "\\(x\\_\\(1, 2\\) \[Equal] \\(\\(-b\\) \[PlusMinus] \\@\\(b\\^2 -\\(4\\ a\\ c\\)\\)\\)\\/\\(2\\ a\\)\\)"

My solution is far from elegant, but it works.

  • Generate fields from a formatted expression: enter image description here
  • Select the output and go to the menu item Cell > Convert to > InputForm . Result: enter image description here
  • Now you can edit the line by putting \! before it and quotation marks around it: "\!\(x \_ \(1, 2\) == \(\(-b\) \[PlusMinus] \@\(b \^2 - \(4\ a\ c\)\)\) \/ \(2\ a\)\)"

If you follow step 3 in an external editor, leave a quote and just \! in front and paste back the result in MMA, it is directly converted to a formatted expression

+1
Jun 21 '11 at 12:38
source share

I think the easiest way is to simply use Front End to format the string. If you are writing a package, you can use the "automatic saving of packages" (i.e. when the contents of the notebook's initialization cells become a package). If you use a text editor to write a package, it may be too complicated to use formatting ...

+1
Jun 21 '11 at 8:17
source share

This answer is in addition to Sjoerd's answer.

First of all, we do not need to use the FrontEnd Cell > Convert to > InputForm to get the linear shape of the boxes. We can get it by simply applying an InputForm to the output of the MakeBoxes :

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

Secondly, we can export the final string string string representation of the boxes in two ways with the 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 ac])/( 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 ac])/(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 into Notepad in FrontEnd gives the original expression (try it yourself!):

screenshot

+1
Jun 21 2018-11-21T00:
source share



All Articles