# 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 '13 at 19:05
@QuantumDot did you ever get this working? –  Mr.Wizard Mar 14 '13 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 '13 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 '13 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"]

-
Thanks to both you and @Brett. Now, how do I decide which one to accept? –  acl Jan 21 '12 at 20:14
@Brett's answer addresses the syntax styling specifically. IMO switching to a working environment so as to capture (solely) syntax styling in printouts is a wrong approach because, as Szabolcs notes you get different font sizes -- plus many other artifacts such as background colouring (perhaps), visible cell brackets, and so on that do not normally appear in print. If the syntax styling is all he needs then the stylesheet modification is the best approach. –  Mike Honeychurch Jan 21 '12 at 22:43
@Szabolcs I think Brett's answer is a bit more general (in that it may be used programmatically), even if yours is easier to use for the specific purpose I have in mind. But thanks to both (there is no way I could have worked this out myself!) –  acl Jan 22 '12 at 17:29
I got this working by setting the PrintStyleEnvironment to "Working" but now the fonts are far too large. How do I make it smaller? –  QuantumDot yesterday