17

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.

VividD
  • 3,660
  • 4
  • 26
  • 42
Gabriel
  • 1,877
  • 12
  • 22
  • 1
    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. – gpap Sep 06 '13 at 15:10
  • @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? – Gabriel Sep 06 '13 at 15:19
  • 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}] – gpap Sep 06 '13 at 16:39
  • Related: http://stackoverflow.com/q/7974804/695132 (alternative answer within). – Szabolcs Jun 15 '15 at 09:33
  • Mathematica lets you do multi page export for TIFF. You could then use a separate program to convert TIFF to pdf. – ions me Sep 20 '21 at 01:15

2 Answers2

16

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}];
Table[CellPrint[{i, 
   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}}];
Do[
   Paste[report, i];
   NotebookWrite[report,Cell["", "PageBreak", PageBreakBelow -> True]];
, {i, myList}]
Export["myList.pdf", report];
NotebookClose[report];
Clear[report];
Timothy Wofford
  • 3,803
  • 2
  • 19
  • 24
  • 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! – Gabriel Sep 06 '13 at 15:14
  • 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! – Gabriel Sep 07 '13 at 05:09
  • 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!

    – Gabriel Sep 07 '13 at 05:44
  • 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.

    – Timothy Wofford Sep 07 '13 at 09:43
  • 3
    I tried this with 1000 images, 2 image per page and it crashes the frontend, any alternatives? – M.R. Mar 16 '15 at 18:18
3

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

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355