In latex this resets counter to be A# where # is a running number. Explain how it works…
- User
In latex this resets counter to be A# where # is a running number. Explain how it works and suggest a command that would make it # where is the appendix section number underwhich the figure is found: \setcounter{figure}{0} \renewcommand{\thefigure}{A\arabic{figure}}
- Assistant
Got it — let’s break this down carefully.
Your Code: latexCopiar código\setcounter{figure}{0} \renewcommand{\thefigure}{A\arabic{figure}}
\setcounter{figure}{0} resets the figure counter back to 0.
\renewcommand{\thefigure}{A\arabic{figure}} tells LaTeX how to display the figure number.
Normally, LaTeX would show figures as 1, 2, etc. in a section, but now it will show A1, A2, … because:
A is just a literal string.
\arabic{figure} gives the Arabic numeral of the figure counter.
So the first figure will appear as A1, the second as A2, etc.
What you want: Instead of a fixed A, you want the appendix section letter or number to appear automatically. In LaTeX, the appendix sections (via \appendix) are usually labeled as A, B, C… (using \Alph{section} or \thesection). So you can do something like this: latexCopiar código\renewcommand{\thefigure}{\thesection\arabic{figure}}
- User
Is there a way to set this in the beginning such that only in the appendix (after \appendix) the behavior changes?
- Assistant
Yes — LaTeX gives you a clean hook for exactly this. When you call \appendix in the standard article/report/book classes, two things happen:
\thesection starts producing letters A, B, C…
It executes @chapapp or \appendixname changes.
But the key is: LaTeX runs the macro \appendix once. You can redefine counters and formats after \appendix so that it only affects the appendix.
Easiest way — redefine right after \appendix latexCopiar código\appendix % from here on, the changes apply \renewcommand{\thefigure}{\thesection.\arabic{figure}} \makeatletter @addtoreset{figure}{section} % resets figure counter at every section \makeatother
\renewcommand{\thefigure}{\thesection.\arabic{figure}} makes figure numbers A.1, A.2, … etc.
@addtoreset{figure}{section} automatically resets the figure counter at each new appendix section (so you don’t have to \setcounter{figure}{0} manually).
This way, only after \appendix do you get the A.1/B.1 numbering.
- User
Could you do it in a way that is seemless? So for example the \thefigure checks a variable that is only set in \appendix or that \appendix sets \appendixfigures as well
- Assistant
Yes — you can make it completely seamless so that as soon as \appendix is called the figure numbering changes automatically, without you doing anything extra. The trick is to patch \appendix so that it not only switches to A/B/C section labels (the default) but also redefines \thefigure and resets the counter for you. Here’s a minimal working version: latexCopiar código\usepackage{etoolbox} % for \pretocmd
% Patch \appendix so figure numbering changes automatically \makeatletter \pretocmd{\appendix}{% % This code runs automatically when \appendix is called: \renewcommand{\thefigure}{\thesection.\arabic{figure}}% @addtoreset{figure}{section}% }{}{} \makeatother
etoolbox’s \pretocmd{\appendix}{...} inserts your code at the start of \appendix.
As soon as \appendix is used, the figure counter style changes to A.1, A.2 etc., and the counter resets at each new appendix section automatically.
Before \appendix, everything stays in the normal style.
If you also want tables or equations to behave similarly, you just add extra lines inside the same block: latexCopiar código\pretocmd{\appendix}{% \renewcommand{\thefigure}{\thesection.\arabic{figure}}% @addtoreset{figure}{section}% \renewcommand{\thetable}{\thesection.\arabic{table}}% @addtoreset{table}{section}% }{}{}