I have a large number of Mathematica notebooks which contain study notes for different topics (as opposed to actual Mathematica Code).

I'm trying to develop some code to randomly pull parts of a given notebook into the current notebook ... to use as a kind of flashcard system for reviewing notes.

I've gotten this far:

nb = NotebookOpen[FindFile["chemistry.nb"], Visible -> False];
sections = NotebookLookup[nb, "CellExpression", Cell[___, "Section", ___]];
NotebookWrite[InputNotebook[], RandomChoice[sections]]

This small amount of code, when evaluated, will write to the current notebook a random section heading cell, from the notebook chemistry.nb.

This is great ... but what I actually want to is write to the current notebook the section heading cell, and all the content cells beneath it (Subsections, Items, ItemParagraphs, Text, etc).

Does anyone know how to do this? There seems to be no built in documentation for NotebookLookup, I only discovered it when I came across this file: http://www.mathematica-journal.com/issue/v9i2/contents/AuthorTools/AuthorTools.pdf

  • $\begingroup$ I use Mathematica 9 and i dont have a function like "NotebookLookup" there are Functions like NotebookFind and NotebookLocate but in your case its better to work with CellObjects as I just recently learned. See my answer for details and dont forget to accept the answer if it solves your problem. Thanks $\endgroup$
    – sacratus
    Mar 27, 2015 at 20:44
  • 1
    $\begingroup$ @sacratus Try <<AuthorTools` $\endgroup$
    – Kuba
    Mar 27, 2015 at 20:51
  • $\begingroup$ @Kuba interesting, seems to be perfect for going deeper into notebook programming and creation. I'll have a closer look, when i reached the limitations of the build-in functionalities. $\endgroup$
    – sacratus
    Mar 27, 2015 at 21:26

1 Answer 1


You can try SelectionMovelike this:

 SelectionMove[nb, Next, CellGroup]


SelectionMove[nb, All, CellGroup]

one of these should work in your case


Full Solution for an arbitrary notebook:

nb = CreateWindow[
DocumentNotebook[{CellGroup[{TextCell["Text Group", "Section"], 
   TextCell["Mary had a little lamb.", "Text"], 
   TextCell["Its fleece was white as snow.", "Text"]}], 
 CellGroup[{TextCell["Graphics Group", "Section"], 
   ExpressionCell[ Plot[Exp[-x^2], {x, -3, 3}], "Output"]}]}]];

sections = Cells[nb, CellStyle -> "Section"]
SelectionMove[RandomChoice[sections], All, CellGroup]
read = NotebookRead[nb];
NotebookWrite[EvaluationNotebook[], read]
  • $\begingroup$ thanks @sacratus, this is exactly what I was looking for $\endgroup$
    – billc
    Mar 28, 2015 at 1:03

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.