Expression Formatting in Mathematica Notebooks

A few examples of Mathematica's unprecedentedly powerful expression formatting capabilities.

Recursive Expressions

NestList[(Sqrt[1 + #])&, λ, 5]

NestList[RadicalBox[#, #]&, x, 4] // DisplayForm

NestList[GridBox[Table[ButtonBox[#], {2}, {2}]]&, •, 
        2] // DisplayForm

This creates another recursive typeset structure.

NestList[SubsuperscriptBox[#, #, #]&, "X", 4] // DisplayForm

Manipulating Box Structures

Factor[(x^12) - 1]

boxes = ToBoxes[%]

DisplayForm[boxes /. SuperscriptBox -> OverscriptBox]

DisplayForm[(# /. a : RowBox[_] -> FrameBox[a])&//@boxes]

Changing Styles

Table[StyleBox["α", FontSize -> i], {i, 60}] // DisplayForm

RowBox[
        Table[StyleBox["\[MathematicaIcon]", FontSize -> ((i^2)/6.5)], {i, 
            2, 23}]] // DisplayForm

Table[
        StyleBox["\[MathematicaIcon]", FontColor -> Hue[i/75.5]], {i, 
          60}] // DisplayForm

RandomizedStyle[expr_] := 
      StyleBox[expr, FontSize -> Random[Integer, {8, 20}], 
        FontColor -> GrayLevel[Random[]/2], 
        FontSlant 
          -> {"Plain", "Italic"}[[1 + 
              Random[Integer]]], 
        FontWeight 
          -> {"Plain", "Bold"}[[1 + 
              Random[Integer]]]]

Do[Print[
        RowBox[RandomizedStyle/@Characters["This text looks strange"]] // 
          DisplayForm], {5}]

Coloring Expressions

Table[
        StyleBox[ToBoxes[Factor[(x^i) - 1]], Background -> Hue[(i/12.)]], {
          i, 12}] // DisplayForm

Needs["Graphics`Colors`"]

RowBox[
        StyleBox[AdjustmentBox[#, BoxMargins -> {{0, 0}, {1, 1}}], 
              Background -> ToExpression[#], FontSize -> 10]&/@AllColors]
         // DisplayForm

Coloring Box Structures

RandomColoring[expr_] := 
      (# /. 
              x : (_RowBox | _FractionBox | _OverscriptBox | _RadicalBox
                       | _SqrtBox | _SubscriptBox | _SubsuperscriptBox
                       | _SuperscriptBox | _UnderoverscriptBox
                       | _UnderscriptBox) :> 
                StyleBox[x, Background -> Hue[Random[], .5]])&//@
        ToBoxes[expr]

RandomColoring[∫(1/((x^5) - 1)) \[DifferentialD]x] // 
      DisplayForm

DepthColoring[expr_] := 
      Module[{b = ToBoxes[expr], d}, d = 2 Depth[b]; 
        (# /. 
                x : (_RowBox) :> 
                  StyleBox[x, Background -> Hue[0.78 Depth[x]/d, .5]])
              &//@b]

DepthColoring[∫(1/((x^5) - 1)) \[DifferentialD]x] // 
      DisplayForm

boxtypes
       = {RowBox, FractionBox, OverscriptBox, RadicalBox, SqrtBox, 
        SubscriptBox, SubsuperscriptBox, SuperscriptBox, UnderoverscriptBox, 
        UnderscriptBox};

boxrules = 
      MapIndexed[
        (#[x__] -> 
              StyleBox[#[x], 
                Background -> Hue[N[First[#2]/Length[boxtypes]], .8]])&, 
        boxtypes];

TypeColoring[expr_] := (#1 /. boxrules&)//@ToBoxes[expr]

TypeColoring[∫(1/((x^5) - 1)) \[DifferentialD]x] // 
      DisplayForm

Visual Bracket Matching

TransformBrackets[f_, expr_] := 
      DisplayForm[FindBrackets[ToBoxes[expr], f]]

\(FindBrackets[startexpr_, f_] := 
	
      Fold[Function[{expr, \ location}, 
		
          ReplacePart[expr, 
            f[expr[[Sequence\ @@\ location
                  ]], Length[location]], location]], 
        startexpr, 
		
        Sort[Position[startexpr, \ 
            RowBox[{a___, "{"\  | \ "["\  | \ "(", \ b__, \ 
                "]"\  | \ "}"\  | \ ")", \ c___}]], \ 
          Length[#1]\  > \ Length[#2]&]]\)

\(SizeBrackets[expr_] := 
      TransformBrackets[
        StyleBox[#1, SpanMinSize -> \((#2 - 2)\), 
					
            SpanMaxSize\  -> \ \((#2 - 2)\)]&, expr]\)

NestList[f, x, 5]

SizeBrackets[%]

ColorBrackets[expr_] := 
      TransformBrackets[
        StyleBox[AdjustmentBox[#1, BoxMargins -> {{0, 0}, {.5, .5}}],  
            Background -> Hue[Mod[N[#2/10], 1], .5]]&, expr]

ColorBrackets[NestList[f, x, 5]]

ColorBrackets[∫(1/((x^5) - 1)) \[DifferentialD]x]