Skip to main content

Output forms

What you see as an expression in the output cell

input cell
1/2

is the output form of expression Times[1, Power[2,-1]].

Wolfram Language cells

By the default all expressions in Wolfram Language cells are printed using so-called StandardForm, which is usually an original expression decorated with some syntax sugar. This means that most output expressions are also valid for input and evaluation.

StandardForm automatically transforms on output all fraction-like expressions into eye-pleasing fractions like this one 12\frac{1}{2}, all graphical elements into actual visible graphics, colors to small color boxes and etc.

To bypass all of this and reveal the actual expression behind the syntax sugar you can apply InputForm or FullForm:

RGBColor[1, 0, 0]

Define your own

tip

To define your own representation you can follows the guide Decorating symbols, but here we will try simpler examples.

Let's say we have a special symbol specialSymbol[], we want to decorate. For this reason we define UpValues for MakeBoxes expression applied on any output:

specialSymbol /: MakeBoxes[s_specialSymbol, StandardForm] := "49"

If you try to evaluate now specialSymbol you might be surprised:

specialSymbol[]
49

However this approach breaks the holy rule of Wolfram Language:

  • output result has be valid input expression

Here 49 is valid, but no longer connected to specialSymbol, i.e.

49

To fix that we can use InterpretationBox:

specialSymbol /: MakeBoxes[s_specialSymbol, StandardForm] := InterpretationBox["49", s]

Now if we evaluate:

specialSymbol[]

And InputForm reveals the actual symbol decorated by 49:

specialSymbol[]
tip

Check it by yourself by coping it from the cell to a normal text-editor app.

We can go further and replace it with a little icon image:


round[i_, r_] :=
Module[{w, h},
{w, h} = ImageDimensions@i;
With[{
f = Reverse,
p = PadRight[DiskMatrix[r][[;; r, ;; r]], {h, w}, 1]},
i~ImageAdd~ColorNegate@Image[p f /@ p f@p f[f /@ p]]]]



specialSymbol /: MakeBoxes[s_specialSymbol, StandardForm] := With[{
icon = round[Colorize[Blur[RandomImage[LaplaceDistribution[0.1, .1], {100, 100}],20]], 30]},

With[{
img = MakeBoxes[icon, StandardForm]
},
InterpretationBox[img, s]
]
]

Now if we try:

specialSymbol[]

We will get a randomly generated icon

What if the output form is not defined?

It will fallback to an InputForm.

Overall diagram

These lead us to the following diagram:

If you may noticed there are some differences for:

  • _Sqrt
  • _Graphics | _Image

Fractions, square roots are using different technique optimized for the case if you have them many in the final expression, while DateObject and RGBColor, Hue and man others rely directly on ViewBox.

Graphics, Graphics3D, Image, Image3D, Sound are usually quite "heavy" object and keeping them inside our editor would be a huge mess. Therefore they are converted to a more compact form - Frontend Objects to save up space keeping only the reference in the actual output expression.

In the end we should always get a string.

WLX, Slide, Markdown cells

It follows similar principles as Wolfram Language cells. The default output form is WLXForm and the end result again has to be a string.

Since Slides, Markdown, WLX outputs are not meant to be edited we should not worry about the expressions behind and focus only on the displayed ones.

Let's try this example:

SpecialSymbol /: MakeBoxes[SpecialSymbol, WLXForm] := "49"

Note: we capitalized the name that it can be correctly parsed by WLX

.slide

## Have a look at

<SpecialSymbol/>

We can do the same for graphical symbols:

round[i_, r_] :=
Module[{w, h},
{w, h} = ImageDimensions@i;
With[{
f = Reverse,
p = PadRight[DiskMatrix[r][[;; r, ;; r]], {h, w}, 1]},
i~ImageAdd~ColorNegate@Image[p f /@ p f@p f[f /@ p]]]]



SpecialSymbol /: MakeBoxes[SpecialSymbol, WLXForm] := With[{
icon = round[Colorize[Blur[RandomImage[LaplaceDistribution[0.1, .1], {100, 100}],20]], 30]},

With[{
img = MakeBoxes[icon, WLXForm]
},
img
]
]

We do not have as many WLXForm definitions as for StandardForm for the obvious reasons. In thee case of WLXForm is not explicitly defined ToString will be applied directly.