3
$\begingroup$

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

Mathematica graphics

And

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

Comes out worse

Mathematica graphics

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.

$\endgroup$
  • 1
    $\begingroup$ Like: DisplayForm@ RowBox[{#, "\[Cross]", FractionBox[#2, #3]}] &["Area[Rectangle]", ("Points in Circle"), \ ("Total Number of Points")]? $\endgroup$ – Kuba Nov 21 '16 at 15:10
  • $\begingroup$ @Kuba Yes! So I need to do some deconstructing of the formula and then reconstruct it with boxes. $\endgroup$ – Edmund Nov 21 '16 at 15:27
  • 1
    $\begingroup$ 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. $\endgroup$ – Alexei Boulbitch Nov 21 '16 at 15:31
  • $\begingroup$ @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. $\endgroup$ – Edmund Nov 21 '16 at 15:36
  • $\begingroup$ you may get by just with Row : Row[{"Area[Rectangle]", "\[Cross]", "Points in Circle"/"Total Number of Points"}] $\endgroup$ – george2079 Nov 21 '16 at 15:47
4
$\begingroup$

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:

enter image description here

The result is

enter image description here

After you click on the word "Area" you get this:

enter image description here

After you click on "Points in Circle" you get this:

enter image description here

If you click on the "Area" again you get this:

enter image description here

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!

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Edmund Nov 28 '16 at 17:44
  • $\begingroup$ 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. $\endgroup$ – Alexei Boulbitch Nov 28 '16 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.