# How to write aligned equations and export them HTML/TeX

This question is mainly about exporting to HTML/TeX. For those of you who do not know how to write aligned equations in Mathematica please see this answer. Lets assume that you modified your stylesheet as I mentioned in my answer. In my notebook this is the cell expression I have:

Cell[BoxData[
FormBox[GridBox[{
{
RowBox[{
RowBox[{"a", "+", "b", "+", "c"}], "\[AlignmentMarker]", "=", "d"}]},
{
RowBox[{"c", "\[AlignmentMarker]", "=",
RowBox[{"d", "-", "a", "-", "b"}]}]}


Now I want to export this expression to TeX, we can do this with:

cell = Cell[BoxData[
FormBox[GridBox[{
{
RowBox[{
RowBox[{"a", "+", "b", "+", "c"}], "\[AlignmentMarker]", "=", "d"}]},
{
RowBox[{"c", "\[AlignmentMarker]", "=",
RowBox[{"d", "-", "a", "-", "b"}]}]}
ConvertTeXBoxesToTeX[cell]


The output is:

\begin{array}{c}
a+b+c=d \\
c=d-a-b
\end{array}


The function ConvertTeXBoxesToTeX is undocumented but the documentation does show how to use it under examples. It seems that the function doesn't do anything with the \[AligmentMarker], but I'm sure we could make good use of them.

What we want to do is find any appearances of the \[AligmentMarker] and substitute them with "&". And if we did find some \[AligmentMaker]s then we use StringReplace to change "array" for "aligned". This is the output that I would like to end up with:

\begin{aligned}
a+b+c&=d \\
c&=d-a-b
\end{aligned}


Does anyone know how to create a wrapper function that does this? The main problem I'm having is replacing things in cell.

-

Using the definition of cell as in the question we can define:

CellToTeX[cell_] := Module[{str},
str = ConvertTeXBoxesToTeX@Replace[cell, {"\[AlignmentMarker]" -> "&"}, {-1}];
StringReplace[str, {"\\&" -> "&", "array" -> "aligned"}]
]


Then when we use it we obtain:

CellToTeX[cell]

\begin{aligned}{c}
a+b+c&=d \\
c&=d-a-b
\end{aligned}


I'm still not so sure of how to define a rule to replace {c} or possibly many {cccc} for an empty string.

-
+1 for working to answer your own question, which BTW is a good one. –  Mr.Wizard Jun 9 '12 at 7:17
@Mr.Wizard, thank you. Now, if I could just manage to do the last part to complete it. –  jmlopez Jun 9 '12 at 15:28
Regarding that (your last line) by chance is "{" ~~ ("c" ..) ~~ "}" -> "" all you're missing? –  Mr.Wizard Jun 9 '12 at 15:34
@Mr.Wizard, you just answered the question. Could you write it as an answer and maybe explain a little about how you came up with it? I saw those type of rules but I would have never thought about using a rule like that... –  jmlopez Jun 9 '12 at 15:40
You know it's funny- you're doing things with LaTeX I don't comprehend; it didn't occur to me that a simple replacement like this was holding you up. –  Mr.Wizard Jun 9 '12 at 15:41