1
$\begingroup$

Consider some Mathematica notebook where there is some preliminary code followed by chapters, sections, and subsections that must be evaluated using the code. Say, there are Chapter 1, Chapter 2, where there are sections 1.1, 1.2, and the subsections 1.1.1 (in 1.1), 1.2.1, 1.2.2 (in 1.2).

Would it be possible to

  1. Associate the chapters, sections, and sub-sections with some parameters (say, Chapter 1 -> "A", Chapter 2 -> "B"), (1.1 -> "First", 1.2 -> "Second"), (1.1.1 -> "Green", 1.2.1 -> "Yellow 1", 1.2.2 -> "Yellow 2"),

  2. Launch only the subsection associated with the given keys entered in a dialog window? Say, if I subsequently choose "A", "First", "Green", then the subsection Chapter 1/1.1/1.1.1 will be launched?

Edit

I have found this question. So I select the particular cell including Chapter 1 and just some equality x = 12 below, add the tag "Chapter 1" in Cells/Cell tags/Add/remove cell tags, and use the following code:

nb = EvaluationNotebook[];
(NotebookFind[nb, #, All, CellTags]; 
   SelectionEvaluate[nb]) & /@ {"Chapter 1"}

As far as I understand, there is a possibility to add particular tags for sections and sub-sections inside the chapter, and that's it. If there are elegant solutions for this problem, I would appreciate any help.

$\endgroup$
2
  • $\begingroup$ It's probably better to split it into different notebooks (under the same kernel) and have it open and evaluate that particular notebook instead. $\endgroup$
    – alex
    Mar 14, 2023 at 11:53
  • 1
    $\begingroup$ You can select a whole cell group. E.g. run this in a doc page for any standard function: NotebookFind[EvaluationNotebook[], "Scope"]; SelectionMove[EvaluationNotebook[], All, CellGroup]. CellTags are probably safer than text search, but you need only tag the chapter heading cells. $\endgroup$
    – Michael E2
    Mar 14, 2023 at 12:25

1 Answer 1

3
$\begingroup$
evaluateGroupDialog := 
 DynamicModule[{sel = {}, 
   types = {"Title", "Chapter", "Section", "Subsection", 
     "Subsubsection"}, cells, inputs, group},
  cells = 
   Association[
    Table[type -> 
      Select[Cells[], 
       CurrentValue[#, "CellStyle"] == {type} &], {type, types}]];
  inputs = 
   Table[{type, 
     PopupMenu[
      Dynamic[sel], (# -> First@NotebookRead[#]) & /@ 
       cells[type]]}, {type, types}];
  
  group = DialogInput[Grid[{{"Select group to evaluate", ""},
      Sequence @@ inputs, {CancelButton[], 
       DefaultButton["Evaluate", DialogReturn[sel]]}}, 
     Alignment -> Left], Modal -> True];
  
  If[group =!= $Canceled,
   SelectionMove[group, All, CellGroup];
   SelectionEvaluate[InputNotebook[]]
   ];
  ]

evaluateGroupDialog;

Group selector

$\endgroup$

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.