... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)


Mathematica's Format Pseudofunction

In Mathematica, we can define customized output formats for expressions using the Format function. For example:

In[1]:= Format[_thing] := "a thing"

In[2]:= thing[1, 2, 3]
Out[2]= a thing

Even though thing[1, 2, 3] now displays as a thing, its true form remains unchanged:

In[3]:= thing[1, 2, 3] // FullForm
Out[3]//FullForm= thing[1,2,3]

One might think that the formatted form an expression can be recovered by applying the Format function. Interestingly, this is not the case:

In[4]:= Format[thing[1, 2, 3]] // FullForm
Out[4]//FullForm= Format[thing[1,2,3]]

Format is simply a wrapper that self-evaluates. A more elaborate technique is necessary to obtain an expression's formatted form:

In[5]:= Format[thing[1, 2, 3]] /. FormatValues[thing] // FullForm
Out[5]//FullForm= "a thing"

Notwithstanding the Format[_thing] := ... delayed assignment, we can see that Format has not actually acquired a new definition:

In[6]:= ??Format

Format[expr] prints as the formatted form of expr.
Assigning values to Format[expr] defines print forms for expressions.
Format[expr,form] gives a format for the specified form of output.  >>


It has no definitions at all, in fact. Apparently, assignments to Format are handled in a special manner, tucking the definition away under the FormatValues of a symbol.

Blog Archive