3
$\begingroup$

I am preparing lecture notes in Mathematica for students, which consist of several tens of notebooks. From time to time I face a necessity in one notebook to give a reference to some formula of another notebook. The formula in such a case is given in a DisplayFormulaNumbered cell, and this cell is tagged.

Now comes my question.

How can I programmatically open the notebook with the formula, such that this cell will be on the screen, possibly, selected, and ready to be seen by the reader?

I will then package this code into a button to put it at the reference, ready to open and look. The best solution would be, if the all CellGroups existing in the document would be collapsed, except for the one containing the tagged cell in question.

To be specific, and to enable one to try, here is a code creating a notebook with three groups of Section/TextCell/TextCell/DisplayFormulaNumberedCell. The cells with the formula are tagged by "L1", "L2" and "L3" correspondingly:

 nb = CreateDocument[{CellGroup[{TextCell["The head1", "Section"], 
     TextCell["First text cell"], TextCell["Second text cell"], 
     TextCell["x+y", "DisplayFormulaNumbered", CellTags -> {"L1"}]}], 
   CellGroup[{TextCell["The head2", "Section"], 
     TextCell["First text cell"], TextCell["Second text cell"], 
     TextCell["a+b", "DisplayFormulaNumbered", CellTags -> {"L2"}]}],
   CellGroup[{TextCell["The head3", "Section"], 
     TextCell["First text cell"], TextCell["Second text cell"], 
     TextCell["u/v", "DisplayFormulaNumbered", CellTags -> {"L3"}]}]
   }]

Let us try, say, to open it programmatically from another notebook with the "L2" cell selected, while the CellGroups which do not contain "L2" - collapsed.

$\endgroup$
  • $\begingroup$ @Kuba Thank you. That works perfectly. I only put a command nb = NotebookOpen["filePath"]; in front of your code to complete it. Why do not you formulate it as a regular answer? $\endgroup$ – Alexei Boulbitch Dec 14 '16 at 10:55
4
$\begingroup$

This procedure seems to do what you are after:

Module[{ nb = NotebookOpen["filePath"] },
  SelectionMove[nb, All, Notebook]; 
  FrontEndExecute @ FrontEndToken[nb, "SelectionCloseAllGroups"];    
  NotebookFind[nb, "L3", All, CellTags]
]

Depending of your needs NotebookLocate can be used too. It automatically aims at CellTags and can open closed notebooks.

$\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.