# How to create formatted math formulas that contain formatted strings?

I would like to create some formulas with short sentences as the variables. A minimal example follows.

$$\text{Area[Rectangle]} \times \frac{\text{Points in Circle}}{\text{Total Number of Points}}$$

In Mathematica

"Area[Rectangle]" ( "Points in Circle")/("Total Number of Points")


returns And

Inactivate["Area[Rectangle]" ( "Points in Circle")/("Total Number of Points"), Times]


Comes out worse How do I go about creating a general function that I can pass in the formula of these string variables and get something like what is rendered above? Preferably without unprotecting any built-ins or loading 3rd-party packages.

• Like: DisplayForm@ RowBox[{#, "\[Cross]", FractionBox[#2, #3]}] &["Area[Rectangle]", ("Points in Circle"), \ ("Total Number of Points")]? – Kuba Nov 21 '16 at 15:10
• @Kuba Yes! So I need to do some deconstructing of the formula and then reconstruct it with boxes. – Edmund Nov 21 '16 at 15:27
• Do you need it as an output? Or it is OK writing it in the DisplayFormula cell? Where do you need to show it: article? presentation? working notebook? Depending on this the answers may differ. – Alexei Boulbitch Nov 21 '16 at 15:31
• @AlexeiBoulbitch I need it in a presentation and a notebook/CDF to display the formula. Current use would be in its own cell in the presentation and also in a grid where I will show the formula, show the numerical values in the same position (using NumberForm to pass the numbers in), and then I will calcuate the result. Same workflow in notebook/CDF. – Edmund Nov 21 '16 at 15:36
• you may get by just with Row : Row[{"Area[Rectangle]", "\[Cross]", "Points in Circle"/"Total Number of Points"}] – george2079 Nov 21 '16 at 15:47

I think that for the notebook/CDF applications the use of the cells, such as DisplayFormula or DisplayFormulaNumbered are best of all. I only would like to turn your attention to the point that these cells look awfully in the default notebook, but quite good if you use the JournalArticle StyleSheet. Alternatively you may create your own StyleSheet with the desired format of the formulae.

Another story is the presentation. Here you need to show the formulas large and, eventually, spectacular. Below I give two codes for functions helping to represent formulas in a presentation.

The function Ac dynamically accentuates the expression in question. The expression wrapped by the Ac changes its color from black to a desired one and in addition blinks few times. All this is upon clicking. The function Pl simply represents the expression with a desired size and color.

Ac[expr_, color_, size_] :=
DynamicModule[{c1 = 0},
EventHandler[
Dynamic[
If[c1 == 0,

Style[expr, Black, Plain, size, Italic] // ExpressionCell,

Dynamic@If[Clock[1, 0.7, 2] < .5,

Style[expr, Gray, Plain, size, Italic] // ExpressionCell,

Style[expr, color, Bold, size, Italic]] // ExpressionCell
]
],                                  \

{"MouseDown" :> (c1 = c1 /. {0 -> 1, 1 -> 0})}
]
];


and

Pl[expr_, color_, size_] :=
ExpressionCell[Style[expr, color, Plain, size, Italic]]


Now to make it comfortable I would first define other functions in which I determine the desired colors and sizes:

acc1[expr_] := Ac[expr, Blue, 28]
acc2[expr_] := Ac[expr, Red, 28]
pl[expr_] := Pl[expr, Black, 28]


Done. Now it is easy to represent any formula. Here is yours, for example

Row[{"Area[Rectangle]" // acc1, "\[Times]" // pl,
"\!$$\*FractionBox[\(Points\\\ in\\\ Circle$$, $$Total\\\ Number\\\ \ of\\\ Points$$]\)" // acc2}]


on your screen it will look as follows: The result is After you click on the word "Area" you get this: After you click on "Points in Circle" you get this: If you click on the "Area" again you get this: After you once more click on "Points..." you return to the initial black image.

There are few other ways of doing all this. The function Acalso provides some blinking. In the case of a dislike, one can do the accentuation, but without blinking, which is in a way easier.

I hope it helps. Have fun!

• This is a nice touch (+1). All I really need is the DisplayForm@RowBox with the held expression so I can deconstruct it and rebuild it to display it. – Edmund Nov 28 '16 at 17:44
• My experience shows that the box laguage is extremely powerful, but much too long. I only apply it, when there is no way around. For example, if I have a notebook containing a button opening another notebook with a formula. Then the formula is better to make with a box language. Or, otherwise, if I want to show an expression when the cursor is hovering over a reference to the formula. In this case also the box language is inevitable. – Alexei Boulbitch Nov 28 '16 at 20:24