1
$\begingroup$

I have the following simple line of code in my notebook

Plot[x^2, {x, 0, 2}]

I add a CellTag with the string "Plot" to that cell.

Sequential Module

I execute the following two module sequentially.

Module[
  {
   nb
   },
  nb = EvaluationNotebook[];
  NotebookFind[nb, "Plot", All, CellTags];
  SelectionEvaluate[nb]
  ];

Module[
  {
   cellsPlot = 
    Cells[CellStyle -> {"Output"}, CellTags -> "Plot"][[1]]
   },
  SelectionMove[cellsPlot, All, Cell];
  FrontEndExecute[FrontEndToken["OpenCloseGroup"]]
  ];

This produces the expected result, a plot with the input cell closed (note the output figure inherits the tag from the input cell).

Mathematica graphics

Nested Module

When I nest the modules

Module[
  {
   nb
   },
  nb = EvaluationNotebook[];
  NotebookFind[nb, "Plot", All, CellTags];
  SelectionEvaluate[nb];
  Module[
   {
    cellsPlot = Cells[CellStyle -> {"Output"}, CellTags -> "Plot"][[1]]
    },
   SelectionMove[cellsPlot, All, Cell];
   FrontEndExecute[FrontEndToken["OpenCloseGroup"]]
   ]
  ];

I get an unexpected result. The Plot is made but the cell is not closed.

Mathematica graphics

I expected that once the plot is made, the figure (with its inherited tag) would be available and the line of code would be closed just as in the sequential module processing.

Can someone explain why this is not happening?

Is there a workaround so that I would cause this to happen (my actual code is more complex and uses buttons)

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.