Having just read this question

and the Kuba's answer I'm trying to optimize my workflow. I'm writing a Mathematica notebook to create and export a series of graphics. These graphics can be static or dynamic, i.e. the result of a Manipulate; what I want to export from a Manipulate is the current state, not the initial state, so I don't want to evaluate the cells that generate the Manipulate every time I need to export the graphics.

I want to be able to export a single graphics, by clicking a button near the graphics for example, and to export all the graphics at once, by clicking a button at the beginning of the notebook for example. As stated, I don't want to evaluate all the notebook or the initialization cells to accomplish this task.

What I think to do is to add a button near to each input cell that create a graphics (how by stylesheet?); this button should read and export the graphics in the output cell in it's current Manipulate state.

I think I can also create a function that search all these outputs in the current notebook, thanks to some tag or style, and virtually press any of these button in some way.

First question: what do you think about this strategy? Any issue, suggestion, better approach?

I'm experimenting with these ideas but I encountered a problem. I globally defined a variable containing a series of options tio apply to all graphics and this cause an issue I don't understand. A minimal example:

plotOpts = Sequence[GridLines -> Automatic, PlotTheme -> "Monochrome"];
g = Manipulate[
  Plot[Sin[n x], {x, 0, 2 \[Pi]}, Evaluate@plotOpts], {{n, 1}, 1, 5}]

Mathematica graphics

and, in the subsequent cell:

Setting @ ToExpression @ First @ NotebookRead @ PreviousCell[]

but I get this error

During evaluation of In[307]:= Plot::nonopt: Options expected 
(instead of $CellContext`plotOpts) beyond position 2 in 
Plot[Sin[n$175064 x],{x,0,2 \[Pi]},$CellContext`plotOpts]. 
An option must be a rule or a list of rules. 

There is some CellContext issue. After reading the only question on this site about this "placeholder" I cannot figure out the cause of the problem and how to solve it.

Second question: there is a solution to this?

I noticed that adding SaveDefinition->True doesn't solve, and, if possible, I don't want to be forced to use this option.

  • $\begingroup$ Just use With: With[{plotOpts = Sequence[GridLines -> Automatic, PlotTheme -> "Monochrome"]}, g = Manipulate[ Plot[Sin[n x], {x, 0, 2 \[Pi]}, Evaluate@plotOpts], {{n, 1}, 1, 5}]] $\endgroup$ Commented Mar 1, 2016 at 22:11
  • $\begingroup$ @RolfMertig I cannot use With, not in this way at least. I set plotOpts once at the beginning of my notebook and the I need to use it many times in many different places.. $\endgroup$
    – unlikely
    Commented Mar 1, 2016 at 22:13
  • $\begingroup$ @unlikely so use With[{localopts = plotOpts}, Hold[localopts]] $\endgroup$
    – Kuba
    Commented Mar 2, 2016 at 8:31
  • $\begingroup$ @Kuba Is this the only way? I.e. changing the way I currently generate all my plots? $\endgroup$
    – unlikely
    Commented Mar 2, 2016 at 15:15
  • $\begingroup$ @unlikely Any comments about the answer? $\endgroup$
    – Kuba
    Commented Mar 4, 2016 at 6:40

1 Answer 1


Ok, assuming you can't change the way your plots are done, let's use brute force:

Setting @ ToExpression @ ToExpression @ ReplaceAll[
    ToBoxes @ NotebookRead[PreviousCell[]][[1, 1]],
    s_String /; StringMatchQ[s, "$CellContext`*" ~~ Except["$"]] :> StringDrop[s, 12]

enter image description here


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.