If you take a look at the Combinatorica
package in Mathematica8 in (mathematicapath)/AddOns/LegacyPackages/DiscreteMath/Combinatorica.m
you will find the definitions of functions. What I'm interested to know is how Mathematica knows how to format the usage messages. Something tells me that I'm not looking at the right file. In any case, lets try the following:
Cofactor::usage = "Cofactor[m, {i, j}] calculates the (i, j)th cofactor of matrix m."
This line is the 682 line in the file mentioned above. Now if we run it in a mathematica notebook and we use ?Cofactor
we will see the exact same message. But if we get the package then the message is formatted. Here is a screenshot:
Notice how the m, i and j inside the function changed and a double arrow was added to the message. I think the arrow was added to the message because there exists documentation for it. Can someone explain this behavior?
EDIT: This is a screenshot of my notebook file that autosaves to an m file.
As you can see, the L
and M
are in italic times new roman. Now I will load the package and see the usage.
So far so good. Now lets look at the Documentation center. I will look for the function LineDistance
.
As you can see, it shows a weird message. In this case we only want to display the message without any styles. I still can't figure out how the Combinatorica
package does this.
I followed this to make the index so that the doc center can display the summary. The summary is essentially the usage display. Let me know if I need to be more specific.
OK, here's the explanation.
Digging in the Combinatorica source reveals this:
It is loading messages from
ToFileName[{System`Private`$MessagesDir,$Language},"Usage.m"]
, which on my machine isSystemFiles\Kernel\TextResources\English\Usage.m
. This is why all usage messages are created conditionally inCombinatorica.m
(only if they don't exist yet). If you look inUsage.m
you'll see it has all the ugly boxes stuff that @ragfield mentioned.I guess the simplest way to have formatted messages is to edit them in the front end in a notebook, and create an auto-save package. This way you can use all the front end's formatting tools, and won't need to deal with boxes.
The way to embed style information in a String expression is to use linear syntax. For a box expression such as:
You can embed this inside of a String by adding
\*
to the front of it and escaping any special characters such as quotes:This should work for any box expression, no matter how complicated:
I am currently working on rewriting your ApplicationMaker for newer Mathematica-Versions with added functionalities and came to the exact same question here.
My answer is simple: Mathematica dont allowes you to use formated summaries for your symbols (or even build in symbols), so we have to unformate the usage-strings for the summaries. The usagestring itself can still have formatting, but one needs to have a function that removes all the formatingboxes from a string.
i have a solution that uses the
UndocumentedTestFEParserPacket
as described by John Fultz! in this question.This funny named Tool parses a String Input into the real unchanged Mathematica BoxForm.
This is my example code:
and this is how the Output looks like (first Output formatted fancy
str0
, second simple flatstr2
)Code Explanation:
str0 is the formatted usagestring with all the StyleBoxes and other formatting boxes.
str1:
UndocumentedTestFEParserPacket[str0, True]
gives Boxes and strips off allStyleBoxes
, thats because the second argument is True. First Replacement removes allRowBoxes
. The outerBoxForm
changed to a List of strings. Whitespaces are inserted between these strings the byRiffle
. SubscriptBox gets a special treatment. The last line replaces every remaining FormatBox such asUnderoverscriptBox
and it does that by adding Whitespaces between the arguments, and returning the arguments as a flat Sequence.was added to include more cases such as
StringReplace::usage
. This cases include string representations""
with Styles inside of a the usage-string, when"args"
has to be given as strings.str2:
In this block of code i only remove unwanted
WhitespaceCharacter
from the string str1 and i add linebreaks"/n"
after the"."
, because they got lost during the Parsing. There are 3 different cases whereWhitespaceCharacter
can be removed. 1 removing left-and right sidedWithespaceCharacter
from a character like"["
. 2. and 3. removing WithespaceCharacter from left(2) or right(3) side.Summary
Istead of
summary-> mySymbol::usage
, usesummary -> unformatString[mySymbol::usage]
withunformatString
being an appropriate function that performes the unformating like descriped above.Alternatively you can define another usage message manually like
than use summary -> mySymbol::usage2
I will answer on how the link in the
Message
is generated. TracingMessage
printing shows a call to undocumentedDocumentation`CreateMessageLink
function which returns the URL to the corresponding Documentation page if this page exists:In some cases we can also see calls to
Internal`MessageButtonHandler
which further callsDocumentation`CreateMessageLink
: