Formatting

Mathematica has a number of useful formatting heads for arbitrary expressions, which fall into two main categories: spatial formatting and styling

Grid, Row, and Column

These are the primary spatial formatting heads. They primarily let you arrange lists nicely:

Column@RandomInteger[20,20]

(*Out:*)

22-333367112565904913

Row@Riffle[RandomInteger[20,20],", "]

(*Out:*)

Row[{20, ", ", 19, ", ", 18, ", ", 7, ", ", 0, ", ", 19, ", ", 18, 
  ", ", 19, ", ", 0, ", ", 20, ", ", 9, ", ", 3, ", ", 4, ", ", 18, 
  ", ", 11, ", ", 18, ", ", 11, ", ", 17, ", ", 15, ", ", 18}]

Grid@Array[RandomInteger[20,20]&,20]

(*Out:*)

22-6000845343406865510

Each of these comes with their own options and formatting rules, but the idea is the same for all of them. I’ll just point out a few things:

Anything can be part of a formatted expression:

Grid@Array[Array[Graphics[{RandomColor[],Disk[]},ImageSizeRandomInteger[{5,20}]]&, 5] &, 5]

(*Out:*)

22-7326022483212805039

The internal expression is also still possible to recover using First :

First@%

22-2463392914657171378

And a Grid is not just a Column of Rows :

Row/@%//Column

(*Out:*)

22-901697867635371312

Grid in particular has three special elements types that change how spacing is done, SpanFromLeft , SpanFromAbove , and SpanFromBoth . It is not always clear exactly what combination of spanning elements will achieve the desired effect, but just play around with them until the output looks right and you’ll be fine.

Be sure to look at all of the options for Grid as there are many cool things one can do. As a quick example, one can put in dividers with arbitrary coloring:

Grid[Array[Array[Graphics[{RandomColor[],Disk[]},ImageSizeRandomInteger[{5,20}]]&, 5] &, 5],Dividers{Array[#->RandomColor[]&,6],Array[#->RandomColor[]&,6]}]

(*Out:*)

22-96816251163000789

Similarly look at Item because it provides more fine-grained control over grid formatting, which can often be useful.

Style, Framed, Pane, and Panel

The other class of formatting heads is the styling heads. First among them is Style which applies styling to an expression, particularly text:


Style[HoldForm[Row@{∫0πCos[θ]Sin[θ]θ,Spacer[25],Graphics[Disk[]]}],BackgroundRandomColor[],FontSizeLarge,FontColorRandomColor[],FontFamilyRandomChoice@$FontFamilies,FontVariations{"Underline" -> True,"Underlight"Purple},FontWeightBold,AutoStyleWords{"Cos""Text","Sin""Section"}]

(*Out:*)

22-671211221228907509

Essentially any styling one can apply to a Cell , one can apply to an expression using Style

Framed on the other hand does one thing. It puts a frame around an expression:

Framed["Are you enjoying the tutorial?"]

(*Out:*)

22-4823551658722428068

But what makes it useful is the styling one can apply to the frame:

Framed[Style["Are you enjoying the tutorial?",Blue],RoundingRadius5,FrameStyleGrayLevel[.8],BackgroundLightBlue]

(*Out:*)

22-1114857042421127565

It does nothing to affect the display of the actual expression though.

In contrast, that is all Pane does. Pane merely provides a way to change how an expression displays on screen. It’s chief use is for preventing large objects from taking up lots of display space:

Pane[ExampleData[{"Text","USConstitution"}],ImageSize{500,250}]

(*Out:*)

22-4737251508472738868

The display size can be variable and one can also attach scroll bars and things:

Pane[ExampleData[{"Text","USConstitution"}],ImageSize{ {Automatic,500},250},Scrollbars{False, Automatic}]//Framed[#,FrameMargins{ {3,0},{0,0} }]&

(*Out:*)

22-4939794383627769131

Pane has less customization than the rest of the styling heads, but is possibly the most useful when designing quality interfaces

Panel is different from the other styling heads in that it is a sort of amalgamation of all three:

Panel["Check this out"]

(*Out:*)

22-5388265198911020986

It basically generates a frame and applies default text styling, but the size of the panel can also be set:

Panel[ExampleData[{"Text","USConstitution"}],ImageSize{500,250}]

(*Out:*)

22-6197124947707195918

Unfortunately scrolling is disabled, so often one wraps a Pane in a Panel to achieve that:

Panel[Pane[ExampleData[{"Text","USConstitution"}],ImageSize{500,250},Scrollbars{False, Automatic}]]

(*Out:*)

22-3037897936210855976

Panel is a nice, simple, reasonably professional looking go-to for arbitrary formatting, so it should often be the one of the first things one considers when formatting and expression

Format and Interpretation

The use for most of this formatting is often to hide the complexity of an expression. One leverages the power of the Format function and Interpretation functions to make an ugly expression look nice.

Usually one does this by defining an inert formatting head to wrap around the expression and defining the formatting for that expression:

Format[ExampleDataFormatter[data_,title_]]:=
Interpretation[
Panel[Column[{
Style[title,Large,Bold],
Panel@Pane[
data,
ImageSize->{ {Automatic,400},150},
Scrollbars->{False, Automatic}
]},
Dividers->Center]
],
ExampleDataFormatter[data,title]
];
ExampleDataFormatter[
ExampleData[{"Text","USConstitution"}],"The United States Constitution"]

(*Out:*)

22-9001538133314045945

Column@{StringLength@First@%,Head@%,Last@%}

(*Out:*)

22-8871422904547814990

This allows the often messy internal structure of a Mathematica expression to be hidden by a nice, content-rich format.

results matching ""

    No results matching ""