8

TeX specialists!

I write a document in LaTeX using the book document class.

In a section at the beginning of my document I describe the structure of the document, listing all the chapters and short description of their content. I would like to start that section with the information how many chapters I have in the document in total.

So, is there a way I can get the total number of chapters in the document to print it in the text?

\section{Structure of the document}
The document is organized into ?? chapters as follows:
\begin{enumerate}
\item Chapter \ref{chap:intro} brings the introduction.
\item Chapter \ref{chap:problem} describes the problem we try to solve.
...
\end{enumerate}
Sharg
  • 89

2 Answers2

10

This is possible with totcount package, for example!

\regtotcounter{chapter} registers an existing counter name and stores the value at the end of the compilation to the .aux file, during the second compilation the total value can be obtained with \total{chapter}.

There's no need to adjust a label to the last chapter then manually.

My own package xassoccnt allows for total counters as well.

\documentclass{book}

\usepackage{totcount}

\regtotcounter{chapter}

\begin{document}


\section{Structure of the document}
The document is organized into \total{chapter} chapters as follows:
\begin{enumerate}
\item Chapter \ref{chap:intro} brings the introduction.
\item Chapter \ref{chap:problem} describes the problem we try to solve.
\end{enumerate}


\chapter{First}  \label{chap:intro}


\chapter{Second} \label{chap:problem}


\chapter{Third} 

\chapter{Four}

\end{document}

enter image description here

Update: Here's a 'proof' that splitting a document into myintro.tex etc. still leads to a working document!

\documentclass{book}


\begin{filecontents}{myintro.tex}
\section{Structure of the document}
The document is organized into \total{chapter} chapters as follows:
\begin{enumerate}
\item Chapter \ref{chap:intro} brings the introduction.
\item Chapter \ref{chap:problem} describes the problem we try to solve.
\end{enumerate}
\end{filecontents}

\usepackage{totcount}

\regtotcounter{chapter}



\begin{document}
\InputIfFileExists{myintro}{}{}
\chapter{First}  \label{chap:intro}


\chapter{Second} \label{chap:problem}


\chapter{Third} 

\chapter{Four}

\end{document}

Update No 2

In case \appendix is used, the chapter counter is reset. in this case use \DeclareTotalAssociatedCounters{chapter}{allchapters}, where allchapters ignores the reseting of chapter.

\documentclass{book}


\usepackage{xassoccnt}

\DeclareTotalAssociatedCounters{chapter}{allchapters}

\begin{document}
\section{Structure of the document}
The document is organized into \TotalValue{allchapters} chapters as follows:
\begin{enumerate}
\item Chapter \ref{chap:intro} brings the introduction.
\item Chapter \ref{chap:problem} describes the problem we try to solve.
\end{enumerate}


\chapter{First}  \label{chap:intro}


\chapter{Second} \label{chap:problem}


\chapter{Third} 

\chapter{Four}

\appendix

\chapter{One of Appendix}
\chapter{Two of Appendix}

\end{document}

enter image description here

  • I tried this approach. But it always prints value 1. I assume the problem is I need to use the \total{chapter} reference inside of the first chapter, thus having the variable initialized again and so far counted only one chapter... – Sharg Jan 31 '16 at 20:49
  • @Sharg: No, the position of \total{chapter} doesn't matter unless you would use it in the preamble. The total chapter counter has a different name as chapter and does not interfere. Apparently you're doing something wrong –  Feb 01 '16 at 10:52
  • I do not want to post the whole document. Is it possible that the separation into multiple tex files and using \input{intro.tex} might be the problem? – Sharg Feb 01 '16 at 17:27
  • @Sharg: Splitting the document causes more bad things than good ones, but in this case, I am not sure this is the issue. totcount normaly writes to the \jobname.aux file, not to something different. –  Feb 01 '16 at 17:29
  • @Sharg: I just tested with a splitted document -- it still works! –  Feb 01 '16 at 17:39
  • @Sharg I'm having the same issue as you had. It always prints value 1. Did you solve this problem? If yes, how? – Yannic Bürgmann Dec 18 '17 at 09:30
  • Sent u a mail :) But don't waste to much time to dig into it. It's just a convenience feature and if it doesn't work its not a big deal. Just wanted to ask if Sharg maybe found an easy solution. – Yannic Bürgmann Dec 18 '17 at 12:33
  • @YannicKlem: I'll try later on, I've seen the mail already, however. –  Dec 18 '17 at 12:35
  • @YannicKlem: I've answered your mail with a working solution, but I got no feedback. –  Jan 04 '18 at 00:00
  • Oh sorry I postboned to try your suggested package but somehow forgot it. Will try today – Yannic Bürgmann Jan 04 '18 at 07:09
  • Sorry for delayed response. I missed the e-mail. I have not found the working solution. I just resigned and used the id of the last chapter to print the number representing the number of chapters. – Sharg Jan 04 '18 at 13:04
  • @Sharg: I did not send an email to you ;-) –  Jan 04 '18 at 13:06
  • @Sharg: I've added another solution, perhaps this works for you –  Jan 04 '18 at 13:19
2

If you're only counting chapters and don't have a counter-resetting Appendix in there somewhere, you can set a \label at the end of the document that specifically captures the current value of \arabic{chapter}:

enter image description here

\documentclass{book}

\makeatletter
\AtEndDocument{%
  \protected@edef\@currentlabel{\arabic{chapter}}%
  \label{chap:last}}
\makeatother

\usepackage{multido}

\begin{document}

\chapter{Structure of the document}
The document has \ref{chap:last} chapters.

\multido{\i=1+1}{57}{\chapter{A chapter}}

\end{document}
Werner
  • 603,163