How can I capture output from Print from a function which does not leave the printed object as its final output, (and which I do not wish to edit)? E.g.
Module[{},
Print[Plot[Sin[x], {x, 0, 2 Pi}]];
a = 123]
I would like to use something like a temporary setting for $PrePrint
or $Post
, rather than use cell selection (SelectionMove
, NotebookRead
).
The aim is somehow to intercept the Print
output -- the plot object -- and set it to a variable, (without altering the module).
This attempt did not work:-
Print[Sow@Plot[Sin[x], {x, 0, 2 Pi}]]
and// Reap
at the end captures Print output? $\endgroup$123
being stored, the plot, or both? $\endgroup$Print
command. $\endgroup$$PrePrint
or$Post
which would also capture the output ofPlot[Sin[x],{x,-5,5}];
? $\endgroup$$Output
. But setting a variable with$PrePrint
could be neater. $\endgroup$