Looking into the structure of a formula

Ivan Cnop
icnop@vub.ac.be

Introduction:

Each  Mathematica  object is a mathematical expression and every detail of it can be modified. 
We will use this fact to color the structure of formulas. 
We will obtain a reversed palette structure: it will be obvious how palette buttons are nested to obtain a complcated expression.

Programming is given in  Mathematica  demo's :

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] &]]\)

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

Remark: modifications to these programming lines are  not recommended

Structure of formulas

Nesting

Clear[f]

ColorBrackets[NestList[f, x, 7]]

Atoms are never colored:

ColorBrackets[2]

Powers and fractions

ColorBrackets[(x^2)]

ColorBrackets[FullForm[(x^2)]]

ColorBrackets[Factor[(((1 + (a/2)))^6) - 1]] // 
      TraditionalForm

Expand[(((1 + (Sqrt[b])))^6)] // TraditionalForm

A TraditionalForm expression is usually treated as an atom

ColorBrackets[%]

ColorBrackets[(((1 + (Sqrt[b])))^6)]

The internal representation gives the full structure:

Internal representation

ColorBrackets[TreeForm[Expand[(((1 + (Sqrt[b])))^6)]]]

Brackets inside powers are always colored:

ColorBrackets[TraditionalForm[(((1 + (Sqrt[1 + b])))^6)]]

ColorBrackets[
      TraditionalForm[((((((1 + (Sqrt[1 + b])))^6) + 1))^2)]]

ColorBrackets[
      TraditionalForm[((((((1/(Sqrt[1 + b])))^5) + 1))^2)]]

TraditionalForm[(((1/(((1 + (((1 + b))^2)))^5) + 1))^2)]

ColorBrackets[%]

ColorBrackets[FullForm[(((1 + (Sqrt[1 + b])))^6)]]

ColorBrackets[TreeForm[(((1 + (Sqrt[1 + b])))^6)]]

ColorBrackets[
      TraditionalForm[((x^6) - 1)/(((1 + a))^6)]]

ColorBrackets[
      TraditionalForm[(((x - 1))^6)/(((1 + a))^6)]]

Factor[(x^6) - 1] // TraditionalForm

ColorBrackets[%]

ColorBrackets[FullForm[Factor[(x^6) - 1]]]

ColorBrackets[TreeForm[Factor[(x^6) - 1]]]

Nest[1/(1 + #) &, x, 5]

ColorBrackets[FullForm[%]]

ColorBrackets[TreeForm[%]]

\(Nest[1 + \@# &, x, 5]\)

ColorBrackets[FullForm[%]]

ColorBrackets[TreeForm[%%]]

\(Nest[1 + #\^ .5 &, x, 5]\)

ColorBrackets[StandardForm[%]]

ColorBrackets[TreeForm[%%]]

Integrals

Integrate[1/(x + (Sqrt[x^2 + a^2])), x]

ColorBrackets[%]

ColorBrackets[TreeForm[%]]

More complicated ones

(∫_a^b) (1/((x^2) + 1)) \[DifferentialD]x /. {Im[
            a] -> 0, Im[b] -> 0}

Simplify[%]

ColorBrackets[%]

ColorBrackets[(∫_a^b) 
                (1/((x^2) + 1)) \[DifferentialD]x /. {Im[a] -> 0, 
          Im[b] -> 0}]

ColorBrackets[
      FullForm[(∫_a^b) (1/((x^2) + 1)) \[DifferentialD]x /. {Im[
                a] -> 0, Im[b] -> 0}]]

It becomes useful if the output is very complicated, as is the case if complex values were allowed for   a and  b  in the previous integral:

ColorBrackets[
      TraditionalForm[(∫_a^b) 
                (1/((x^2) + 1)) \[DifferentialD]x]]

The structure of the integral expression itself is given by

ColorBrackets[
      FullForm[Hold[(∫_a^b) 
                  (1/(x + 1)) \[DifferentialD]x]]]

Sums

(∑_(j = 1)^n)(((1 + (a/2)))^j)

ColorBrackets[
      TraditionalForm[(∑_(j = 1)^n)(((1 + (a/2)))^j)]]

ColorBrackets[FullForm[Hold[(∑_(j = 1)^n) (x^j)]]]

ColorBrackets[FullForm[(∑_(j = 1)^n) (x^j)]]

Piecewise defined functions

f[x_] := If[x < 0, -1, If[x < 1, 1, 2]]

Plot[f[x], {x, -3, 3}]

ColorBrackets[FullForm[If[x < 0, -1, If[x < 1, 1, 2]]]]

Equations

(((x - 1))^6) == (((1 + a))^6)

ColorBrackets[%]

Solve for x

Solve[(((x - 1))^6) == (((1 + a))^6), x] // TableForm

x /. % // TableForm

ColorBrackets[Solve[(((x - 1))^6) == (((1 + a))^6), x]]

Matrices and composite structures

ColorBrackets[FullForm[(Sqrt[2 + 3  I])]]

(({{a, b}, {c, d}, {e, f}}))==(({{1, 2}, {3, 4}, {5, 6}}))

ColorBrackets[%] // TraditionalForm

MathML conversion

MathML form

MathMLForm[x/a+y]

MathMLForm[x/a + y]

b = (z/a) + y

MathMLForm[%]

\(Nest[apple + \@# &, x, 5]\)

MathMLForm[%]

Reversing by CopyAs

<math><mfrac><msup><mi>x</mi><mn>2</mn></msup><mi>z</mi></mfrac></math>

select and perform CopyAs ,  Paste to obtain

((x^2)/z)