**2.8.16 Defining Output Formats**

Just as Mathematica allows you to define how expressions should be evaluated, so also it allows you to define how expressions should be formatted for output. The basic idea is that whenever Mathematica is given an expression to format for output, it first calls Format[expr] to find out whether any special rules for formatting the expression have been defined. By assigning a value to Format[expr] you can therefore tell Mathematica that you want a particular kind of expression to be output in a special way.

This tells Mathematica to format bin objects in a special way.
In[1]:= **Format[bin[x_, y_]] := MatrixForm[{{x}, {y}}]**

Now bin objects are output to look like binomial coefficients.
In[2]:= **bin[i + j, k]**

Out[2]=

Internally, however, bin objects are still exactly the same.
In[3]:= **FullForm[%]**

Out[3]//FullForm=

Defining your own rules for formatting.

By making definitions for Format, you can tell Mathematica to format a particular expression so as to look like another expression. You can also tell Mathematica to run a program to determine how a particular expression should be formatted.

This specifies that Mathematica should run a simple program to determine how xrep objects should be formatted.
In[4]:= **Format[xrep[n_]] := StringJoin[Table["x", {n}]]**

The strings are created when each xrep is formatted.
In[5]:= **xrep[1] + xrep[4] + xrep[9]**

Out[5]=

Internally however the expression still contains xrep objects.
In[6]:= **% /. xrep[n_] -> x^n**

Out[6]=

Output forms for operators.

This prints with f represented by the "prefix operator" <>.
In[7]:= **Prefix[f[x], "<>"]**

Out[7]=

Here is output with the "infix operator" <>.
In[8]:= **s = Infix[{a, b, c}, "<>"]**

Out[8]=

By default, the "infix operator" <> is assumed to have "higher precedence" than ^, so no parentheses are inserted.
In[9]:= **s^2**

Out[9]=

When you have an output form involving operators, the question arises of whether the arguments of some of them should be parenthesized. As discussed in Section 2.1.3, this depends on the "precedence" of the operators. When you set up output forms involving operators, you can use PrecedenceForm to specify the precedence to assign to each operator. Mathematica uses integers from 1 to 1000 to represent "precedence levels". The higher the precedence level for an operator, the less it needs to be parenthesized.

Here <> is treated as an operator with precedence 100. This precedence turns out to be low enough that parentheses are inserted.
In[10]:= **PrecedenceForm[s, 100]^2**

Out[10]=

When you make an assignment for Format[expr], you are defining the output format for expr in all standard types of Mathematica output. By making definitions for Format[expr,form], you can specify formats to be used in specific output forms.

This specifies the TeXForm for the symbol x.
In[11]:= **Format[x, TeXForm] := "{\\bf x}"**

[0]

The output format for x that you specified is now used whenever the TeX form is needed.
In[12]:= **TeXForm[1 + x^2]**

Out[12]//TeXForm= 1 + {{{\bf x}}^2}