It's the end of the semester and I have a prof who wants me to turn in a printout of the nb files ive worked on. I have 33 separate files, and I was wondering if there was any easy way I could combine all of these files in batch to another nb or a document format to print out (pdf/rtf/doc/html anything really). Nothing fancy needs to happen here, basically i just need to make this into a printable file and preserve any plots i've made, and I wanted to have it all in a single file so that it's all contained to just that file. The data in these files is very simple, mainly just evaluating a problem using mathematica instead of by hand. There are no dependencies between any of the files to each other or any external libraries.

  • $\begingroup$ You could merge NBs in Mathematica programly (please refer to this tutorial), or print them out separately and merge the PDFs by using some external tools. $\endgroup$
    – Silvia
    Jun 12 '13 at 9:09
  • 1
    $\begingroup$ The smart Prof should have made this the final assignment for you...! (Or - perhaps he did...) $\endgroup$
    – cormullion
    Jun 12 '13 at 9:11

Here's an idea to get you started. I would prefer to export each notebook to a separate PDF file, then combine them.

notebookfiles = FileNames["/Users/me/test-dir/*.nb"]
openednotebooks = NotebookOpen[StringJoin[Directory[], "/", #],
    Visible -> False] & /@ notebookfiles
MapThread[NotebookPrint[#1, StringJoin[#2, ".pdf"]] &,
 {openednotebooks, notebookfiles}]
NotebookClose[#] & /@ openednotebooks

Then combine the PDF files using operating system tools. For example, on OS X, you open the first PDF file in Preview, drag and drop the other PDF files into the thumbnails drawer, then re-order and export the resulting assembly to a new file.

As with all such batch operations, testing is recommended before you commit yourself...


This code should do the work:

directory="Your Notebooks directory";

outputNB = 
  CreateDocument[{}, ShowPageBreaks -> True, 
   WindowTitle -> "Your submision"];

  nb = NotebookOpen[ToFileName[directory, fname]];
  SelectionMove[nb, All, Notebook];
  NotebookWrite[outputNB, NotebookRead /@ SelectedCells[nb]];
  SelectionMove[outputNB, After, Notebook];
  SelectionMove[outputNB, Previous, Cell];
  SetOptions[NotebookSelection[outputNB], PageBreakBelow -> True];
  , {fname, fnames}

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.