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:

This will apply to MathMLForm, OutputForm, StandardForm, TeXForm, and TraditionalForm.

You can specify a specific form in the assignment to Format:

Special formats might not be very relevant for individual symbols, but rather for custom functions (objects):

You can use several helper functions to format expressions:

Infix[exprop]
formats the arguments of expr with infix operator op.
Prefix[exprop]
formats the argument of expr with prefix operator op.
Postfix[exprop]
formats the argument of expr with postfix operator op.
StringForm[formarg1arg2, ...]
formats arguments using a format string.

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[expri1i2, ...]
displays expr with subscript indices i1, i2, ...
Superscript[exprexp]
displays expr with superscript (exponent) exp.

If you want even more low-level control of how expressions are displayed, you can override MakeBoxes:

This will even apply to TeXForm, because TeXForm implies StandardForm:

Except some other form is applied first:

MakeBoxes for another form:

You can cause a much bigger mess by overriding MakeBoxes than by sticking to Format, e.g. generate invalid XML:

However, this will not affect formatting of expressions involving c:

That's because MathMLForm will, when not overridden for a special case, call StandardForm first. Format will produce escaped output:

For instance, you can override MakeBoxes to format lists in a different way:

However, this will not be accepted as input to Mathics anymore:

By the way, MakeBoxes is the only built-in symbol that is not protected by default:

MakeBoxes must return a valid box construct:

The desired effect can be achieved in the following way:

You can view the box structure of a formatted expression using ToBoxes:

The list elements in this RowBox are strings, though string delimeters are not shown in the default output form: