1

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
0

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.

\usepackage{ifthen}

% Create a list of tokens, and allow text to be added to it
\newtoks\pagecounttoks
\pagecounttoks={\ \par}
\newcommand{\appendtopagecounttable}[1]{%
\edef\tmp{\pagecounttoks{\the\pagecounttoks#1}}\tmp%
}
\newcommand{\pagecounttable}{\the\pagecounttoks}

\newcommand{\addchapterpagecount}[3]{%
    \appendtopagecounttable{\noindent #1\ \ \ #2\ \ \ \ #3\par\ \par}
}
\newcommand{\addsectionpagecount}[3]{%
    \appendtopagecounttable{#1\ \ \ #2\ \ \ \ #3\par}
}

% Allow commands to be executed after each \chapter and \section command
\let\origsection\section
\renewcommand\section{\@ifstar{\starsection}{\nostarsection}}
\newcommand\nostarsection[1]{%
  \origsection{#1}\sectionpostlude{#1}{\the\value{section}}
}
\newcommand\starsection[1]{%
  \origsection*{#1}\sectionpostlude{#1}{\ }
}
\let\origchapter\chapter
\renewcommand\chapter{\@ifstar{\starchapter}{\nostarchapter}}
\newcommand\nostarchapter[1]{%
  \origchapter{#1}\chapterpostlude{#1}{\the\value{chapter}}
}
\newcommand\starchapter[1]{%
  \origchapter*{#1}\chapterpostlude{#1}{-1}
}

% The counter used for counting pages
\newcounter{sectionstartpage}
\newcounter{sectionendpage}
\newcounter{chapterstartpage}
\newcounter{chapterendpage}

% Name of previous section
\newcommand{\prevchaptername}{}
\newcommand{\prevchaptervalue}{}
\newcommand{\prevsectionname}{}
\newcommand{\prevsectionvalue}{}

% The content of before/after \section commands
\newcommand\sectionpostlude[2]{%
  \let\oldlabel\label
  \def\label##1{}
  \ifthenelse{\equal{\value{sectionstartpage}}{0} \or \equal{\prevchaptervalue}{-1}}{}{
    \setcounter{sectionendpage}{\thepage}%
    \addtocounter{sectionendpage}{-\value{sectionstartpage}}%
    \addsectionpagecount{\prevchaptervalue.\prevsectionvalue}{\prevsectionname}{\the\value{sectionendpage}}
  }
  \setcounter{sectionstartpage}{\thepage}%
  \renewcommand{\prevsectionname}{#1}
  \edef\prevsectionvalue{#2}
  \let\label\oldlabel
}

% The content of before/after \section commands
\newcommand\chapterpostlude[2]{%
  \sectionpostlude{(Introduction)}{0}
  \let\oldlabel\label
  \def\label##1{}
  \ifthenelse{\equal{\value{chapterstartpage}}{0} \or \equal{\prevchaptervalue}{-1}}{}{
    \setcounter{chapterendpage}{\thepage}%
    \ifthenelse{\equal{#1}{Bibliography}}{%
      \addtocounter{chapterendpage}{1}%
    }{}
    \addtocounter{chapterendpage}{-\value{chapterstartpage}}%
    \addchapterpagecount{\prevchaptervalue}{\prevchaptername}{\the\value{chapterendpage}}
  }
  \setcounter{chapterstartpage}{\thepage}%
  \renewcommand{\prevchaptername}{#1}
  \edef\prevchaptervalue{#2}
  \let\label\oldlabel
}

% Also update everything when we get to the bibliography
\let\oldbibliography\bibliography
\renewcommand{\bibliography}[1]{
  \chapterpostlude{Bibliography}{-1}
  \oldbibliography{#1}
}

\newcommand\pagecounttablex{%
  \setstretch{1}
  \clearpage
  \phantomsection\addcontentsline{toc}{chapter}{Page counts}
  \noindent{\huge\bf Page counts}\par\ 
  {\small\pagecounttable}
}

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.