Well, I have a notebook with the built-in stylesheet Preprint, which I'd like to convert it to pdf. Everything works well except for the code cells(I use the shortcut key alt+8 to create cells of code style) with gray background. the code cell in the notebook looks like this: enter image description here

But, after saving my entire notebook to pdf, the gray background lost! This is a screen-shot of the same code cell above from my pdf viewer. enter image description here

So, is there anyway to make the "code cell" in the exported pdf looks exactly the same as it is in the notebook? I mean, It should have gray background. Or am I missing something?

Thank you.

  • $\begingroup$ If I redo the steps you describe, I get in the pdf this gray background. Perhaps this is a version problem. Which Mathematica version do you use? I tried it with 8.0.1. $\endgroup$
    – partial81
    Apr 8, 2013 at 9:02
  • $\begingroup$ Depending on your intent (syntax highlighting too?) this is either closely related or a duplicate: mathematica.stackexchange.com/q/451/121 $\endgroup$
    – Mr.Wizard
    Apr 8, 2013 at 10:50
  • $\begingroup$ @Mr.Wizard I have read this post before and the first anwser to it(as you mentioned in this question) do solve my problem,but the pdf rendered in "working environment" looks a bit strange (too big!),so I would like to look for a better solution. $\endgroup$
    – mmjang
    Apr 8, 2013 at 13:53
  • $\begingroup$ @mm.Jang You can customize the appearance by using a private style sheet definition as Chris Degnen describes. The "Printout" environment has a setting Magnification->0.8 by default which makes things smaller. For example, go to Format > Edit Stylesheet... then paste in Cell[StyleData[All, "Working"], Magnification -> 0.8] and try saving a PDF again (printing environment set to "Working"). $\endgroup$
    – Mr.Wizard
    Apr 8, 2013 at 14:32
  • $\begingroup$ @Mr.Wizard Thank you very much!It works perfectly. $\endgroup$
    – mmjang
    Apr 8, 2013 at 14:44

2 Answers 2


Notebooks saved to PDF are rendered with the Printing Environment that is selected. The Code style does not have a gray background in the "Printout" environment. You can change the Printing Environment to "Working" in the menu:

  • File > Print Settings > Printing Environment

Combining this method with Chris Degnen's you can further customize the output using private style sheets. For example, to make objects print (export to PDF) at a smaller size, as they do by default in the "Printout" environment, you can:

  1. Open Format > Edit Stylesheet...

  2. Paste: Cell[StyleData[All, "Working"], Magnification -> 0.8]

  3. Close the style sheet


You should probably be able to get it working with this addition to your stylesheet:-

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

Ref: http://forums.wolfram.com/mathgroup/archive/2011/May/msg00323.html

(Stylesheet for coloured syntax code printing.)


Your Answer

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

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