Avoiding Numbering Headaches: Per-Chapter Vs. Continuous Counters In Latex

When structuring a complex LaTeX document with multiple chapters, managing figure, table, and equation numbering can become problematic. Should counters reset per chapter, creating disjoint numbering like "Figure 1.1", "Figure 2.1"? Or do they increment continuously throughout the document, potentially reaching unwieldy numbers like "Figure 27"? There are benefits and drawbacks to each approach, but LaTeX provides tools to handle either case gracefully.

Defining the Problem: Conflicting Numbering Requirements

Continuous vs. per-chapter numbering needs

Resetting counters on a per-chapter basis keeps numbering localized and relevant to each chapter, avoiding confusion from references to incredibly high figure numbers towards the end of a long document. However, continuous sequential counting provides a unique identifier for every figure, handy when citing sources across chapters. It also prevents duplicate labels if the same figure number gets reused in separate chapters.

Managing cross-references across chapters

Cross-referencing is a crucial capability provided by LaTeX's labeling and referencing commands like \label and \ref. However, per-chapter numbering can create ambiguity with references like "See Figure 1.2". Did this point to the second figure in Chapter 1, or the figure labeled "1.2" in a later chapter? Continuous sequential numbering avoids such confusion but may reference unrelated entities with large numerical gaps between the current context and the cited figure number.

Setting the Counter Style

\usepackage{chngcntr} package

The chngcntr package extends LaTeX's built-in counter manipulation by adding commands like \counterwithin and \counterwithout. This allows resetting counters per logical grouping while retaining continuous numbering for the entire document. Other counter configuration packages exist, but chngcntr provides the right balance of capabilities for complex multifaceted documents.

Defining counter styles

LaTeX counters trigger increasing integer sequences that can be formatted into chapter.number style representation when referenced in the document body. The \the command defines this display format.

For example, \thefigure might be defined as:

\renewcommand{\thefigure}{\thechapter.\arabic{figure}}  

Which would format all figure numbers as "chapter.figurenumber". Modifying counter display styles is key to clarifying document structure for the reader.

Resetting Counters Per Chapter

\counterwithin{counter}{parentcounter}

The \counterwithin command from chngcntr resets the specified counter whenever its parent counter changes. This allows counters to increment locally within logical groups.

For example, \counterwithin{figure}{chapter} would reset the figure counter to 1 whenever the chapter counter increases, resulting in per-chapter localized figure numbering.

Example: \counterwithin{figure}{chapter}

To number figures within chapters:

\usepackage{chngcntr}

\counterwithin{figure}{chapter} 

If Chapter 1 displayed Figures 1.1, 1.2, 1.3 and Chapter 2 displayed Figures 2.1, 2.2, 2.3, and so on, this achieves per-chapter numbering while retaining the global figure counter sequence.

Using Continuous Counting

Global counters with \counterwithout

The complement command to \counterwithin is \counterwithout from the chngcntr package. This disconnects the counter sequence from parent resets, incrementing globally across all descendants.

Example: \counterwithout{figure}{chapter}

  
\usepackage{chngcntr}
  
\counterwithout{figure}{chapter}

Now figures increment sequentially throughout the document, unbound from per-chapter resets. For example, Chapter 1 could display Figures 1, 2, 3 and Chapter 2 would continue with Figures 4, 5, 6. This provides a unique identifier for each figure while retaining readable formatting via commands like \thefigure.

Handling Cross-references

\ref vs \autoref for references

The \ref command inserts the formatted counter label like the figure number for a referenced entity. In contrast, \autoref automates the textual description, outputting "Figure 1.2" instead of just "1.2". \autoref relies on counter name localization via the \newrefformat command.

\newrefformat{figure}{Figure~\ref{#1}}

Now \autoref{fig:myfigure} would output the text "Figure" along with the referenced figure number.

Linking to figures across chapters

Cross-referencing figures in other chapters can be ambiguous if the numbering style depends on the chapter counter. \autoref's automatic labeling resolves such ambiguity since it will output the full "Figure 10.2" instead of just "10.2" when pointing to a distant figure.

For per-chapter numbering schemes, explicitly including the chapter number in references is recommended, even though this slightly contradicts LaTeX's emphasis on leaving numbering details to the templating system. Some manual intervention clarifies document structure for readers.

Example Document Structure

Document class and packages

\documentclass[12pt,a4paper]{book}
  
\usepackage{chngcntr}
\usepackage{hyperref} 

\newrefformat{figure}{Figure~\ref{#1}} 
\newrefformat{table}{Table~\ref{#1}}

Defining counters

% Per-chapter numbering 
\counterwithin{figure}{chapter}  

% Continuous numbering
\counterwithout{table}{chapter}  

Chapter 1 figures

\chapter{Introduction}
...text...
  
\begin{figure}[h]
\centering
\includegraphics[scale=0.5]{fig1.pdf} 
\caption{Figure one} \label{fig:1a}
\end{figure}

\begin{figure}[h] 
\centering
\includegraphics[scale=0.5]{fig2.pdf}
\caption{The second figure} \label{fig:1b}
\end{figure}

Chapter 2 figures

  
\chapter{Main Content}
...text...
  
\begin{figure}[h]
\centering
\includegraphics[scale=0.5]{fig3.pdf}
\caption{First figure this chapter} \label{fig:2a} 
\end{figure}
  
\begin{table}[h]
\centering
\begin{tabular}{ |c|c| } 
 \hline
 Cell 1 & Cell 2\\ 
 \hline
\end{tabular}
\caption{A table} \label{tab:1}
\end{table}

Cross-references

Figure \ref{fig:1a} in Chapter 1 shows something whilst \autoref{fig:2a} in Chapter 2 demonstrates something else. \autoref{tab:1} summarizes the content.

Conclusion

Recap of techniques

Managing numbering and cross-referencing in complex LaTeX documents with chapters requires using:\

  • chngcntrs \counterwithin and \counterwithout commands
  • Custom counter display styles (\thecounter)
  • \ref and \autoref for references
  • Explicit chapter context for ambiguous references

Additional resources

For more examples, Chapter 10 of the LaTeX Wikibook covers numbering and referencing in depth. The chngcntr package documentation also covers advanced usage.

Leave a Reply

Your email address will not be published. Required fields are marked *