# How can I format Mathematica expressions for $\LaTeX$ without manual intervention? [closed]

I want to show my work to my audience mathematically. My audience is not interested in Mathematica but in the math itself, so I want $\LaTeX$ output. I tried TeXForm, but my trial made this question look too complicated. How can I print my work easily with beautiful typesetting on A4-sized paper?

AA = -a*g/(A*(2*g*h)^(1/2));
aa = (F1 + F2)^2/(2*a^2*g);
BB = 2/A;
Du = F1 + F2 - 1.2;
Dx = h - aa;
...
Px = AA*Dx - BB*Du // Simplify;

The video here shows many ways but I haven't yet found one good way. Anyway they may help someone.

Different ways

Way 1

Export["/tmp/test.tex", { AA, aa, BB, Du, Dx, Px}];

Way 2

{ AA, aa, BB, Du, Dx, Px} // TeXForm;

Perhaps related

1. Breaking long equations in $\LaTeX$ here.

2. This question but in the TeX-biased form here, more about formatting and related.

-
 Mathematica doesn't know the details of the page width, font size, font family, etc. of your latex document, so I don't think this is possible. Even that info probably wouldn't be sufficient, and it would most likely need the underlying box dimensions (which will depend on the kerning, spacing, fonts, etc.) from latex to even begin to attempt this. I think it might be simpler to ask the folks at TeX - LaTeX for a solution to automatically break the lines – rm -rf♦ Oct 14 '12 at 0:35 Yes, but your example above is not as simple as what you've shown in the pastie... I'm guessing all the aa, AA, Px, etc. variables get expanded before you convert to latex. – rm -rf♦ Oct 14 '12 at 0:39 Well, it's not really the same question IMO, because formatting this might be simple and straightforward. The real trouble arises when you substitute expressions into each of them like in your previous revision, causing it to overflow the page. Or are you not interested in that anymore? – rm -rf♦ Oct 14 '12 at 0:49 @rm-rf I cannot understand, I hope TeX -things will be now there and Mathematica -things will be here. – hhh Oct 14 '12 at 0:53

## closed as not a real question by Jens, m_goldberg, Yves Klett, Sjoerd C. de Vries, rm -rf♦Mar 22 at 23:04

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

One idea is to put everything in an align* environment.

SetAttributes[MassAlign, HoldAll];
SetAttributes[AlignElement, {HoldAll, Listable}];

AlignElement[elem_Symbol] := StringJoin[
ToString@TeXForm@SymbolName@Unevaluated@elem,
" &= ",
ToString@TeXForm@elem,
" \\\\\n"
];

MassAlign[list_] := StringJoin[
"\\begin{align*}\n",
AlignElement[list],
"\\end{align*}"
];

Here it is applied to your example.

AA = -a*g/(A*(2*g*h)^(1/2));
aa = (F1 + F2)^2/(2*a^2*g);
BB = 2/A;
Du = F1 + F2 - 1.2;
Dx = h - aa;
Px = AA*Dx - BB*Du // Simplify;

MassAlign[{AA, aa, BB, Du, Dx, Px}]
\begin{align*}
\text{AA} &= -\frac{a g}{\sqrt{2} A \sqrt{g h}} \\
\text{aa} &= \frac{(\text{F1}+\text{F2})^2}{2 a^2 g} \\
\text{BB} &= \frac{2}{A} \\
\text{Du} &= \text{F1}+\text{F2}-1.2 \\
\text{Dx} &= h-\frac{(\text{F1}+\text{F2})^2}{2 a^2 g} \\
\text{Px} &= -\frac{\frac{\sqrt{2} a g \left(h-\frac{(\text{F1}+\text{F2})^2}{2 a^2 g}\right)}{\sqrt{g h}}+4. (\text{F1}+\text{F2}-1.2)}{2 A} \\
\end{align*}

Be careful, this is a String so you should copy it as Plain Text in Mathematica. In $\LaTeX$ it looks like

\begin{align*} \text{AA} &= -\frac{a g}{\sqrt{2} A \sqrt{g h}} \\ \text{aa} &= \frac{(\text{F1}+\text{F2})^2}{2 a^2 g} \\ \text{BB} &= \frac{2}{A} \\ \text{Du} &= \text{F1}+\text{F2}-1.2 \\ \text{Dx} &= h-\frac{(\text{F1}+\text{F2})^2}{2 a^2 g} \\ \text{Px} &= -\frac{\frac{\sqrt{2} a g \left(h-\frac{(\text{F1}+\text{F2})^2}{2 a^2 g}\right)}{\sqrt{g h}}+4. (\text{F1}+\text{F2}-1.2)}{2 A} \\ \end{align*}

-