Using V12 on windows.
This is the problem. I am calling an external function (Rubi in this case, but it could be anything), which ends up printing a cell (image) to the Console. I simply want to capture this cell and export it to PDF file.
Even if I redirect the output to the current notebook, I can't use the command Export["image.pdf", NotebookRead[PreviousCell[]]];
to do what I want, since the code which call the external function runs in a loop.
Is there a way to do the same as Export["image.pdf", NotebookRead[PreviousCell[]]];
but to use the Message console as the notebook instead of the current notebook?
To make it more clear, here is a MWE.
CurrentValue[$FrontEnd, {"PrintAction"}] = {"PrintToConsole"};
(*a function which prints an image to console*)
externalFunction[max_] := Print[Plot[Sin[x], {x, -max, max}]];
(*run a loop calling the function, and export its output*)
Do[
externalFunction[n];
(*need something like this*)
(*Export["t.pdf",MessagesNotebookRead[PreviousCell[]]]; ??*)
,
{n, 1, 5}
]
Directing the Print to the current notebook does not work
CurrentValue[$FrontEnd, {"PrintAction"}] = {"PrintToNotebook"};
externalFunction[max_] := Print[Plot[Sin[x], {x, -max, max}]];
Do[
externalFunction[n];
Export["t.pdf", NotebookRead[PreviousCell[]]];
,
{n, 1, 5}
]
Since PreviousCell[]
in the above is not what I want, it will read the cell before the actual loop itself.
Also this does not work
CurrentValue[$FrontEnd, {"PrintAction"}] = {"PrintToNotebook"};
externalFunction[max_] := Print[Plot[Sin[x], {x, -max, max}]];
Do[
Export["t.pdf", externalFunction[n]]
,
{n, 1, 5}
]
I could not find a way to tell Mathematica to use the Messages console notebook as the notebook to the PreviousCell[]
What is the best way export a cell in Messages console to pdf file? It will be the last cell each time, as in the above example. This is done in a loop. Each iteration generates one Rubi cell in the Messages console and I need to capture that cell as an image.
Appendix
If someone would like to try this with the actual Rubi output, here is a MWE
<< Rubi`
CurrentValue[$FrontEnd, {"PrintAction"}] = {"PrintToConsole"};
Do [
Steps[Int[x Sin[x], x]];
(*save output in console to image, how?*)
Steps[Int[x Cos[x], x]]
(*save output in console to image, how?*)
,
{n, 2}
]
Last[Cells[MessagesNotebook[]]]
instead ofPreviousCell[]
? You wouldn't need to change the printing action then. $\endgroup$