Hello everyone,
I have a function for grading student work. The function outputs a grade report for the student wrapped in a title cell. Here’s a mocked up snippet (probably too much detail) of the output. I can select the title cell manually, choose Print Selection… under the File menu, and save the report as a nice looking PDF file. How can I select and print the cell to file programmatically?
(i wanted to print a graphic cell print because it “printed better”. the above says if the previous output of the last cell was Graphics, then use printcell to print it). I have omitted “printopts” which are just print settings specific for that task.
printcell[file_String, nbopts___] :=
Module[{nb, nbsel, newnb, cell, v, xxx},
nb =.; newnb =.; nbsel =.;
nb = EvaluationNotebook[];
SelectionMove[nb, Previous, EvaluationCell, AutoScroll -> False];
SelectionMove[nb, Previous, Cell, AutoScroll -> False];
SelectionMove[nb, All, TextParagraph, AutoScroll -> False];
cell = NotebookRead[nb];
newnb =.;
newnb = NotebookCreate[Visible -> False];
SelectionMove[newnb, All, Notebook, AutoScroll -> False];
(*
bug if opts not defined short circuits most tests
*)
If[Length[{nbopts}] =!= 0,
(*SetOptions[EvaluationNotebook[],nbopts];*)
SetOptions[newnb, nbopts];
];
NotebookWrite[newnb, cell];
NotebookPrint[newnb, file];
SelectionMove[newnb, All, Notebook, AutoScroll -> False];
nbsel = NotebookSelection[newnb];
v = Options[newnb];
NotebookClose[newnb];
nb =.; newnb =.; nbsel =.;
v
];