2
$\begingroup$

I want to print Manipulate[] graphics while batch printing several notebooks. Here's the long version:

I have several Manipulate[] graphics cells in my notebooks. The only way I know of to get the graphics to appear in PDF output is to open the notebook, evaluate it, and then save to PDF. If I close my notebook after evaluating the cells with Manipulate[] graphics in them, when I reopen the notebook the graphics are gone.

Now I want to process an entire directory of ~20 notebooks with one command. I thought that by opening the notebooks with NotebookOpen[] then evaluating them with NotebookEvaluate[] before running NotebookPrint[notebook,"filename.pdf"] that would get the Manipulate[] graphics to print to PDF.

Unfortunately, that didn't work. I get huge gray boxes with manipulator controls instead of the graphics images.

This is the code I'm using:


allToPDF[] := Module[{inDir, outDir, nbFileNames, toPDF}
  , inDir = SetDirectory@NotebookDirectory[];
  outDir = inDir <> "PDF";
  nbFileNames = FileNames["review*.nb"];
  toPDF[nbFileName_] := Module[{outFileName, inFileName, nb}
    , inFileName = NotebookDirectory[] <> "\\"  nbFileName;
    outFileName = 
     inDir  "\\PDF\\" <> FileBaseName[nbFileName] <> ".pdf";
    If[FileExistsQ[
       outFileName] \[And] (FileDate[inFileName] <
        FileDate[outFileName]), Return[]];
    nb = NotebookOpen[inFileName];
    NotebookEvaluate[nb];
    NotebookPrint[nb, outFileName];
    NotebookClose[nb]
    ];
  toPDF[#] & /@ nbFileNames
  ]
allToPDF[]
$\endgroup$

1 Answer 1

3
$\begingroup$

Consider this. I have 3 notebooks, {"nb1.nb", "nb2.nb", "nb3.nb"} which for the sake of testing all have a simple Manipulate object. If I follow your method, they print a grey box.

Below I suggest a method to achieve what you're after,

Turn on TrustByDefault

By default, your FE hides Dynamic content (giving grey boxes until you accept the Dynamic Warning dialog). To get around this, you want to set the Global option "TrustByDefault" to True:

    SetOptions[$FrontEnd, {"NotebookSecurityOptions" -> {"TrustByDefault" -> True}}]
    (* Press "Yes" in the dialog that appears *)

I would recommend setting this back to Automatic when you complete your evaluation.

Print notebooks in bulk

Line by line...

Set Directory:

SetDirectory[NotebookDirectory[]];

Locate files to print:

filesToPrint = FileNames["nb*"]
(*{"nb1.nb", "nb2.nb", "nb3.nb"}*)

Open the 3 files. Note the Visible -> False, so your screen isn't filled with 20 Notebooks:

nbs = NotebookOpen[NotebookDirectory[] <> "\\" <> #, 
   Visible -> False] & /@ filesToPrint;

Feed the NotebookObjects and the file names to NotebookPrint to print the open (albeit invisible) notebooks:

NotebookPrint[#[[1]], 
   NotebookDirectory[] <> "\\" <> #[[2]] <> ".pdf"] & /@ 
 Transpose[{nbs, filesToPrint}]

Close the opened notebooks:

NotebookClose /@ nbs;

Altogether:

SetDirectory[NotebookDirectory[]];
filesToPrint = FileNames["nb*"]
nbs = NotebookOpen[NotebookDirectory[] <> "\\" <> #, 
     Visible -> False] & /@ filesToPrint;
NotebookPrint[#[[1]], 
    NotebookDirectory[] <> "\\" <> #[[2]] <> ".pdf"] & /@ 
  Transpose[{nbs, filesToPrint}];
NotebookClose /@ nbs

Now for every "nb*.nb" file, we have a "nb*.nb.pdf":

FileNames[]
(*{"nb1.nb", "nb1.nb.pdf", "nb2.nb", "nb2.nb.pdf", "nb3.nb", \
"nb3.nb.pdf", "print.nb"}*)

And don't forget to set "TrustByDefault" back:

SetOptions[$FrontEnd, {"NotebookSecurityOptions" -> {"TrustByDefault" \
-> Automatic}}]

I'll leave it as an exercise for the reader to compile this into a function, if desired.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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