3
$\begingroup$

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.

$\endgroup$
2
  • $\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
    Commented Jun 12, 2013 at 9:09
  • 1
    $\begingroup$ The smart Prof should have made this the final assignment for you...! (Or - perhaps he did...) $\endgroup$
    – cormullion
    Commented Jun 12, 2013 at 9:11

2 Answers 2

2
$\begingroup$

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...

$\endgroup$
1
$\begingroup$

This code should do the work:

directory="Your Notebooks directory";
fnames=FileNames["*.nb"]

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

Do[
  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];
  NotebookClose[nb];
  , {fname, fnames}
];
$\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.