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}
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}
The document is organized into \ref{chap:last} chapters as follows: