3

I have the following code:

Remove[f];
f::usage = "f[\"message\"] prints the message";
f[message_] := Print[message];

The usage message works except the quotation marks which are not shown in the information popup.

Mathematica Wrong Usage Message

As an alternative one could use \\[OpenCurlyDoubleQuote] and \\[CloseCurlyDoubleQuote] but it is a bit long to write:

Remove[f];
f::usage = "f[\\[OpenCurlyDoubleQuote]message\\[CloseCurlyDoubleQuote]] prints the message";
f[message_] := Print[message];

With the resulting popup:

Mathematica Curly Quotes Usage Message

Do you know any way to make this shorter or get Mathematica to show the normal quotation marks?

Qbyte
  • 469
  • 3
  • 9

1 Answers1

4

Here's a way around that, making use of the very-hard-to-work-with string-embedded syntax for boxes:

Remove[f]
f[a_] := 1;
f::usage = "\!\(f[\*StyleBox[\\\"message\\\", \"TI\", ShowStringCharacters->True]]\) generates a plot of \!\(\*StyleBox[\"f\", \"TI\"]\) as a function of \!\(\*StyleBox[\"x\", \"TI\"]\) from \!\(\*SubscriptBox[StyleBox[\"x\", \"TI\"], StyleBox[\"min\", \"TI\"]]\) to \!\(\*SubscriptBox[StyleBox[\"x\", \"TI\"], StyleBox[\"max\", \"TI\"]]\).";

What did I do here? First I took the ::usage message for Plot, coped out the first line, and then made modifications of the argument structure by hand. This will look like:

enter image description here

The styling trick is that I took the first StyleBox in the message for Plot and added a ShowStringCharacters->True

One weird thing that seems to come from how Mathematica parses these templates, if you want to add spaces to the message you need to do them in a RowBox and change the formatting a bit, e.g.:

Remove[f]
f[a_] := 1;
f::usage = "\!\(f[\*StyleBox[RowBox[{\"\\\"this\", \" \", \"is\", \" \", \"a\", \" \", \"message\\\"\"}], \"TI\", ShowStringCharacters->True]]\) generates a plot of \!\(\*StyleBox[\"f\", \"TI\"]\) as a function of \!\(\*StyleBox[\"x\", \"TI\"]\) from \!\(\*SubscriptBox[StyleBox[\"x\", \"TI\"], StyleBox[\"min\", \"TI\"]]\) to \!\(\*SubscriptBox[StyleBox[\"x\", \"TI\"], StyleBox[\"max\", \"TI\"]]\).";
b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • Thank you for your answer! Is there a tutorial how to use these boxes in general? Please, can you explain what the usage string exactly means? – Qbyte Mar 23 '20 at 00:54
  • The best tutorial is to look around at stuff on the site. In general most things that the FE renders, like Style are represented as a "box", e.g. StyleBox. The format \!\(\*<body>\) means that whatever is inside <body> will be rendered as boxes, not as a string. That's about it. – b3m2a1 Mar 23 '20 at 00:58
  • Ok thank you. The only thing which I could not find was "TI" in StringBox. How is it used/useful? – Qbyte Mar 23 '20 at 01:06
  • Ah the "TI" is an italic text style that the FE uses all over the place, but that I wouldn't worry about. The only reason I left it in was to show that I copied this stuff directly from the usage message for Plot. You can get rid of it everywhere it occurs without worry. – b3m2a1 Mar 23 '20 at 01:07
  • Thank you again. Note for others: You can reverse engineer the usage messages by printing e.g. File::usage and copy and paste it into a string literal ("") – Qbyte Mar 23 '20 at 01:22
  • One problem I found is that if the "message" contains spaces it breaks. E.g. if one wants to make the usage message f["This is a message"] – Qbyte Mar 23 '20 at 13:43
  • @Qbyte see the update – b3m2a1 Mar 23 '20 at 18:02