14
$\begingroup$

I'm exporting a Grid[] to a PDF. It's working, but it's real ugly. Here is an example of what it looks like:

enter image description here

Possibly the first problem is the reason I posted a picture of (rather than sharing) the PDF: as you can see, it's a single page, with about 3x6 plots, yet it's somehow 16MB. Since normal PNG files of reasonable size are ~50kb or so, I'm guessing that the file is so huge because it's doing something like writing each data point to the file or something, so has way more information than is useful or even possible to really see.

So, my first question is, how can I do this so the file isn't enormous?

My second question is about the formatting. Right now, it's obviously very ugly: The titles and axes labels of the plots are being cut off, the tic mark labeling is crammed together, etc. It's hard to give an example of the code because it's quite big, but here's a contrived example that will show what I mean:

Grid[Table[
  Table[Plot[a*x^b, {x, 0, 5}, 
    PlotLabel -> 
     "What a long and interesting and certainly necessary title for a \
plot of the function " <> ToString[ax*b]], {a, 2, 3}], {b, 4, 5}]]

So, how can I make it wrap around automatically, and resize so it's not taking up a ton of space?

Any other tips for this would be welcome as well! I'm just doing it naively, the way it was easiest to.

$\endgroup$
10
  • 1
    $\begingroup$ From your screenshot it looks like you are using Acrobat, which does have an "Optimize PDF" functionality, and that can cut down the size quite a lot. Have you tried this? $\endgroup$
    – Igor Rivin
    Oct 2, 2014 at 16:50
  • $\begingroup$ FWIW, Mathematica is notorious for producing gigantic PDFs. $\endgroup$
    – Igor Rivin
    Oct 2, 2014 at 16:51
  • 1
    $\begingroup$ Also, this does not really answer your question, but inserting explicit \n into the PlotLabel text does introduce a newline. Not automatic, but does work. $\endgroup$
    – Igor Rivin
    Oct 2, 2014 at 17:03
  • $\begingroup$ @IgorRivin, thanks, but I don't really want to have to do that each time I produce a PDF. One option (I think) would be to make my program export all my plots as .png's, then Import those and put them into the PDF, that might make it smaller. But it'd be really annoying even if it does work. I know I can use \n but it'd be nicer if it was automatic. Thank you! $\endgroup$ Oct 2, 2014 at 18:16
  • 1
    $\begingroup$ @AlexeyPopkov, I actually don't need to use PDF I guess -- I just defaulted to that. Now I'm trying Rasterize[] and Export[] to .png. The files it produces aren't massive, but it has its own problems. I'm just trying to make a large presentation for a projector. $\endgroup$ Oct 7, 2014 at 15:02

3 Answers 3

9
$\begingroup$

Part a response, Because you do not show additional information and do not describe the problem with the code, I show one approach.

First we construct some helpers :

ClearAll["Global`*"]
SetDirectory@NotebookDirectory[];
siz1 = 250;
siz2 = 500;
tex1 = "What a long and interesting and certainly necessary title for \
a panel of functions.";
title = Panel[Style[tex1, Black, 16, LineIndent -> 0], 
   ImageSize -> siz2, Background -> White, Alignment -> Left];
tex2 = "This is another text without meaning and is only here to \
prove place.";
caption = 
  Panel[Style[tex2, Black, 10, LineIndent -> 0], ImageSize -> siz1, 
   Background -> White, Alignment -> Center];

p1 = Plot[Sin[x], {x, -\[Pi], \[Pi]}, PlotLabel -> Sin, 
   FrameLabel -> {{None, None}, {caption, None}}, Frame -> True, 
   ImageSize -> siz1, PlotStyle -> Red];
p2 = Plot[Cos[x], {x, -\[Pi], \[Pi]}, PlotLabel -> Cos, 
   FrameLabel -> {{None, None}, {caption, None}}, Frame -> True, 
   ImageSize -> siz1, PlotStyle -> Blue];
p3 = Plot[Tan[x], {x, -\[Pi], \[Pi]}, PlotLabel -> Tan, 
   FrameLabel -> {{None, None}, {caption, None}}, Frame -> True, 
   ImageSize -> siz1, PlotStyle -> Darker[Green]];
p4 = Show[p1, p2, p3, PlotLabel -> "Sin, Cos, Tan", Frame -> True];

