How would I make the table of contents display a count of the number of pages in each section, so instead of

1. Foo 1
1.1 Bar 2
1.2 Baz 5

it would show

1. Foo 7
1.1 Bar 4
1.2 Baz 3

I guess something like resetting the page counter to 1 at the end the \section command? But that would capture the number of pages in the previous section, not the current one.

This is just for my own benefit (and discussion with my supervisor) rather than for readers so I don't mind if this looks sloppy or appears at the end of the document rather than the start; I just presumed that modifying the table of contents would be the easiest way to achieve it. My document consists of just chapters and sections (no subsections), so I would be happy if this just worked for sections.

It would be even better if it also worked for chapters though, because they won't necessarily equal the sum of section pages: the sum would tend to overcount because any page containing parts of more than one section will be counted twice, and on the other hand the "section 0" at the start of the chapter wouldn't be included. If this were to work it would be nice if it could also avoid counting blank pages at the end of chapters finishing on an odd-numbered page. (I could avoid blank pages manually by just changing to one-sided layout.)

  • The biggest problem is that you want to keep the same order in the toc, so you will need to use the aux file to tell \chapter where the last page is. – John Kormylo Sep 12 '14 at 13:37
  • @JohnKormylo I'm happy for the table to go at the end of the document, so it's enough to save the page counts as we go along through the document and spit them all out at the end. I'm just not quite experienced enough to do this. (I have something half working using a token register but all the page counts are set to the final value.) – Jim Oldfield Sep 12 '14 at 14:37
  • I posted the particular problem I'm having with my solution to this to: tex.stackexchange.com/questions/200875/… – Jim Oldfield Sep 12 '14 at 15:32
  • Hmm I wonder if one could use \marks for this... – morbusg Sep 21 '14 at 14:49

Since no one replied, I'll post what I ended up using. I got help on adding tokens to a list by asking here on StackExchange and using David Carlisle's answer.

IMPORTANT! This is code that I just slapped together with almost no real understanding of TeX. It is absolutely not meant to be a model of good practice and should be treated with suspicion. I'm posting it here in case it's useful to anyone else, but be sure not to use it for anything "serious" e.g. in package code.


% Create a list of tokens, and allow text to be added to it
\pagecounttoks={\ \par}

    \appendtopagecounttable{\noindent #1\ \ \ #2\ \ \ \ #3\par\ \par}
    \appendtopagecounttable{#1\ \ \ #2\ \ \ \ #3\par}

% Allow commands to be executed after each \chapter and \section command
  \origsection*{#1}\sectionpostlude{#1}{\ }

% The counter used for counting pages

% Name of previous section

% The content of before/after \section commands
  \ifthenelse{\equal{\value{sectionstartpage}}{0} \or \equal{\prevchaptervalue}{-1}}{}{

% The content of before/after \section commands
  \ifthenelse{\equal{\value{chapterstartpage}}{0} \or \equal{\prevchaptervalue}{-1}}{}{

% Also update everything when we get to the bibliography

  \phantomsection\addcontentsline{toc}{chapter}{Page counts}
  \noindent{\huge\bf Page counts}\par\ 

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.