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

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.

share|improve this question
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. – partial81 Apr 8 '13 at 9:02
Depending on your intent (syntax highlighting too?) this is either closely related or a duplicate: – Mr.Wizard Apr 8 '13 at 10:50
@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. – mm.Jang Apr 8 '13 at 13:53
@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"). – Mr.Wizard Apr 8 '13 at 14:32
@Mr.Wizard Thank you very much!It works perfectly. – mm.Jang Apr 8 '13 at 14:44
up vote 2 down vote accepted

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

share|improve this answer

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

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


(Stylesheet for coloured syntax code printing.)

share|improve this answer

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.