# Exporting numbered equations to HTML/TeX using conversion rules

This question is a follow up from this one. It may also be considered a duplicate since I have already asked this question in stackoverflow almost a year ago. For a background on using numbered equations see my post from SO.

Since then I had not messed with the question till today. Consider the following:

ExportString[
Cell[
BoxData[
FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ", "x"}],
],
"NumberedEquation",
CellTags -> "eq:myEq"
]
, "HTML",
"FullDocument" -> False,
"ConversionRules" -> {
"NumberedEquation" -> {
"\$$\n", ConvertTeXBoxesToTeX[#] &, "\n\$$"
}
}
]


The output is:

$$<a id="eq_myEq"></a> f(x) = x$$


Notice how it was able to detect the cell tag using html. But if you were to export it using latex, it just doesn't. What we want is this output when using latex:

$$\label{eq:myEq} f(x) = x$$


The question is, how we can use the conversion rules to take advantange of the cell tag.

I've got my own package that I've used for a few years to generate LaTeX from Mathematica. All the labs on my Mathematica course page were produced with this package. Here's a handout on probability theory for Calc II students that was produced by the package. Unfortunately, it's not at all polished and really not usable by anyone but me. I can present the basic ideas here, however. I might potentially polish it up at some date.

After moving to the beginning of the notebook, we programmatically read through each cell of the notebook, export each individual cell to LaTeX, and write the result to an open stream. This looks something like so:

SelectionMove[nb, Before, Notebook];
While[c =!= {},
SelectionMove[nb, Next, Cell];
LaTeXExportTop[strm, c, nb]];


Now, c is a cell of the form Cell[data, type] and LaTeXExportTop that acts on cells depending on the type. For many cells, LaTeXExportTop is quite simple. For example, for a text cell, LaTeXExportTop looks like

LaTeXExportTop[strm_, Cell[data_, "Text", ___], nb_] := Module[{},
WriteString[strm, "\n"];
LaTeXExportInline[strm, data];
WriteString[strm, "\n\n"]];


Since, a top level cell may consist of many sub-cells, LaTeXExportInline is a second function that recursively acts on data until it reaches some primitive type, such as a string. We'll see an example of that in a moment.

To incorporate numering, we simply scan the CellTags. For example, here's how LaTeXExportTop acts on cells of type "Equation":

LaTeXExportTop[strm_, cc : Cell[_, "EquationNumbered", opts___],
nb_] := Module[
{equationTag, exportString, tex},
WriteString[strm, "\$$"]; equationTag = CellTags /. Cases[{opts}, _Rule]; If[equationTag =!= CellTags, WriteString[strm, "\\label{" <> equationTag <> "}"]]; WriteString[strm, "\n"]; exportString = ExportString[cc, "TeX"]; exportString = StringReplace[exportString, {"\\cup" -> "\\bigcup", "\\cap" -> "\\bigcap"}]; tex = StringSplit[exportString, {"\\begin{document}\n\n$", "$"}][[2]]; WriteString[strm, tex <> "\n"]; WriteString[strm, "\$$"]];


Now suppose that our Mathematica noteook has an automatic numbering object in it. Inside a cell, this will look like CounterBox[type,label]. Thus, we need a definition of the following type.

LaTeXExportInline[strm_, CounterBox[_, label_String]] :=
WriteString[strm, "\\ref{" <> label <> "}"];


I've uploaded two sample notebooks to my webspace:

1. http://facstaff.unca.edu/mcmcclur/LaTeXSaveMinimal.nb, which implements the minimal amount of code necessary to get an interesting example working.
2. http://facstaff.unca.edu/mcmcclur/MinimalLaTeXPaper.nb, which is a sample paper for use with the previous notebook.

If you download those two notebooks, open both of them and then execute the code in the first, you should generate a folder containing a LaTeX and other documents. The notebook and resulting output will hopefully looks something like so:

I should also mention that my more complete version depends heavily on my own style sheets to emulate LaTeX. One really needs styles indicating the beginning and ending of environments, for example.

• Very nice ideas you got here Mark. I'm now a step closer to creating my stylesheet. I'm taking this as an answer since I doubt anyone will find a way to properly use the Export function. May 28, 2012 at 16:11
• @jmlopez Great - glad to hear it. I guess that I did mean to mention that I have tried the ConversionRules approach and, while I have had some success, I don't think you can get the level of control that you can get with the technique presented here. May 28, 2012 at 17:02
• True, optimally I would love to do everything with ConversionRules, but this won't be possible. So I will have to use a combination of the conversion rules and your technique. Now I only have one more question to complete my stylesheet. Will be posting soon. Hey, on another note, could you write an answer to my post in StackOverflow? May 28, 2012 at 17:12