# Formatting output

The way results are formatted for output in Mathics is rather sophisticated, as compatibility to the way Mathematica® does things is one of the design goals. It can be summed up in the following procedure:

1. The result of the query is calculated.
2. The result is stored in Out (which % is a shortcut for).
3. Any Format rules for the desired output form are applied to the result. In the console version of Mathics, 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.
4. MakeBoxes is applied to the formatted result, again given either OutputForm, MathMLForm, or TeXForm depending on the execution context of Mathics. This yields a new expression consisting of “box constructs”.
5. The boxes are turned into an ordinary string and displayed in the console, sent to the browser, or written to the documentation LaTeX file.
As a consequence, there are various ways to implement your own formatting strategy for custom objects.

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]
formats the arguments of expr with infix operator op.
Prefix[expr, op]
formats the argument of expr with prefix operator op.
Postfix[expr, op]
formats the argument of expr with postfix operator op.
StringForm[form, arg1, arg2, ...]
formats arguments using a format string.

• 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[{...}]
displays expressions in a row.
Grid[{{...}}]
displays a matrix in two-dimensional form.
Subscript[expr, i1, i2, ...]
displays expr with subscript indices i1, i2, ...
Superscript[expr, exp]
displays expr with superscript (exponent) exp.

• Grid[{{a, b}, {c, d}}]

• Subscript[a, 1, 2] // TeXForm

If you want even more low-level control of how expressions are displayed, you can 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 Mathics 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]

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 delimeters are not shown in the default output form:

• InputForm[%]