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}
5
  • 1
    you can use like The document is organized into \ref{chap:last} chapters as follows:
    – touhami
    Jan 31, 2016 at 15:25
  • 1
    Good point. It means I need to update it if I add another chapter to the end but it should work well enough. Thanks.
    – Sharg
    Jan 31, 2016 at 15:30
  • Any news on this?
    – user31729
    Feb 4, 2016 at 16:04
  • None of the suggested solutions worked for me, so I stayed with using the label of the last chapter for the count.
    – Sharg
    Jul 22, 2017 at 12:59

2 Answers 2

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

13
  • 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, 2016 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
    – user31729
    Feb 1, 2016 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 1, 2016 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.
    – user31729
    Feb 1, 2016 at 17:29
  • @Sharg: I just tested with a splitted document -- it still works!
    – user31729
    Feb 1, 2016 at 17:39
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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .