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.

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. – jmlopez – 2012-05-28T16:11:22.820

@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. – Mark McClure – 2012-05-28T17:02:01.697True, optimally I would love to do everything with

– jmlopez – 2012-05-28T17:12:10.420`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?