# Saving a notebook as a $\LaTeX$ file, with syntax highlighting preserved

How can I save a notebook as a $\LaTeX$ file, while preserving the syntax highlighting (such as input/output boxes)?

-
What about TexForm? Link Cant comment much on syntax highlighting. –  drN Sep 23 '12 at 18:02
@drN thanks, but TexForm simply transcribes the mathematica expression in latex. I would like to retain the notebook "look" according to the selected style. One possibility is to export as pdf and then use pdfinsert in Latex as a figure, but this is cumbersome. –  victor Sep 24 '12 at 4:07
Related question: Saving to PDF preserving syntax highlighting (I know this doesn't answer this question.) –  Szabolcs Dec 17 '12 at 16:49

Personally I'm always disappointed by the quality of the conversion done when one simply saves a notebook as a TeX file--it never looks like what I want or expected. But perhaps this isn't too surprising, as the two file types are very different, and therefore many choices need to be made about the conversion.

I think a good general approach for solving questions of this kind is to write some Mathematica code to perform the conversion for you. Code in one notebook can open and read the contents of another notebook, manipulate it as you require, and spit it out as $\LaTeX$ file.

Here is some code I've written for my own purposes. It doesn't directly solve your problem but illustrates the general principle. First, let's open the notebook we want to convert "input.nb".

nb = NotebookOpen[ToFileName[NotebookDirectory[], "input.nb"], Visible -> True];


And prepare the grounds for writing the TeX file.

Check[DeleteFile[FileNameJoin[{NotebookDirectory[], "output.tex"}]], Null];
s = OpenWrite[FileNameJoin[{NotebookDirectory[], "output.tex"}]];


Now for my application, I was not very interested in the input cells. I wanted to extract the Title, the Section and Subsection headings, any Text cells, and the Output cells.

The output cells are likely to need the most processing, so I defined the following function to deal with them. If the cell contains a graphics object, it exports it as a PDF (with some silly name generated using the Hash function) and prepares a string to put into the TeX file to include the pdf graphics in the final document. Otherwise it assumes it is an equation, and applies TeXForm to it.

 OutputCell[cell_] :=
If[! FreeQ[cell, GraphicsBox],
Export[IntegerString[Hash[cell], 36, 5] <> ".pdf", cell];
"\\begin{figure}[!ht]\n\\begin{center}\n\\includegraphics{" <>
IntegerString[Hash[cell], 36, 5] <> ".pdf" <>
"}\n\\end{center}\n\\end{figure}\n",
"\$$\n" <> ToString@TeXForm@DisplayForm@cell <> "\$$\n"];


Now we are ready to actually iterate through the cells in the input notebook and write to the file stream we opened earlier. Here I specify my own, very short, preamble for the TeX file and I apply some rules to the contents of Text cells to replace curly quotes that for some reason don't appear properly in the TeX.

If you wanted to use this to process Input cells also, you'd simply add a case to the Switch statement and to the CellStyle option of Cells.

WriteString[s,
"\\documentclass[10pt]{article}  \n\\usepackage{fullpage} \n\
\\usepackage{graphicx} \n\\usepackage{amsmath} \n\
\\usepackage{amssymb} \n\\begin{document}\n",
Sequence @@ Riffle[Switch[#[[2]],
"Text",
StringReplace[#[[1]], {"\[CloseCurlyDoubleQuote]" -> "\"",
"\[OpenCurlyDoubleQuote]" -> "\"",
"\[CloseCurlyQuote]" -> "'", "\[OpenCurlyQuote]" -> "'"}] <>
"\n",
"Title", "\\title{" <> #[[1]] <> "}\n\\maketitle",
"Section", "\\section{" <> #[[1]] <> "}\n",
"Subsection", "\\subsection{" <> #[[1]] <> "}\n",
"LaTeX", #[[1]] <> "\n",
"Output", OutputCell[#]] & /@ (NotebookRead /@
Cells[nb,
CellStyle -> {"Title", "Section", "Subsection", "Text",
"LaTeX", "Output"}]), "\n"], "\n\\end{document}"];

Close[s];


At the end we close the file stream. You'll notice a reference to a cell style "LaTeX", this is simply a style I've got in one of my custom stylesheets, the intention being to allow me to write $\LaTeX$ code in a notebook and just have it included in the TeX file verbatim.

This code reduces your problem to implementing the syntax highlighter in Mathematica, or finding a $\LaTeX$ package to do it for you (which you'd then need to mention in the preamble). But it also makes possible far more general manipulations that, I imagine, could be fairly useful.

-