Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
Generating a table of contents

By selecting File > Printing Settings > Headers and Footers from the menu, I am able to enter << Total Pages >> to print the total number of pages in the notebook. How would I retrieve this value programmatically?

share|improve this question

marked as duplicate by Sjoerd C. de Vries, J. M. Sep 28 '12 at 12:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This is discussed here: mathematica.stackexchange.com/questions/1704/… –  intermath Sep 28 '12 at 0:50
Included in the more comprehensive question here: mathematica.stackexchange.com/q/476/57. –  Sjoerd C. de Vries Sep 28 '12 at 5:34

1 Answer 1

up vote 9 down vote accepted

If you evaluate:

Options[EvaluationNotebook[], PageHeaders]

You will see the underlying code that generates the total pages is:

CounterBox["LastPage", CounterFunction :> Identity]

Counters can be added into a notebook via the menu:

enter image description here

enter image description here

However I was unable to get the counter to return the page count. When trying other counters such as input cells or sections everything worked fine but both "Page" and "LastPage" failed to return a count (although they worked as advertised in the page header when the page is printed). e.g.


works whereas


didn't work.

enter image description here

However when you print the page the counters for "page" and "LastPage" evaluate:

enter image description here

So it appears that page counts are not evaluated until you actually print. So if you require the page count to appear within your notebook working environment it doesn't appear to be possible.


@user1420372 has found that showing the page breaks makes the counters work. I guess the pages remain "uncounted" until required, e.g. either by printing or evaluating and displaying page breaks.

share|improve this answer
Thanks :) That is excellent. I got CellPrint[Cell[TextData[ CounterBox["LastPage"]]]] to work when I showed the page breaks (without showing page breaks it returns 0). –  MLD Sep 28 '12 at 1:00
Note that in the end I used CurrentValue[{"MaxCounterValue", "LastPage"}] based on comments in: (mathematica.stackexchange.com/questions/6073/…) –  MLD Sep 28 '12 at 5:01
I believe you have to set page break view before this works. –  Sjoerd C. de Vries Sep 28 '12 at 5:28

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