3
$\begingroup$

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.

$\endgroup$
  • $\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$ – Rolf Mertig Mar 1 '16 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 Mar 1 '16 at 22:13
  • $\begingroup$ @unlikely so use With[{localopts = plotOpts}, Hold[localopts]] $\endgroup$ – Kuba Mar 2 '16 at 8:31
  • $\begingroup$ @Kuba Is this the only way? I.e. changing the way I currently generate all my plots? $\endgroup$ – unlikely Mar 2 '16 at 15:15
  • $\begingroup$ @unlikely Any comments about the answer? $\endgroup$ – Kuba Mar 4 '16 at 6:40
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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