I have a list, myList, of let's say 20 (different) objects

myList = Table["object", {20}]

I know would like to export every single element of myList to its own PDF page in a 20-paged PDF document; i.e., myList[[1]] comes on page 1, ..., myList[[20]] comes on page 20.

Is this possible in Mathematica? I have heard of the Export function, of course, but have not seen any way of combining 20 exports in one single document.

  • 1
    $\begingroup$ I'd do this in LaTeX: Export["myList.txt", myList // TeXForm] and then \input{mylist.txt} in a minimal latex document where you can ask latex to control page breaks etc. $\endgroup$
    – gpap
    Sep 6 '13 at 15:10
  • $\begingroup$ @gpap: Interesting option as well. Can you please elaborate what the options are in LaTeX? One would use the Verbatim or how is possible to change all "{" and "}" of myList to page breaks? $\endgroup$
    – Gabriel
    Sep 6 '13 at 15:19
  • $\begingroup$ TexForm does that. I would use longtable.sty or force bage breaks within latex but seeing as you know exactly where the page breaks need to be Table[Export["page" <> ToString@i <> ".txt", TeXForm@myList[[i]]], {i, Length@myList}] will export page1.txt, ..., page20.txt. Then you can add these on your tex document using \input{page1.txt}...\input{page20.txt}. Writing this 20 times may be a bit cumbersome but you can actually make mathematica produce the needed code StringJoin @@ Table["\\" <> "input{page" <> ToString@i <> ".txt}\n" <> "\\" <> "newpage", {i, Length@myList}] $\endgroup$
    – gpap
    Sep 6 '13 at 16:39
  • $\begingroup$ Related: stackoverflow.com/q/7974804/695132 (alternative answer within). $\endgroup$
    – Szabolcs
    Jun 15 '15 at 9:33
  • $\begingroup$ Mathematica lets you do multi page export for TIFF. You could then use a separate program to convert TIFF to pdf. $\endgroup$
    – Ion Sme
    Sep 20 at 1:15

Vitally has the same idea, but I thought it would be a hassle to do it manually like that, so let MMA insert the page breaks:

myList = Array["object", {20}];
   Cell["", "PageBreak", PageBreakBelow -> True]}], {i, myList}]

Edit: In response to the comment,

Here we create a new notebook and set the page headers to none. Then we Paste your objects and NotebookWrite page breaks. Finally, we Export the notebook and clean up.

myList = Array["object", {20}];
report = CreateDocument[Null, 
   PageHeaders -> {{None, None, None}, {None, None, None}}];
   Paste[report, i];
   NotebookWrite[report,Cell["", "PageBreak", PageBreakBelow -> True]];
, {i, myList}]
Export["myList.pdf", report];
  • $\begingroup$ Thanks both Vitaly and Timothy. The problem with this approach, is that when "Printing as PDF", the name of the document is at the top of each page, and I prefer a way without this. Also, would there be a "Export["nameOfDocument.pdf,%]" way of doing this? Thanks again! $\endgroup$
    – Gabriel
    Sep 6 '13 at 15:14
  • $\begingroup$ Thank you for the update, Timothy. One question, though: why the use of Paste? Why doesn't "NotebookWrite[ report, Cell[ i ]]" work? Thanks for all help! $\endgroup$
    – Gabriel
    Sep 7 '13 at 5:09
  • $\begingroup$ Why is it, when I change the Do[ ] part in your code above by Do[ ( NotebookWrite[report, { Cell[i], Cell[Paste[report, Column@myList[[i]]]], Cell["dd", "PageBreak", PageBreakBelow -> True] }]; ) , {i, Length[myList]}] that Mathematica output "Null" on every page? Thank you for all help! $\endgroup$
    – Gabriel
    Sep 7 '13 at 5:44
  • $\begingroup$ We get a clue by looking at the order of printed cells in the new notebook. The contents of Paste are printed first which means that Paste is executed first. Then Paste returns Null (i.e. the contents of Cell[Paste[...]] become Cell[Null]). Then the NotebookWrite writes the Cell[Null]. As for why Paste instead of NotebookWrite... I don't know, the documentation says Paste does some evaluation on its contents, but NotebookWrite says it is essentially the same as Paste. $\endgroup$ Sep 7 '13 at 9:43
  • 2
    $\begingroup$ I tried this with 1000 images, 2 image per page and it crashes the frontend, any alternatives? $\endgroup$
    – M.R.
    Mar 16 '15 at 18:18

For a few objects on the page a manual approach is

Top Menu >> Insert >> Page Break

And then to make sure (on Mac):

Top Menu >> File >> Print >> PDF >> Open PDF in Preview

enter image description here


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.