The following can be used as a starting point. It transforms the usage string to itsEdit:
Since my old approach with TeXForm
, the replaces certain substrings turned out to obtainbe quite a more readable string. After some iterationsbad idea, the string no longer changes and has hopefully become something that looks likehere is a simple string representations of the usage messagenew one that uses InputForm
. It is much more stable and already correctly covers many, many symbols. Let's start with the code:
transformusageString[s_Symbol] := StringReplace[
StringReplace[
StringReplace[Module[{string,
rules = StringReplace[{
"\\\"" StringReplace[
~~ a___ ~~ "\\\"" /; StringFreeQ[a, "\\"] :> StringReplace[a,
"\"" StringReplace[
~~ a__ ~~ "\"" :> a,
StringReplace[
"StyleBox[" ~~ a___ ~~ ", " ~~ ("TI]" | "TR]") /; StringReplace[
StringFreeQ[a, "]"] :> StringReplace[a,
"\\!\\(\\*" ~~ a___ ~~ "\\)" /; #StringFreeQ[a,
"\\("] :> a,
"SubscriptBox[" ~~ a__ "\\text{.~~ $\\backslash $n}" ->, ".\n"],
~~ b__ ~~ "]" /;
"$" ~~ a : Except["$"] ..StringFreeQ[a ~~<> "$"b, :>"Box" a],| "]"] :> a <> "_" <> b,
"SuperscriptBox[" ~~ a__
~~ ", " ~~ b__ ~~ "]" /;
"\\textit{" ~~ b___ ~~ "}"StringFreeQ[a /;<> StringFreeQ[bb, "{""Box" | "}"]"]"] :> b]a <> "^" <> b,
"SubsuperscriptBox[" ~~ a___ ~~ ", "\\text{" ~~ c___b___ ~~ "}" /; StringFreeQ[c, "{" |~~ "}"]c___ :>~~ c],
"]" "\\left"/; ~~StringFreeQ[a d_<> :>b If[StringMatchQ[d<> c, "]""Box" | "["],"]"] d,:> ""]],
a "\\right"<> ~~"_(" d_<> :>b If[StringMatchQ[d,<> "]"")^(" |<> "["],c d,<> ""]],")"}
},
string = "_{"
~~ e___ ~~Fold[StringReplace, "}"ToString[MessageName[s, /;"usage"], StringFreeQ[eInputForm], "{"
| "}"] :> "_"rules];
<> e]string = FixedPoint[StringReplace[#,
"^"RowBox[{" ~~ e___a__ ~~ "}"]" /; StringFreeQ[eStringFreeQ[a, "{""RowBox" | "}"]]"] :> "^"
<> e],
"\\inta] "&,
-> "\[Integral] "], string];
StringReplace[StringJoin@StringSplit[string, ", "\\ldots""], "\\n" -> "\[Ellipsis]"] &;
"\n"]
usageString[s_] := FixedPoint[transform, ToString@TeXForm[s]]]
For many functions with simple usage messagesBefore covering the main problem that usageString
still has, let's have a look at what it works:
usageString[Minus::usage]
-x is the arithmetic negation of x.
It sort of works for more complicated usage messagescan do (note thatsorry, but I designedhave to use images to convey this first version explicitly with Integrate
in mind):
usageString[Integrate::usage]
Integrate[f,x] gives the indefinite integral \[Integral] f dx.
Integrate[f,{x,x_\min ,x_\max }] gives the definite integral \[Integral] _x_\min ^x_\max f dx.
Integrate[f,{x,x_\min ,x_\max },{y,y_\min ,y_\max },\[Ellipsis] ] gives the multiple integral \[Integral] _x_\min ^x_\max dx\[Integral]_y_\min ^y_\max dy \[Ellipsis] f.
Of course there are stillYou can see that it transforms many problems with special symbols and theirof the TeXFormRowBox
, as well as with basic layoutingSupersciptBox
etc. For exampleconstructs found in those fancy usage messages to standard strings. It still lacks some tranformation rules, just tryhowever, for things like usageString[D::usage]UnderoverscriptBox
. or StyleBox
with options:
I will try to fixthink that by adding some of those issues. Inmore replacement rules to cover the meantimeremaining boxing constructs and options, I hope this can serve ascould be a starting point for other people's approachesnice way to get simple string representations of the fancy 2D strings.