# Inserting aligned equations in graphics

I'd like to produce annotated graphics (which may eventually end up in a Dynamic or Manipulate environment) that include aligned sets of equations. In principle, I could do something like:

Graphics[{Text[
StringJoin["\!$$\*SubscriptBox[\(\[Alpha]$$, $$0$$]\)=",
ToString[
NumberForm[(Mod[3 Pi/2 + \[Beta] - \[Theta], 2 Pi] - Pi)/Degree,
4]], "\[Degree]"], {xt, yt}],
Text[StringJoin["\[Beta]=", ToString[NumberForm[\[Beta]/Degree, 4]],
"\[Degree]"],
Offset[{0, -18}, {xt,yt}]]}]


with \beta , \theta, r, defined variables, say, and xt, yt the coordinates where the text will appear.

The above code does that, but here is the issue: I would like to have expressions like this aligned nicely, using multiple alignment marks. In this example, one would probably want the "=" signs aligned, as well as the decimal points. Of course, more complex expressions are possible, both on the left-hand and the right-hand sides. What is the best structured way to achieve something like this? Given the somewhat vague question I am asking, I'm not necessarily expecting a complete piece of code for this, just a rational and efficient path to achieve what I want. If I can I'd like to avoid having to spend hours crafting the code to insert three aligned expressions into some graphics (when I could do something like this in LaTeX in 3 minutes...).

Note that any of the expressions appearing may be evaluated before being displayed (as in those ToString bits in my example), so what exactly appears in these expressions is not known beforehand.

Note that I did see a trick somewhere with a person converting LaTeX input into a Mathematica expression, and using that. This helps avoiding the nightmares of Mma's typesetting language, but is of very limited use if I want to format expressions that have been calculated within Mathematica.

Second remark: Once I have my nicely formatted set of expressions, I will need to find a way to have it appear within a graphics, which I don't know how to do, either.

• Have you seen MaTeX? It allows you to insert $\TeX$ code directly, so you don't need to bother with formatting in MMA – Jason B. Aug 3 '16 at 22:16
• @JasonB: Very interesting package, but it can only deal with expressions that fit within \$...\$ in LaTeX, so we can't do fancy arrays. I also would like to retain the flexibility to have dynamic expressions in there which is a no-go with this kind of an approach. Basically, I really need to make this work with Mathematica's built-in capabilities. – Pirx Aug 4 '16 at 20:46
• Have you seen this question? – Jason B. Aug 4 '16 at 21:15
• @JasonB: Yes, that's the "trick" I was referring to above. Very neat, but I don't think this will deal with dynamic expressions. – Pirx Aug 4 '16 at 21:50
• @Pirx why don't you think so? Have you tried? – Kuba Aug 5 '16 at 7:01

You should be able to align things with Column and using its second argument, e.g.:

Column[ {...} , "=" ]


Then in graphics you can you Inset to place that Column expression:

Graphics[
Inset[
Column[{
HoldForm[xxxxxx = 1],
HoldForm[y = 20000]
}, "="],
{0, 0}]]


In the second more complicated case (from the comment you made below), I would switch to a grid.

eqns = { HoldForm[xxxxxx = 1.2345], HoldForm[y = 20000.02], HoldForm[y=200.02] }


Then I would stringify those equations and split them at the equal sign:

eqns = Map[StringSplit[ToString[#], "=" -> "="] &, eqns]


And then this:

Graphics[Inset[Grid[eqns, Alignment -> "."], {0, 0}]]


If it bothers you that ToString turns 20000.02 into 20000. you can also use NumberForm using something like this:

eqns = Map[StringSplit[ToString[NumberForm[#, 10]], "=" -> "="] &, eqns]


• Hmm, we're getting closer. I looked at the documentation of Column, which sounds like I can have multiple alignment items, so I tried Graphics[Inset[ Column[{HoldForm[xxxxxx = 1.2345], HoldForm[y = 20000.02], HoldForm[y = 200.02]}, {"=", "."}], {0, 0}]], but this still only aligns at the "=" sign, and not also at the decimal points. Bummer. – Pirx Aug 4 '16 at 21:48

If you are that comfortable with Latex, why not to stay with it? Latex is a nice tool. The question is only how flexible are you with this or that software.

Below I give few ideas for answering your question with Mma as I prefer to do such tasks, more manually, but fast. It also takes few minutes. There are, however, several other approaches, more programmatic, and probably other users will communicate them.

1. Have a look at this:

Manipulate[
Column[{
F == 1/2 αη^2 + 1/4 βη^4 +
Row[{Spacer[k],
\!$$\*OverscriptBox[\(X$$, $$_$$]\) ==
HoldForm[1/n]*Defer@Sum[Subscript[X, i], {i, 1, n}] //
}], {k, 1, 100}]


yielding this

by moving the slider you choose the positioning of the second line with respect to the first one, and insert the obtained k value into the code. Then remove the Manipulate envelope.

1. Another possibility, a bit more formatting is below:

Manipulate[
Column[{
Style["F=\!$$\*FractionBox[\(1$$, $$2$$]\)\!$$\*SuperscriptBox[\(\ αη$$, $$2$$]\)+\!$$\*FractionBox[\(1$$, \
$$4$$]\)\!$$\*SuperscriptBox[\(βη$$, \
$$4$$]\)+\!$$\*FractionBox[\(1$$, $$6$$]\)\!$$\*SuperscriptBox[\(\ γη$$, $$6$$]\)", 16, Italic],
Row[{Spacer[k],
Style["<X>=\!$$\*FractionBox[\(1$$, \
$$n$$]\)\!$$\*SubsuperscriptBox[\(Σ$$, $$i = 1$$, \
$$n$$]\)\!$$\*SubscriptBox[\(X$$, $$i$$]\)", 16, Italic]}]
}], {k, 1, 100}]


It might have a better look. The recipe is the same. You may finally place it into, say, Epilog statement for the graphics. Or wrap it with Graphics/Text statements, as you like.

Have fun!

• But the displays from #1 and #2 do not have the equations aligned on the equal sign. The OP wanted such alignment. – murray Aug 4 '16 at 15:05
• @murray, yes, that's one of the issues. Actually, I want several alignment points. For example, I could have expressions representing equalities, which I want aligned at the equal sign. On the right-hand side of those, there may be real numbers, which I want aligned at the decimal dots, etc., etc. I can construct things like that fairly easily in LaTeX. However, Mathematica's typesetting language is a nightmare, plain and simple, and my guess is that very, very few people on this planet use it in its raw form to typeset anything whatsoever. – Pirx Aug 4 '16 at 20:34
• @Alexei: By the way, neither of your examples above seem to work for me, at least not by copying and pasting into a notebook. First one doesn't work at all and gives a syntax error, the second one gives me raw typesetting code, not any rendered expressions. – Pirx Aug 4 '16 at 20:54
• @Pirx - did the edit I just made help? If not, what version are you using, it seems to work for me. – Jason B. Aug 4 '16 at 21:14
• @JasonB: Yes, now it works, in MMA 10.4. Still, doesn't fill my bill... – Pirx Aug 4 '16 at 22:06