TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
1  
you can use like The document is organized into \ref{chap:last} chapters as follows: – touhami Jan 31 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 at 15:30
    
    
Any news on this? – Christian Hupfer Feb 4 at 16:04

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.

\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}
share|improve this answer
    
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 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 – Christian Hupfer Feb 1 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 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. – Christian Hupfer Feb 1 at 17:29
    
@Sharg: I just tested with a splitted document -- it still works! – Christian Hupfer Feb 1 at 17:39

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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