Possible Duplicate:
Saving a notebook as PDF, preserving syntax highlighting

I would like to export my notebook code in a colored PDF form. What I do is select the brace in the right side of the lines I'd like to export and then from "File->Save Selection As" I export it as pdf. The thing is that there aren't any colors.

Is there a way to export in PDF while adopting all colors?

EDIT: I also tried "Print Selection" instead of "Save selection As" but I faced to problems: The is a lot of empty space, which means that I will not be able to include it in my tex code and secondly there aren't any colors in my input code, while the output graphic really adopts color. My output is

Mathematica graphics


marked as duplicate by acl, image_doctor, Ajasja, rm -rf Nov 30 '12 at 21:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Your question is not entirely clear (to me) but are you wanting the syntax colouring to appear in your input code in the PDF? If so this is controlled via stylesheeting which by default has this switched off for printing. If you confirm that is what you are wanting to achieve I'll post a solution. ...but it may not be until tomorrow. $\endgroup$ – Mike Honeychurch Nov 30 '12 at 8:24
  • $\begingroup$ @MikeHoneychurch: This is exactly what I am trying to do!!! I try to find something by myself;you just gave a path to walk! But any help from an expert is always vital!!! Thank you! $\endgroup$ – Thanos Nov 30 '12 at 8:26
  • $\begingroup$ ok I just posted the explanation and answer. $\endgroup$ – Mike Honeychurch Nov 30 '12 at 8:49

What is happening here is that when you print (or make a PDF) the default printing style has the sytnax colouring switched off. You can see this by editing your stylesheet then clicking on "default.nb" and then when that notebook opens clicking on "Core.nb" and have a look at the printout style:

enter image description here

So when you highlight some input and save as PDF, or Export a notebook to PDF etc. you see stuff like this:

enter image description here

So the way to fix this is via the stylesheet is to go to Format>Edit Stylesheet and add this style cell to your notebooks stylesheet:

Cell[StyleData[All, "Printout"],

To fix this "permanently" you probably want to install the style stylesheet so you can use the installed stylesheet regularly rather than have to do this every time. Here is what it the new PDF looks like:

enter image description here

The way to fix this via the option inspector is to switch to printout style environment and then change the option setting.


enter image description here


enter image description here

  • $\begingroup$ Yeah!!! It is working like a charm!!!Thank you so much!!! $\endgroup$ – Thanos Dec 2 '12 at 11:47

(not an answer, can't put this in the comment)

Which version are you using? using V9 I have no problem seeing color in the PDF generated file. Here. May be your print env. is messed up? Mine is set to printout. Window 7

enter image description here

  • $\begingroup$ How can I check my print environment? I also tried "Print Selection As" instead of "Save selection As" but I faced to problems(Check my edited answer for the output): The is a lot of empty space, which means that I will not be able to include it in my tex code and secondly there aren't any colors in my input code, while the output graphic really adopts color. $\endgroup$ – Thanos Nov 30 '12 at 7:49
  • $\begingroup$ I am using V9 in Vista $\endgroup$ – Thanos Nov 30 '12 at 7:52
  • $\begingroup$ I checked my print env and mine is set to "printout" as well... $\endgroup$ – Thanos Nov 30 '12 at 7:53
  • $\begingroup$ This is unusual behaviour unless the default printout style has been changed in V9. $\endgroup$ – Mike Honeychurch Nov 30 '12 at 8:51

Your saved PDF supports color. It's just that the FrontEnd's code coloring isn't encoded into the cell expression from which the PDF is made. It's just something the FrontEnd's code editor adds when it displays the code on your screen.

Edit: This is misleading because, as Mike Honeychurch's answer states, there is an option for the printing environment that will preserve code coloring.

  • $\begingroup$ Thank you very much for your answer! But I checked Nasser M. Abbasi's answer and his PDF output is colored... $\endgroup$ – Thanos Nov 30 '12 at 7:55

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