# Error invoking Information on functions containing Legended

In adapting some plotting functions to utilize the PlotLegends option of version 9, I came across the following errors:

f[x_] := Legended[Plot[Sin[t], {t, 0, Pi}], x]
f[1];
Information[f]


TextForm[First::first: StandardForm[Short[Shallow[HoldForm[{}], {10, 50}], 5]] has a length of zero and no first element. >>]

This is just a toy example for a more complex situation where I can't directly use the PlotLegends option but have to use Legended explicitly.

What is the best way to catch this error when a user invokes the above or types ?f to see the code for this function. It seems the output of Definition works as expected (it formats the Legended contents, and that happens for other constructs such as Framed too).

-
For the error in ?f, specifying usage message is enough f::usage = "bla";. – swish May 5 '13 at 20:48
Wierd.. I can reproduce the problem at first time. But after doing some tracing, I quit the kernel, then the problem is gone! I restarted the whole mma, still no problem now! – Silvia May 5 '13 at 20:52
@Silvia Yeah, me too! Weird indeed... – swish May 5 '13 at 20:57
Mmm not sure whether this one needs to be reported as a bug or exorcized – Dr. belisarius May 5 '13 at 21:07
@swish Guess evaluating f introduced something to box functions. Check this: SystemDumpmakeusageboxes[f[x_] := Legended["123", x]]. – Silvia May 5 '13 at 21:14

I Trace-ed the Information[ f ] command both before and after an invocation of f (with a simpler definition f[x_] := Legended["123", x]). By comparing the results, I found a function SystemDumpmakeusageboxes:

which reduces to another function BoxFormMakeBoxesWithTextFormatting:

Attributes[BoxFormMakeBoxesWithTextFormatting] = {HoldAllComplete}

BoxFormMakeBoxesWithTextFormatting[BoxFormargs___] :=
Block[Evaluate[BoxForm$TextFormattingForms], Block[{BoxFormUseTextFormattingQ = True}, MakeBoxes[BoxFormargs]]]  By setting BoxFormUseTextFormattingQ to False (which is its default global value), the problem seems to be bypassed: Unprotect[BoxFormMakeBoxesWithTextFormatting] BoxFormMakeBoxesWithTextFormatting[BoxFormargs___] := Block[Evaluate[BoxForm$TextFormattingForms], MakeBoxes[BoxFormargs]]

Protect[BoxFormMakeBoxesWithTextFormatting]


# Edit

As it might be safer not to modify system functions, here is another possible workaround (the disadvantage is an extra UpValue item):

f /: Information[f, opt_] :=
Block[{InternalConvertFormTest = (InputForm &)}, Information[f, opt]] /;
InternalConvertFormTest[] === None

Information[f]


I don't know why, but somehow InternalConvertFormTest[] is called after an invocation of f. Playing around it reveals its connection to some box formatting.

-
Unfortunately, your edited solution doesn't work with my original function. And your first solution (using Unprotect) still doesn't work with the function I defined in this answer which motivated my question. But certainly some kind of UpValues` solution will be the way to go, even if it has to be more hand-tuned... so I upvoted this. I'll wait a little more before accepting it. – Jens May 6 '13 at 3:16
@Jens Hmm.. More and more looks like a bug, I guess? Will try digging deeper tomorrow.. – Silvia May 6 '13 at 15:31
I agree it behaves like a bug. Would you mind adding the bug tag to the question (so I don't get in trouble for doing it myself)? – Jens May 7 '13 at 2:38
@Jens Of course not. Done. – Silvia May 7 '13 at 5:12