Skip to main content
completely removed old code, added new code and pictures
Source Link
einbandi
  • 4.1k
  • 2
  • 24
  • 42

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.

enter image description here

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:

enter image description here

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.

The following can be used as a starting point. It transforms the usage string to its TeXForm, the replaces certain substrings to obtain a more readable string. After some iterations, the string no longer changes and has hopefully become something that looks like a simple string representations of the usage message.

transform = StringReplace[
    StringReplace[
     StringReplace[
      StringReplace[
       StringReplace[
        StringReplace[
         StringReplace[
          StringReplace[
           StringReplace[
            StringReplace[
             #,
             "\\text{. $\\backslash $n}" -> ".\n"],
            "$" ~~ a : Except["$"] .. ~~ "$" :> a],               
            "\\textit{" ~~ b___ ~~ "}" /; StringFreeQ[b, "{" | "}"] :> b],
          "\\text{" ~~ c___ ~~ "}" /; StringFreeQ[c, "{" | "}"] :> c],
         "\\left" ~~ d_ :> If[StringMatchQ[d, "]" | "["], d, ""]],
        "\\right" ~~ d_ :> If[StringMatchQ[d, "]" | "["], d, ""]],
       "_{" ~~ e___ ~~ "}" /; StringFreeQ[e, "{" | "}"] :> "_" <> e],
      "^{" ~~ e___ ~~ "}" /; StringFreeQ[e, "{" | "}"] :> "^" <> e],
     "\\int " -> "\[Integral] "],
    "\\ldots" -> "\[Ellipsis]"] &;

usageString[s_] := FixedPoint[transform, ToString@TeXForm[s]]

For many functions with simple usage messages it works:

usageString[Minus::usage]

-x is the arithmetic negation of x.

It sort of works for more complicated usage messages (note that I designed 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 still many problems with special symbols and their TeXForm, as well as with basic layouting. For example, just try usageString[D::usage]. I will try to fix some of those issues. In the meantime, I hope this can serve as a starting point for other people's approaches.

Edit:

Since my old approach with TeXForm turned out to be quite a bad idea, here is a new one that uses InputForm. It is much more stable and already correctly covers many, many symbols. Let's start with the code:

usageString[s_Symbol] := Module[{string,
   rules = {
     "\\\"" ~~ a___ ~~ "\\\"" /; StringFreeQ[a, "\\"] :> a,
     "\"" ~~ a__ ~~ "\"" :> a,
     "StyleBox[" ~~ a___ ~~ ", " ~~ ("TI]" | "TR]") /; 
       StringFreeQ[a, "]"] :> a,
     "\\!\\(\\*" ~~ a___ ~~ "\\)" /; StringFreeQ[a, "\\("] :> a,  
     "SubscriptBox[" ~~ a__ ~~ ", " ~~ b__ ~~ "]" /;  
       StringFreeQ[a <> b, "Box" | "]"] :> a <> "_" <> b,
     "SuperscriptBox[" ~~ a__ ~~ ", " ~~ b__ ~~ "]" /;  
       StringFreeQ[a <> b, "Box" | "]"] :> a <> "^" <> b,
     "SubsuperscriptBox[" ~~ a___ ~~ ", " ~~ b___ ~~ ", " ~~ c___ ~~ 
        "]" /; StringFreeQ[a <> b <> c, "Box" | "]"] :> 
      a <> "_(" <> b <> ")^(" <> c <> ")"}
   },
  string =  
   Fold[StringReplace, ToString[MessageName[s, "usage"], InputForm],  
    rules];
  string = FixedPoint[StringReplace[#,
      "RowBox[{" ~~ a__ ~~ "}]" /; StringFreeQ[a, "RowBox" | "}]"] :>  
       a] &,
    string];
  StringReplace[StringJoin@StringSplit[string, ", "], "\\n" -> "\n"]
  ]

Before covering the main problem that usageString still has, let's have a look at what it can do (sorry, but I have to use images to convey this):

enter image description here

You can see that it transforms many of the RowBox, SupersciptBox etc. constructs found in those fancy usage messages to standard strings. It still lacks some tranformation rules, however, for things like UnderoverscriptBox or StyleBox with options:

enter image description here

I think that by adding some more replacement rules to cover the remaining boxing constructs and options, this could be a nice way to get simple string representations of the fancy 2D strings.

Source Link
einbandi
  • 4.1k
  • 2
  • 24
  • 42

The following can be used as a starting point. It transforms the usage string to its TeXForm, the replaces certain substrings to obtain a more readable string. After some iterations, the string no longer changes and has hopefully become something that looks like a simple string representations of the usage message.

transform = StringReplace[
    StringReplace[
     StringReplace[
      StringReplace[
       StringReplace[
        StringReplace[
         StringReplace[
          StringReplace[
           StringReplace[
            StringReplace[
             #,
             "\\text{. $\\backslash $n}" -> ".\n"],
            "$" ~~ a : Except["$"] .. ~~ "$" :> a],               
           "\\textit{" ~~ b___ ~~ "}" /; StringFreeQ[b, "{" | "}"] :> b],
          "\\text{" ~~ c___ ~~ "}" /; StringFreeQ[c, "{" | "}"] :> c],
         "\\left" ~~ d_ :> If[StringMatchQ[d, "]" | "["], d, ""]],
        "\\right" ~~ d_ :> If[StringMatchQ[d, "]" | "["], d, ""]],
       "_{" ~~ e___ ~~ "}" /; StringFreeQ[e, "{" | "}"] :> "_" <> e],
      "^{" ~~ e___ ~~ "}" /; StringFreeQ[e, "{" | "}"] :> "^" <> e],
     "\\int " -> "\[Integral] "],
    "\\ldots" -> "\[Ellipsis]"] &;

usageString[s_] := FixedPoint[transform, ToString@TeXForm[s]]

For many functions with simple usage messages it works:

usageString[Minus::usage]

-x is the arithmetic negation of x.

It sort of works for more complicated usage messages (note that I designed 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 still many problems with special symbols and their TeXForm, as well as with basic layouting. For example, just try usageString[D::usage]. I will try to fix some of those issues. In the meantime, I hope this can serve as a starting point for other people's approaches.