4
$\begingroup$

I can count the number of "Subsection" cells in a notebook by evaluating the following code at the BOTTOM of the notebook.

CurrentValue[{"CounterValue", "Subsection"}]

It only counts "Subsection" cells above the cell which I evaluate, so you should put the code at the BOTTOM of the notebook so the count is not short.

I'm trying to capture this count externally from another notebook. I can't figure out how to do this as the code above must be evaluated in a cell inside the notebook being counted. And must be at the bottom.

Is there a way to do this programmatically? Or is there a more direct method?


Updated Response to Suggestion Below: @Dan7geo I am running M10.3 on MACOS 10.12. To illustrate why your second suggestion fails using a custom stylesheet, I created a simple notebook with 1 subsection cells and evaluated it using your code with no custom stylesheet. It works. However, if I apply a custom stylesheet it alters the result. See gif below.

enter image description here

Below is one expression from my stylesheet, that is adding to the count. There are 4 of them...

Cell[StyleData["Section"],
 CellFrame->{{0, 0}, {0, 1}},
 CellMargins->{{15, Inherited}, {8, 18}},
 StyleKeyMapping->{"Tab" -> "Subsection"},
 CellGroupingRules->{"SectionGrouping", 30},
 PageBreakBelow->False,
 CellFrameMargins->4,
 CellFrameColor->GrayLevel[0.7],
 LineSpacing->{1, 2},
 LanguageCategory->"NaturalLanguage",
 CounterIncrements->"Section",
 CounterAssignments->{{"Subsection", 0}, {"Subsubsection", 0}, {"Item", 0}, {"Subitem", 0}, {
   "Subsubitem", 0}, {"ItemNumbered", 0}, {"SubitemNumbered", 0}, {"SubsubitemNumbered", 0}},
 MenuSortingValue->1250,
 MenuCommandKey->"4",
 FontFamily->"Gill Sans",
 FontSize->28,
 FontColor->RGBColor[0.7612268253604944, 0.29576562142366675`, 0.08555733577477684]]
$\endgroup$

2 Answers 2

3
$\begingroup$

(Updated to work with custom stylesheets and when notebook history tracking is turned on)

You can execute the following to find the number of subsections in any notebook:

Count[First@NotebookGet@NotebookOpen@"Test.nb", Cell[_, "Subsection",___], Infinity]

(Replace "Test.nb" with the path to the notebook you wish to check)

To find the number of subsections in the current notebook, you can run this command anywhere (not necessarily at the end):

Count[First@NotebookGet@InputNotebook[], Cell[_, "Subsection",___], Infinity]

Note that this method returns the total number of subsections within the notebook as shown below:

enter image description here

$\endgroup$
16
  • 1
    $\begingroup$ Weird. It works on version 11.0 on Windows 10. I don't have a machine with OS X to test this on. $\endgroup$
    – dan7geo
    Oct 9, 2016 at 0:50
  • 1
    $\begingroup$ You could try executing NotebookGet@InputNotebook[] and visually inspect the structure of the output and then try modifying the pattern given to Count. $\endgroup$
    – dan7geo
    Oct 9, 2016 at 0:51
  • 1
    $\begingroup$ Nevermind. Use the modified code I just updated. It should work with Notebook History tracking enabled $\endgroup$
    – dan7geo
    Oct 9, 2016 at 1:01
  • 1
    $\begingroup$ CurrentValue[{"CounterValue","Subsection"}] gives only number of subsections upto the previous section. Whereas the method I posted gives the total number in the notebook. You can test this by creating a subsection then a section and another subsection. Your method returns 1 and mine gives 2. $\endgroup$
    – dan7geo
    Oct 9, 2016 at 1:41
  • 1
    $\begingroup$ This issue doesn't occur for any built-in stylesheets. I've never used any custom stylesheets before so I can't test it with any. $\endgroup$
    – dan7geo
    Oct 9, 2016 at 2:13
4
$\begingroup$

If I understood correctly this should do:

Length @ Cells[InputNotebook[], CellStyle -> "Subsection"]
$\endgroup$
1
  • $\begingroup$ Yes, this is petty straight forward now that I see it. $\endgroup$
    – B flat
    Oct 9, 2016 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.