Wolfram Computation Meets Knowledge

PrettyForm

Contributed by: Taliesin Beynon

Print human-readable form of complex expressions

ResourceFunction["PrettyForm"][expr]

attempts to present expr in a human-readeable form.

Details and Options

ResourceFunction["PrettyForm"] is useful for interpreting structured expressions.
ResourceFunction["PrintDefinitions"] uses ResourceFunction["PrettyForm"].
Symbol contexts are displayed in ResourceFunction["PrettyForm"] as tooltips, and color coded similarly to ResourceFunction["PrintDefinitions"].

Examples

Basic Examples

Examine a ragged array:

In[1]:=
ResourceFunction["PrettyForm"][Table[x^i + j, {i, 3}, {j, i}]]
Out[1]=

Inspect Wolfram Language code:

In[2]:=
ResourceFunction["PrettyForm"][
 Unevaluated[
  Module[{f}, f[1] = f[2] = 1; f[i_] := f[i] = f[i - 1] + f[i - 2]; f[n]]]]
Out[2]=

Hide symbol contexts in tooltips:

In[3]:=
ResourceFunction["PrettyForm"][{foo`symbol, bar`symbol}]
Out[3]=

Interpret a Graphics object:

In[4]:=
ResourceFunction["PrettyForm"][
 Graphics[{Thick, Green, Rectangle[{0, -1}, {2, 1}], Red, Disk[], Blue, Circle[{2, 0}], Yellow, Polygon[{{2, 0}, {4, 1}, {4, -1}}], Purple, Arrowheads[Large], Arrow[{{4, 3/2}, {0, 3/2}, {0, 0}}], Black, Dashed, Line[{{-1, 0}, {4, 0}}]}]]
Out[4]=

Applications

Examine a Wolfram Language function definition:

In[5]:=
ClearAttributes[Information, ReadProtected]
In[6]:=
ResourceFunction["PrettyForm"][Information[Information]]
Out[6]=

Properties and Relations

InputForm prints explicit contexts of symbols:

In[7]:=
dataset = Dataset[{
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>}]
Out[7]=
In[8]:=
InputForm[dataset]
Out[8]=

PrettyForm provides context information in tooltips:

In[9]:=
ResourceFunction["PrettyForm"][dataset]
Out[9]=

PrettyForm is not a member of $OutputForms:

In[10]:=
$OutputForms
Out[10]=
In[11]:=
MemberQ[$OutputForms, ResourceFunction["PrettyForm"]]
Out[11]=

Therefore, PrettyForm does not get stripped off when wrapped around the output:

In[12]:=
ResourceFunction["PrettyForm"][foo`bar]
Out[12]=
In[13]:=
%
Out[13]=
In[14]:=
InputForm[%]
Out[14]=

Compare with, for instance, MatrixForm, which is a member of $OutputForms:

In[15]:=
MatrixForm[{{1, 2}, {3, 4}}]
Out[15]=
In[16]:=
%
Out[16]=
In[17]:=
InputForm[%%]
Out[17]=

Resource History