53
$\begingroup$

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:

enter image description here

on-screen (mathematica notebook, screenshot):

enter image description here

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

$\endgroup$

4 Answers 4

53
$\begingroup$

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]
$\endgroup$
5
  • 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
    Commented Nov 7, 2012 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
    Commented Feb 1, 2013 at 19:05
  • $\begingroup$ @QuantumDot did you ever get this working? $\endgroup$
    – Mr.Wizard
    Commented Mar 14, 2013 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
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 at 4:02
39
+100
$\begingroup$

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

SetOptions[
  SelectedNotebook[], 
  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

$\endgroup$
5
  • $\begingroup$ Your screenshot didn't post correctly. $\endgroup$
    – Mr.Wizard
    Commented Sep 1, 2014 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
    Commented Nov 3, 2015 at 21:33
  • $\begingroup$ This answer is good however, it does not capture the errors style. Try run Sin[x,y] and print. $\endgroup$ Commented Dec 22, 2015 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$ Commented Dec 23, 2015 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
    Commented Oct 28, 2020 at 22:39
26
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Thanks to both you and @Brett. Now, how do I decide which one to accept? $\endgroup$
    – acl
    Commented Jan 21, 2012 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$ Commented Jan 21, 2012 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
    Commented Jan 22, 2012 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
    Commented Aug 26, 2014 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
    Commented Sep 1, 2014 at 12:35
3
$\begingroup$

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

$\endgroup$
2
  • $\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
    Commented Dec 24, 2015 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$ Commented Dec 27, 2015 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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