The strategy to combine plots with text I got from this QnA (thanks @Murta and @kguler)

dg1 = Deploy@
  Grid[{{title, SpanFromLeft}, {p1, p2}, {p3, p4}}, Dividers -> Gray, 
   Spacings -> {1, 1}]

To combine multiple plots try;

ex1 = Grid[{{dg1, dg1}, {dg1, dg1}, {dg1, dg1}}, Spacings -> {1, 2}, 
  Frame -> All]

enter image description here

enter image description here

For export and fine tuning:

Export["myFig-.pdf", ex1]
Export["myFig-IR.pdf", ex1, ImageResolution -> 256]
Export["myFig-Ras.pdf", ex1, "AllowRasterization" -> True, 
 ImageSize -> Full, ImageResolution -> 512]

The file size on 10.0 for Mac OS X x86 (64-bit) (September 10, 2014) is about 158 KB.

$\endgroup$
1
  • $\begingroup$ So cool and so interesting! $\endgroup$ Oct 5, 2014 at 18:38
4
$\begingroup$

The file is gigantic because Mathematica is putting vector objects in the file in an inefficient way, meaning a LOT of vector objects, fonts, etc, as you suspect.

The best procedure is to generate the page as an image, save the image then add the image to the PDF. This loses the capability to make the PDF page scalable, but you probably don't care about that.

You can generate an image by using Rasterize or CopyToClipboard[Rasterize[...]]. Taking a screen shot has the same effect (assuming the whole image fits on your screen).

If scalability is important to you (for example, you want a reader to be able to magnify one of the plots with no loss of resolution), then one possible trick is to use LaTeX. Generate the LaTaX code for the output cell and then manually massage the LaTeX code to eliminate any obvious inefficiencies, then render it as a PDF using the LaTeX publishing system. To make the LaTeX closely match the Mathematica output will probably take a significant amount of editing and require knowledge of LaTeX syntax, so you don't want to get into this unless scalability is pretty important.

* Worst Case Scenario *

If all else fails you can do a screen shot (Alt-Print Screen on Windows). Note that the Window menu in Mathematica has a "Magnification" choice, so you can blow up your image to any size. If the image does not fit on the screen, take images piecemeal, then stitch them together in an image editing program like Photoshop, Gimp, Inkscape, etc.

$\endgroup$
4
  • $\begingroup$ I believe my suggestion in the comment to Optimize PDF in acrobat is much more time efficient. $\endgroup$
    – Igor Rivin
    Oct 2, 2014 at 23:59
  • $\begingroup$ Hi, thank you for the answer! I'm doing this method now, but I'm running into some trouble. First, when doing Rasterize, it was cramming the numbers on the tickmarks of my plots together, so they were overlapping and looking messy. I think I managed to make this better by fiddling with the Rasterize options (currently using RasterSize -> 1000, ImageSize -> 2000), but then when I do Export[] to a .png, it makes the .png have really bad resolution. What can I do? $\endgroup$ Oct 3, 2014 at 20:20
  • $\begingroup$ All I really want to do is have a bitmap picture of what Grid[] pops out, so why is Rasterize messing around with the fonts and sizes and formatting? Can't it just take the output of Grid[] and essentially 'flatten' it? Thank you! $\endgroup$ Oct 3, 2014 at 20:21
  • $\begingroup$ @FdotFloss As posted in my answer you can always fall back to screenshots. I have noticed that in some cases Rasterize does not always give exact reproduction of screen renderings. $\endgroup$ Oct 4, 2014 at 2:09
2
$\begingroup$

You have not specified why do you actually need to have your graphics in the PDF format and why (actually) the 16 Mb size is a problem for you. I'll only comment about the appearance.

From your description I can conclude that you have generated a nice-looking graphics inside of the Mathematica's FrontEnd but when you Export it as PDF your graphics does not look as it was inside of the FrontEnd: it is damaged. The most probable reason for this is that the FrontEnd by default displays everything in the "Working" style environment while Export to PDF uses by default the "Printout" style environment. So the first thing to try in such cases is to Export your graph in the "Working" style environment. One way to do this is described in this answer:

SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"]

After evaluating the above, Export your graph again. For reverting to the default behavior, just evaluate:

SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Printout"]

It is hard to say more without an example code and with such a little information on what your are actually trying to do.

$\endgroup$

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.