Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


enter image description here

on-screen (mathematica notebook, screenshot):

enter image description here

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

share|improve this question
up vote 47 down vote accepted

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"],
share|improve this answer
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
up vote 24 down vote

The better solution that I have found so far shown as below:

  PrintingStyleEnvironment -> "Printout", ShowSyntaxStyles -> True]

enter image description here

Response to Algohi's comment:

This solution could also capture the errors style

enter image description here

share|improve this answer
Your screenshot didn't post correctly. – Mr.Wizard Sep 1 '14 at 11:23
@Mr.Wizard,OK,I have edited it.Thanks :)! – Shutao TANG Sep 1 '14 at 11:28
couldn't figure out the first answer and didn't like the second answer, but this one worked perfectly! =) – Hanmyo Nov 3 '15 at 21:33
@Hanmyo Yes, I am glad to know you like it. I have been using this code piece to save the Mathematica notebook as PDF file in my study. :) – Shutao TANG Nov 4 '15 at 2:38
This answer is good however, it does not capture the errors style. Try run Sin[x,y] and print. – Algohi Dec 22 '15 at 20:06

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"]

References: How to export graphics in “Working” style environment rather than “Printout”?

share|improve this answer
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 Aug 26 '14 at 15:03
@QuantumDot,You can have a try(@Alexey Popkov's method): Cell[StyleData[All, "Working"],Magnification -> 0.8] Or my answer – Shutao TANG Sep 1 '14 at 12:35

Here is what I got using V10.3:

enter image description here

As you can see, V10.3 maintains all of the formatting when converting to pdf. I checked, just to make sure, and the PrintingStyleEnvironment is set to "PrintOut".

share|improve this answer
How was this done? I don't get the same result when using File > Save As > PDF. It is still uncolored for me in 10.3. – Ghersic Dec 24 '15 at 8:57
If saving the notebook as a pdf is not working you can try another method. Select all cells you want to save. Then go to the file menu and choose Save Selected As. Then choose pdf as the output format. – George Hrabovsky Dec 27 '15 at 7:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.