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?


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"],
  • 1
    $\begingroup$ 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... $\endgroup$ – user4553 Nov 7 '12 at 8:43
  • $\begingroup$ 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? $\endgroup$ – QuantumDot Feb 1 '13 at 19:05
  • $\begingroup$ @QuantumDot did you ever get this working? $\endgroup$ – Mr.Wizard Mar 14 '13 at 9:33
  • $\begingroup$ @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 $\endgroup$ – QuantumDot Mar 15 '13 at 3:42
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Mar 15 '13 at 4:02

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

  • $\begingroup$ Your screenshot didn't post correctly. $\endgroup$ – Mr.Wizard Sep 1 '14 at 11:23
  • $\begingroup$ couldn't figure out the first answer and didn't like the second answer, but this one worked perfectly! =) $\endgroup$ – Hanmyo Nov 3 '15 at 21:33
  • $\begingroup$ This answer is good however, it does not capture the errors style. Try run Sin[x,y] and print. $\endgroup$ – Algohi Dec 22 '15 at 20:06
  • $\begingroup$ @ShutaoTANG that is not what I meant. You have to run Sin[x,y] until you get error message TagSet::tagpos: "Tag x in Cos[x]^2 is too deep for an assigned rule to be found" you will notice that the color of the message is not preserve. Only if you set the PrintingStyleEnvironment to "Working" then you can get the color of the message in the pdf. $\endgroup$ – Algohi Dec 23 '15 at 2:37
  • $\begingroup$ How to use this method as a default across all notebooks? SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Printout", ShowSyntaxStyles -> True] doesn't seem to work. $\endgroup$ – Sterling Oct 28 '20 at 22:39

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”?

  • $\begingroup$ Thanks to both you and @Brett. Now, how do I decide which one to accept? $\endgroup$ – acl Jan 21 '12 at 20:14
  • 6
    $\begingroup$ @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. $\endgroup$ – Mike Honeychurch Jan 21 '12 at 22:43
  • $\begingroup$ @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!) $\endgroup$ – acl Jan 22 '12 at 17:29
  • $\begingroup$ I got this working by setting the PrintStyleEnvironment to "Working" but now the fonts are far too large. How do I make it smaller? $\endgroup$ – QuantumDot Aug 26 '14 at 15:03
  • $\begingroup$ @QuantumDot,You can have a try(@Alexey Popkov's method): Cell[StyleData[All, "Working"],Magnification -> 0.8] Or my answer $\endgroup$ – xyz 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".

  • $\begingroup$ 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. $\endgroup$ – Ghersic Dec 24 '15 at 8:57
  • 1
    $\begingroup$ 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. $\endgroup$ – George Hrabovsky Dec 27 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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