Formatting Output

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:


  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 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.
  4. 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”.
  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[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.

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, $i_1$, $i_2$, ...]

displays expr with subscript indices $i_1$, $i_2$, ...

Superscript[expr, exp]

displays expr with superscript (exponent) exp.

If you want even more low-level control over expression display, 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 Mathics3 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 delimiters are not shown in the default output form:

Comparisons and Boolean Logic
Functions and Patterns