How can I save a notebook as a $\LaTeX$ file, while preserving the syntax highlighting (such as input/output boxes)?
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".
And prepare the grounds for writing the TeX file.
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
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
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.