1

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.

John Taylor
  • 5,701
  • 2
  • 12
  • 33
  • It's probably better to split it into different notebooks (under the same kernel) and have it open and evaluate that particular notebook instead. – alex Mar 14 '23 at 11:53
  • 1
    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. – Michael E2 Mar 14 '23 at 12:25

1 Answers1

3
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

Domen
  • 23,608
  • 1
  • 27
  • 45