# Saving a notebook as PDF, preserving syntax highlighting

Currently, one may save notebooks as PDFs from the menu by Save As... and then selecting PDF (on a Mac, I imagine it is similar on other OSs). However, the resulting PDF does not have preserve the syntax highlighting of the code, even though things like plots are coloured. Printing to a PDF has the same effect (again, all this on a Mac).

Is there some way to save a notebook to PDF format so that syntax highlighting is preserved?

Here's an example of what I mean:

PDF:

on-screen (mathematica notebook, screenshot):

I feel that I am missing something obvious, but what?

-

The default style sheets set ShowSyntaxStyles -> False for the "Printout" environment.

You could change the notebook to use a style sheet that doesn't set this. Probably the easiest way is to copy the definition from Default.nb, and modify it:

Cell[StyleData[All, "Printout"],
ShowSyntaxStyles->True]

-
Should Default.nb be directly edited? I have two such files, in \Mathematica\8.0\SystemFiles\FrontEnd\StyleSheets and \Mathematica\8.0\Documentation\English\System\ReferencePages\Symbols which one is it? None has a Cell[StyleData[All, "Printout"], ShowSyntaxStyles->False] line... –  user4553 Nov 7 '12 at 8:43
This is not working for me. I tried (1) to run the code in the notebook I wanted to preserve syntax highlighting. (2) Go to Format->Edit Stylesheet..., and tried to run the code under the Default.nb (3) same as before but under Local definition of 'All'. None of these attempts worked. How do I make this work? –  QuantumDot Feb 1 at 19:05
@QuantumDot did you ever get this working? –  Mr.Wizard Mar 14 at 9:33
@Mr.Wizard No, I never got it to work. But, I am still eager to get it to work. I have v 9.0.1 –  QuantumDot Mar 15 at 3:42
@QuantumDot I suggest you post a new question, referencing this one, and describe exactly what you have tried and what the (lack of) result was, along with your system particulars. –  Mr.Wizard Mar 15 at 4:02
This is because Mathematica exports to PDF in the Printout screen environment. If you change this to the working environment, it'll keep the syntax highlighting, but will also make everything a bit bigger:
SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"]  You can also change this setting from the GUI using Format -> Option Inspector... If you want the effect to last only until you close the Front End, use SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"]