The way results are formatted for output in Mathics3 is rather sophisticated, compatibility with Mathematica® is one of the design goals. It can be summed up in the following procedure:
Out
(which %
is a shortcut for).Format
rules for the desired output form are applied to the result. In the console version of Mathics3, the result is formatted as OutputForm
; MathMLForm
for the StandardForm
is used in the interactive Web version; and TeXForm
for the StandardForm
is used to generate the LaTeX version of this documentation.MakeBoxes
is applied to the formatted result, again given either OutputForm
, MathMLForm
, or TeXForm
depending on the execution context of Mathics3. This yields a new expression consisting of “box constructs”.You can specify how a symbol shall be formatted by assigning values to Format
:
Format[x] = "y";
x
This will apply to MathMLForm
, OutputForm
, StandardForm
, TeXForm
, and TraditionalForm
.
x // InputForm
You can specify a specific form in the assignment to Format
:
Format[x, TeXForm] = "z";
x // TeXForm
Special formats might not be very relevant for individual symbols, but rather for custom functions (objects):
Format[r[args___]] = "<an r object>";
r[1, 2, 3]
You can use several helper functions to format expressions:
Infix
[expr, op]Prefix
[expr, op]Postfix
[expr, op]StringForm
[form, $arg1$, $arg2$, ...]Format[r[args___]] = Infix[{args}, "~"];
r[1, 2, 3]
StringForm["`1` and `2`", n, m]
There are several methods to display expressions in 2-D:
Row[{...}]
Grid[{{...}}]
Subscript
[expr, $i_1$, $i_2$, ...]Superscript
[expr, exp]Grid[{{a, b}, {c, d}}]
Subscript[a, 1, 2] // TeXForm
If you want even more low-level control over expression display, override MakeBoxes
:
MakeBoxes[b, StandardForm] = "c";
b
This will even apply to TeXForm
, because TeXForm
implies StandardForm
:
b // TeXForm
Except some other form is applied first:
b // OutputForm // TeXForm
MakeBoxes
for another form:
MakeBoxes[b, TeXForm] = "d";
b // TeXForm
You can cause a much bigger mess by overriding MakeBoxes
than by sticking to Format
, e.g. generate invalid XML:
MakeBoxes[c, MathMLForm] = "<not closed";
c // MathMLForm
However, this will not affect formatting of expressions involving c
:
c + 1 // MathMLForm
That's because MathMLForm
will, when not overridden for a special case, call StandardForm
first.Format
will produce escaped output:
Format[d, MathMLForm] = "<not closed";
d // MathMLForm
d + 1 // MathMLForm
For instance, you can override MakeBoxes
to format lists in a different way:
MakeBoxes[{items___}, StandardForm] := RowBox[{"[", Sequence @@ Riffle[MakeBoxes /@ {items}, " "], "]"}]
{1, 2, 3}
However, this will not be accepted as input to Mathics3 anymore:
[1 2 3]
Clear[MakeBoxes]
By the way, MakeBoxes
is the only built-in symbol that is not protected by default:
Attributes[MakeBoxes]
MakeBoxes
must return a valid box construct:
MakeBoxes[squared[args___], StandardForm] := squared[args] ^ 2
squared[1, 2]
squared[1, 2] // TeXForm
=
The desired effect can be achieved in the following way:
MakeBoxes[squared[args___], StandardForm] := SuperscriptBox[RowBox[{MakeBoxes[squared], "[", RowBox[Riffle[MakeBoxes[#]& /@ {args}, ","]], "]"}], 2]
squared[1, 2]
You can view the box structure of a formatted expression using ToBoxes
:
ToBoxes[m + n]
The list elements in this RowBox
are strings, though string delimiters are not shown in the default output form:
InputForm[%]