4

I spent some time looking at existing posts on this topic but I could not find anything that I was able to use. I am writing a LaTeX document with some sections and an appendix. I would like all the equations in the sections to be labeled (and cross-referenced) normally, using the \label{somelabel} inside \begin{equation} ... \end{equation} and then \ref{somelabel} when I want to reference it. So this will produce labels (1), (2), etc.

But I would like to use a separate counter for the equations in the appendix, and these equations should be labeled (A1), (A2), etc. So the printed output should look like:

Section 1 This is the first equation (1). Using equation (A1), we find the second equation (2)

Section 2 This is the third equation (3), using equation (A2)

Appendix This is the first appendix equation (A1). This is the second appendix equation (A2).

etc.

5
  • 1
    Does writing \setcounter{equation}{0}\renewcommand\theequation{A\arabic{equation}} after \apendix solves your problem?
    – Udi Fogiel
    Dec 18, 2022 at 14:09
  • @Udi Fogiel Yes, it does! Thank you. Dec 18, 2022 at 14:32
  • Which document class do you employ? And which command(s) do you employ to enter the appendix portion of the document? Does your document contain a single appendix section, or could it contain more than one appendix section? If the document contains, two appendix sections labelled "A" and "B", should any equations in Appendix B be labeled (B1), (B2), etc? Please advise.
    – Mico
    Dec 18, 2022 at 14:51
  • 1
    @Mico I am using documentclass{article} I am using \appendix to enter the appendix. My document contains only one appendix but it would be interesting to know ow to handle the situation with two appendices. Dec 18, 2022 at 15:02
  • @ValerioDeAngelis - please see the answer I posted.
    – Mico
    Dec 18, 2022 at 15:17

3 Answers 3

7

I suggest you add the instructions

\counterwithin*{equation}{section}
\renewcommand\theequation{\thesection\arabic{equation}}

immediately after \appendix. The former instruction tells LaTeX to reset the equation counter to zero each time \section is executed. The latter determines how equation numbers will be displayed -- here: by prefixing the section "number" (A, B, etc) before the plain equation number.

That way, equations in Appendix B will automatically be labeled "B1", "B2", etc.

enter image description here

\documentclass{article}
\usepackage[noabbrev]{cleveref} % for "clever" cross-references

\begin{document}

\noindent 
\Cref{eq:aaa,eq:bbb}.
\Cref{eq:ccc,eq:ddd,eq:eee,eq:fff,eq:ggg,eq:hhh}.

\section{Hello World}

\begin{equation} \label{eq:aaa} 1+1=2 \end{equation}
\begin{equation} \label{eq:bbb} 1+2=3 \end{equation}

\appendix  % label section numbers alphabetically: "A", "B", etc
\counterwithin*{equation}{section} % reset 'equation' counter whenever '\section' is executed
\renewcommand\theequation{\thesection\arabic{equation}} % how to display the equation "number"

\section{Uno}
\begin{equation} \label{eq:ccc} 2+2=4 \end{equation}
\begin{equation} \label{eq:ddd} 2+3=5 \end{equation}
\begin{equation} \label{eq:eee} 3+3=6 \end{equation}

\section{Due}
\begin{equation} \label{eq:fff} 3+4=7 \end{equation}
\begin{equation} \label{eq:ggg} 4+4=8 \end{equation}
\begin{equation} \label{eq:hhh} 4+5=9 \end{equation}

\end{document}
1
  • 1
    +1. Yes, your solution is more general. (I always think about how simple should be an answer to a basic question). Dec 18, 2022 at 15:32
4

A naive, but working version:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\section{One}

\begin{equation}
\label{eq1}
2+2=7
\end{equation}

\begin{equation}
\label{eq2}
2+2=7
\end{equation}

\begin{equation}
\label{eq2}
2+2=7
\end{equation}

\section{Two}

\begin{equation}
\label{eq4}
2+2=7
\end{equation}

\begin{equation}
\label{eq5}
2+2=7
\end{equation}

\begin{equation}
\label{eq6}
2+2=7
\end{equation}

\eqref{eq1}, \eqref{eq4}, \eqref{eq7}

\appendix

\section{Appendix}
\renewcommand{\theequation}{A.\arabic{equation}}
\setcounter{equation}{0}

\begin{equation}
\label{eq7}
2+2=7
\end{equation}

\begin{equation}
\label{eq8}
2+2=7
\end{equation}

\begin{equation}
\label{eq9}
2+2=7
\end{equation}

\eqref{eq1}, \eqref{eq4}, \eqref{eq7}

\end{document}

enter image description here

If you really do not want the dot between A and the number, you can write \renewcommand{\theequation}{A\arabic{equation}}.

1
  • +1. You could combine \renewcommand{\theequation}{A.\arabic{equation}}\setcounter{equation}{0} as \counterwithin{equation}{section}.
    – Mico
    Dec 18, 2022 at 15:24
1

The easiest way is to add the section Appendix for equation numbering after you declare the start of \appendix. Like this

\appendix
\numberwithin{equation}{section}

Then all equations after that will be labeled (A.1), (A.2) and so on. This is neat since \numberwithin does not have to be put in the preamble.

1
  • You should also mention that (a) the \numberwithin macro is provided by the amsmath package and hence that it's necessary to load the amsmath package, and (b) that your proposed solution inserts a period between the section label and the equation number; this formatting aspect runs counter to the OP's stated formatting objective. A separate issue: Your claim that "This is neat since \numberwithin does not have to be put in the preamble" is somewhat confusing since, in fact, \numberwithin{equation}{section} should not be placed in the preamble for the use case at hand.
    – Mico
    Feb 22 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .