1

I would like to change from within section to within chapter numbering of figures and tables after the first chapter of my document. However, entering \counterwithin{figure}{chapter} \counterwithin{table}{chapter} mid document does not work. Using the starred option does count within chapters, but resets the formatting of the caption numbers to "Figure 2.1.1, 2.1.2, etc...".

\documentclass[a4paper, 12pt]{report}

\usepackage[french]{babel}
\usepackage{caption}
\usepackage{chngcntr}

\begin{document}

% Setup caption numbering for first chapter
\renewcommand\thesection{\Alph{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}

\chapter{Chapter 1}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

% Setup caption numbering for subsequent chapters
\renewcommand\thesection{\thechapter.\arabic{section}}
\counterwithin{figure}{chapter}
\counterwithin{table}{chapter}

\chapter{Chapter 2}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}


\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\end{document}
1

see if this solution work for you:

\documentclass[a4paper, 12pt]{report}
%\usepackage[french]{babel}
\usepackage{caption}
\usepackage{etoolbox}
\AtBeginEnvironment{figure}{%
\ifnum\value{chapter}=1
    \renewcommand\thefigure{\thesection-\arabic{figure}}
\else
    \renewcommand\thefigure{\thechapter-\arabic{figure}}
\fi}
\AtBeginEnvironment{table}{%
\ifnum\value{chapter}=1
    \renewcommand\thetable{\thesection-\arabic{table}}
\else
    \renewcommand\thetable{\thechapter-\arabic{table}}
\fi}

\begin{document}
\chapter{Chapter 1}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\chapter{Chapter 2}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}


\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\end{document}

Use of chngcntr and macros \counterwithin{figure}{chapter} and \counterwithin{table}{chapter} in this case doesn't work as you expected. Even with the use of etoolbox as I suggest in the mwe above. Therefore I use redefinition of \thefigure and \thetable depending on chapter number.

enter image description here enter image description here

1

You're forgetting to call the relevant \counterwithout instructions.

\documentclass[a4paper, 12pt]{report}

\usepackage{chngcntr}

\begin{document}

% Setup caption numbering for first chapter
\renewcommand\thesection{\Alph{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}

\chapter{Chapter 1}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

% Setup caption numbering for subsequent chapters
\renewcommand\thesection{\thechapter.\arabic{section}}
\counterwithout{figure}{section}
\counterwithout{table}{section}
\counterwithin{figure}{chapter}
\counterwithin{table}{chapter}

\chapter{Chapter 2}

\section{Section 1}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\begin{figure}[h!]
\caption{A figure}
\end{figure}


\section{Section 2}

\begin{figure}[h!]
\caption{A figure}
\end{figure}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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