2
$\begingroup$

I assign a few LinePlots to plotA, plotB, plotC and present them in a Row, using

a = Row @ {plotA, plotB,plotC}

though with some tweaks to their appearance and alignment;

a = Row @ {
  Show[plotA, ImageSize -> 350], 
  Show[plotB, ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}],
  plotC
}

This renders beautifully in the front-end:

enter image description here

I now wish to export this arrangement as a single .pdf file. Sadly,

Export["a.pdf", a]

produces a pdf which has wrapped the images:

enter image description here

I've tried

SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"]
Export["a.pdf", a, ImageFormattingWidth -> \[Infinity]]

but this does not change the wrapping.

One work-around is to turn off the line numbers ("In[n]"), via

SetOptions[$FrontEnd, ShowCellLabel -> False];

then select my row in the front-end, and use File > Save Selection As...

but this is a bit annoying, and I'm looking for an entirely programmatic method so I can automate this without goofing up my notebook.

Any ideas?

$\endgroup$
4
$\begingroup$

Based on the answer to this question, you can wrap the plots in a Cell and specify the PageWidth there:

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

ImportString[
 ExportString[
  Cell[
   BoxData@
    ToBoxes@Row@
      Table[ListPlot[RandomReal[{0, 1}, {5, 3}], ImageSize -> 300], 3],
   PageWidth -> Infinity
   ],
  "PDF"
  ],
 "PDF"
 ]

enter image description here

The ImportString/ExportString combo is just so you can quickly verify the result within Mathematica. To actually create a PDF file, use Export instead of ExportString:

 Export[
  "a.pdf",
  Cell[
   BoxData@
    ToBoxes@Row@
      Table[ListPlot[RandomReal[{0, 1}, {5, 3}], ImageSize -> 300], 3],
   PageWidth -> Infinity
   ],
  "PDF"
  ]
$\endgroup$
5
  • $\begingroup$ Ahh certainly the PageWidth was respected, but this introduced some other problems (I've updated my question with the output) $\endgroup$ – Anti Earth Apr 16 '20 at 16:01
  • $\begingroup$ @AntiEarth Did you check how the exported PDF looks like in an external viewer? There's a known issue with Mathematica and clipped regions in PDFs (see e.g. here) $\endgroup$ – Lukas Lang Apr 16 '20 at 16:18
  • $\begingroup$ I couldn't write this string to a file successfully; using WriteString gives me a blank pdf (of the correct one-row dimensions though, I notice) and Export gave me an invalid file. Is there something wrong with WriteString["test.pdf", ExportString[ ..., "PDF"]]? $\endgroup$ – Anti Earth Apr 16 '20 at 16:36
  • $\begingroup$ @AntiEarth Sorry, I should have specified that in the post itself - simply replace ExportString[...,"PDF"] with Export["file.pdf",...,"PDF"] to actually export the plots (see also the updated answer) $\endgroup$ – Lukas Lang Apr 16 '20 at 16:54
  • $\begingroup$ Fantastic, you've done it :) Thanks very much! $\endgroup$ – Anti Earth Apr 17 '20 at 14:46
1
$\begingroup$

You can also use LineBreakWithin -> False, as in this answer:

ImportString@ExportString[
    Style[Row[Table[Plot[Sin[x], {x, 0, 10}, ImageSize->300], 3]], LineBreakWithin -> False],
"PDF"
]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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