8

The code block below

CreateDocument[{
  TextCell["Title", "Title"],
  TextCell["Subtitle", "Subtitle"],
  TextCell["Section 1", "Section"],
  TextCell["Section 1.1", "Subsection"],
  TextCell["Section 1.2", "Subsection"],
  TextCell["Section 1.3", "Subsection"],
  TextCell["Section 2", "Section"],
  TextCell["Section 2.1", "Subsection"],
  TextCell["Section 2.2", "Subsection"],
  TextCell["Section 2.3", "Subsection"],
  TextCell["Section 3", "Section"],
  TextCell["Section 2.1", "Subsection"],
  TextCell["Section 2.2", "Subsection"],
  TextCell["Section 2.3", "Subsection"]}
 ]

will create a skeleton notebook.

Is it possible to create that notebook so that the sections are collapsed? So that the notebook will be displayed as if (eg) the Cell closer covering Section 1 had been clicked. Ditto for Sections 2 & 3.

Abhranil Das
  • 4,972
  • 6
  • 31
  • 41
dwa
  • 494
  • 5
  • 12

1 Answers1

11

Use CellGroup to open or close specific cells - see http://reference.wolfram.com/mathematica/ref/CellGroup.html

CreateDocument[{
  TextCell["Title", "Title"],
  TextCell["Subtitle", "Subtitle"],
  CellGroup[{
    TextCell["Section 1", "Section"],
    TextCell["Section 1.1", "Subsection"], 
    TextCell["Section 1.2", "Subsection"], 
    TextCell["Section 1.3", "Subsection"]
  }, Closed],
  TextCell["Section 2", "Section"],
  TextCell["Section 2.1", "Subsection"], 
  TextCell["Section 2.2", "Subsection"], 
  TextCell["Section 2.3", "Subsection"],
  TextCell["Section 3", "Section"],
  TextCell["Section 2.1", "Subsection"], 
  TextCell["Section 2.2", "Subsection"], 
  TextCell["Section 2.3", "Subsection"]}]

Or you could wrap the entire collection of TextCells in one high-level CellGroup and play with CellGroup's optional second argument. For example, this will open only the first three cell groups:

CreateDocument[{
  CellGroup[{
    TextCell["Title", "Title"],
    TextCell["Subtitle", "Subtitle"],
    TextCell["Section 1", "Section"],
    TextCell["Section 1.1", "Subsection"], 
    TextCell["Section 1.2", "Subsection"], 
    TextCell["Section 1.3", "Subsection"],
    TextCell["Section 2", "Section"],
    TextCell["Section 2.1", "Subsection"], 
    TextCell["Section 2.2", "Subsection"], 
    TextCell["Section 2.3", "Subsection"],
    TextCell["Section 3", "Section"],
    TextCell["Section 2.1", "Subsection"], 
    TextCell["Section 2.2", "Subsection"], 
    TextCell["Section 2.3", "Subsection"]
  }, {1, 2, 3}]
}]
Bill White
  • 1,739
  • 13
  • 14
  • Thanks @Bill White! The first example was just what I was after. – dwa May 12 '11 at 03:28
  • 1
    And thank you for accepting my answer. By the way, using Closed as CellGroup's second argument seems to be undocumented, unless I'm overlooking something. I use CellGroupData[{...}, Closed] all the time when generating notebooks programmatically, and Closed seems to work fine here, too. – Bill White May 12 '11 at 04:18