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
MakeBoxes[b, TraditionalForm] = "c";
b
In the browser, this will even apply to TeXForm, because TeXForm implies TraditionalForm:
b // TeXForm
Notice however that in the CLI, default output formatted in OutputForm,
which do not take into account MakeBoxes rules. The same happens if we
ask explicitly for this form:
b // OutputForm // TeXForm
In a similar way, in the CLI, we can ask for TraditionalForm explicitly
b // TraditionalForm // TeXForm
MakeBoxes for another form:
MakeBoxes[TeXForm[b], form_] = "d";
b // TeXForm
You can cause a much bigger mess by overriding MakeBoxes than by sticking to Format, e.g. generate invalid XML:
MakeBoxes[MathMLForm[c], form_] := "<not closed";
c // MathMLForm //StandardForm
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___}, TraditionalForm] := RowBox[{"[", Sequence @@ Riffle[MakeBoxes /@ {items}, " "], "]"}]
{1, 2, 3}
{1, 2, 3} // TeXForm
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___], TraditionalForm] := squared[args] ^ 2
squared[1, 2]
squared[1, 2] // TeXForm
=
The desired effect can be achieved in the following way:
MakeBoxes[squared[args___], TraditionalForm] := 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[%]