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];
c = NotebookRead[nb];
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, "\\begin{equation}"];
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, "\\end{equation}"]];
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:
- http://facstaff.unca.edu/mcmcclur/LaTeXSaveMinimal.nb, which implements the minimal amount of code necessary to get an interesting example working.
- 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.