# Usage displays properly only after second call

If I have a usage message containig two-dimensional stuff (subscripts or formulas) and I call the usage, then sometimes I get an improperly formatted output enclosed in "...". Linebreaks are gone, but interestingly some formulas survive. Another ?example outputs fine.

It's hard to give an example, because it happens randomly.
Is this a known issue and and if so, is there a fix?

This is the output, when evaluated the first (bad) and second time (ok):

And this is the original usage:

FlaechenPlot::usage=
"FlaechenPlot[{\!$$\*SubscriptBox[\(f$$, $$1$$]\), \!$$\*SubscriptBox[\(f$$, $$2$$]\),...}, {x, \!$$\*SubscriptBox[\(x$$, $$min$$]\), \!$$\*SubscriptBox[\(x$$, $$max$$]\)}, {\!$$\*SubscriptBox[\(f$$, $$min$$]\), \!$$\*SubscriptBox[\(f$$, $$max$$]\)}] zeichnet die Funktionen \!$$\*SubscriptBox[\(f$$, $$i$$]\) im Bereich zwischen \!$$\*SubscriptBox[\(x$$, $$min$$]\) und \!$$\*SubscriptBox[\(x$$, $$max$$]\) und f\[ADoubleDot]rbt den Bereich zwischen \!$$\*SubscriptBox[\(f$$, $$min$$]\) und \!$$\*SubscriptBox[\(f$$, $$max$$]\) gem\[ADoubleDot]\[SZ] der gew\[ADoubleDot]hlten Option Filling.
FlaechenPlot[f, {x, \!$$\*SubscriptBox[\(x$$, $$min$$]\), \!$$\*SubscriptBox[\(x$$, $$max$$]\)}, {\!$$\*SubscriptBox[\(f$$, $$min$$]\), \!$$\*SubscriptBox[\(f$$, $$max$$]\)}] zeichnet entprechend den Bereich zwischen f und der x\[Dash]Achse.
FlaechenPlot[{\!$$\*SubscriptBox[\(f$$, $$1$$]\), \!$$\*SubscriptBox[\(f$$, $$2$$]\),...}, {x, \!$$\*SubscriptBox[\(x$$, $$min$$]\), \!$$\*SubscriptBox[\(x$$, $$max$$]\)}] zeichnet und f\[ADoubleDot]rbt das Gebiet zwischen den Kurven im Bereich von \!$$\*SubscriptBox[\(x$$, $$min$$]\) bis \!$$\*SubscriptBox[\(x$$, $$max$$]\).
FlaechenPlot[f, {x, \!$$\*SubscriptBox[\(x$$, $$min$$]\), \!$$\*SubscriptBox[\(x$$, $$max$$]\)}] f\[ADoubleDot]rbt und zeichnet das Gebiet zwischen Kurve und x\[Dash]Achse im Bereich von \!$$\*SubscriptBox[\(x$$, $$min$$]\) bis \!$$\*SubscriptBox[\(x$$, $$max$$]\).
OPTIONEN sind alle Optionen von Plot. Die Plot\[Dash]Option Filling \[Rule] Axis ist default."

-
I have seen this problem myself several times. I can confirm that this happens. –  Szabolcs Apr 5 '12 at 19:27
I'm actually seeing a similar problem with Options and SyntaxInformation for a function I've defined in a custom package. When first loading a package, all the options and inputs work, but aren't highlighted properly. Loading the package twice fixes things... –  tkott Apr 6 '12 at 16:34
Hmm, only happens to me when restarting frontend. If you restart the kernel, things are fine... –  tkott Apr 6 '12 at 17:12
Trace@?example is fascinating. So is ctrl+shift+e: the difference is the addition of extra slashes  for every one present. –  tkott Apr 6 '12 at 17:27

I think I have found a solution to this issue, which has been a problem since at least Mathematica version 6 and continues through version 10. The problem occurs when a user-defined usage message has complex formatting, for example, subscripts, entered using 2D input.

Suppose we have a function with usage message defined as

which also can be input as

f::usage = "\!$$\*SubscriptBox[\(test$$, $$1$$]\)\n\!$$\*SubscriptBox[\(test$$, $$2$$]\)"


If we evaluate ?f once, we get

which is incorrectly formatted, but if we evaluate it again in the same input cell, without deleting the output cell, we get the correct version:

To see what's going on, we can run Trace[Information[f]], but that produces an enormous output, most of which has nothing to do with formatting the output string. To look for mentions of the usage string itself, we can use

Trace[Information[f], x_String /; !StringFreeQ[x, "test"], TraceBackward -> True]


This fingers the function SystemDumpfixmessagestring as a possible culprit. The function definition can be found with

InputForm@Definition[SystemDumpfixmessagestring]


which returns

SystemDumpfixmessagestring[SystemDumps_] :=
StringJoin[
"\"",
StringReplace[SystemDumps, {"\n" -> "\\n", "\\" -> "\\\\", "\"" -> "\\\""}],
"\""
]


This function converts some special characters in the string to explict form, as is done in InputForm. However, there are many kinds of formatting that this function doesn't address, which may be part of the problem. We can redefine this function to do a more thorough job by evaluating

SystemDumpfixmessagestring[SystemDumps_] := ToString@InputForm@SystemDumps


and now ?f returns the correct result every time. It's still not clear to me why the original version would cause a problem on the first evaluation, but not on the second, but, regardless, it seems that we have a fix.

A concern might be if this has an effect on built-in usage messages, but in my testing, the built-in messages display identically.

The redefinition could be done inside the user-defined package, making the fix invisible.

Also, with the command On[SystemDumpfixmessagestring], we can have Mathematica send us a trace message whenever SystemDumpfixmessagestring is called. That will let us see if the function is ever called anywhere outside of Information[], where the redefinition might have other consequences. So far it seems that the function is just confined to Information[] and ?.

Since there seems to be a one-line fix to this problem, it would be nice if it were eventually fixed in Mathematica itself.

-
This is truly a very interesting discovery! How did you find the fixmessagestring function? –  Silvia Apr 11 at 13:07
@Silvia I edited my answer to describe how I found it. –  Simon Rochester Apr 16 at 0:43
Thanks! It's very helpful to know the process! :) –  Silvia Apr 16 at 8:03