# Equation numbers messed up

I'm writing a thesis as a collection of different articles. I'm treating each article as a chapter. The first article has Equations numbering A.1, A.2 etc and the second article has equation numbering B.1, B.2 etc. For some unknown reason, now when I compile the thesis, the equations in Article B has numbers as B.2.1, B.2.2 where the middle 2 is the section number. I want to have B.1 and B.2 as before. Why is LaTeX now including the section number in the equation numbering?

Any help would be highly appreciated.

Thanks a lot...

Update: I still have problems after "mpg" answer. The problem is that in my Article 2, the equation numbers are reset at each main section so that I have in Section B.1 equations B.1, B.2 etc and in Section B.2 I get equation numbers B.1 , B.2 again instead of B.3 and B.4. The equation numbers from Article 1 is fine though. I'm attaching a minimal example. Here is the main document (save as main.tex):

\documentclass[fleqn,a4paper,openany,twoside,11pt]{book}
\usepackage[latin1]{inputenx}
\usepackage[OT1]{fontenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{cite}
\usepackage{fancyhdr}
\usepackage{chapterbib}
\parfillskip 0pt plus 0.75\textwidth

\renewcommand\chaptername{Paper}
\renewcommand{\thechapter}{\Alph{chapter}}
\setcounter{secnumdepth}{4}

\pagestyle{empty}
\pagestyle{fancy}
\fancyfoot{}
\newcommand{\clearemptydoublepage}{\newpage{\pagestyle{empty}\cleardoublepage}}

\begin{document}

%\clearemptydoublepage
\newpage{\pagestyle{empty}\cleardoublepage\pagenumbering{roman}}
\setcounter{page}{3}

\clearemptydoublepage
\begingroup

\renewcommand{\appendix}{%
\par
\setcounter{section}{0}%
\renewcommand{\thesection}{\thechapter.\Alph{section}}%
}

\include{paper1}

\endgroup

\begingroup

\renewcommand{\appendix}{%
\par
\setcounter{section}{0}%
\renewcommand{\thesection}{\thechapter.\Alph{section}}%
}

\include{paper2}
\endgroup

\end{document}


Here is paper 1 (article 1)...save as paper1.tex :

\chapter{\huge \bfseries My paper A}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\section{Modelling}
\subsection{Simulating}
bla bla bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla bla
bla blabla blabla blabla blabla blabla blabla bla
$$\label{eq:eqmotion} \begin{bmatrix} m & 0 \\ 0 & m \end{bmatrix} = \begin{pmatrix} F \thinspace \cos \omega_{f} \thinspace t \\ F \thinspace \sin \omega_{f} \thinspace t \end{pmatrix}$$

\section{experiment}
bla bla bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla bla
bla blabla blabla blabla blabla blabla blabla bla
$$\label{eq:eqmotion2} \begin{bmatrix} m & 0 \\ 0 & m \end{bmatrix} = \begin{pmatrix} F \thinspace \cos \omega_{f} \thinspace t \\ F \thinspace \sin \omega_{f} \thinspace t \end{pmatrix}$$

\appendix
\numberwithin{equation}{section}
\section{force coefficients}
\subsection{second force}
The coefficients of bla bla
\begin{align}
A_{0} & = 15 \nonumber  \\
B_{1} & = 1
\end{align}

\subsection{first force}
\begin{align}
A_{0} & = 14 \nonumber  \\
B_{1} & = 2
\end{align}


And paper2 (article 2).......save as paper2.tex :

\chapter{\huge \bfseries My paper B}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\numberwithin{equation}{chapter}

\section{Modelling}
\subsection{Simulating}
bla bla bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla bla
bla blabla blabla blabla blabla blabla blabla bla
$$\label{eq:beqmotion} \begin{bmatrix} m & 0 \\ 0 & m \end{bmatrix} = \begin{pmatrix} F \thinspace \cos \omega_{f} \thinspace t \\ F \thinspace \sin \omega_{f} \thinspace t \end{pmatrix}$$

\section{experiment}
bla bla bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla bla
bla blabla blabla blabla blabla blabla blabla bla
$$\label{eq:beqmotion2} \begin{bmatrix} m & 0 \\ 0 & m \end{bmatrix} = \begin{pmatrix} F \thinspace \cos \omega_{f} \thinspace t \\ F \thinspace \sin \omega_{f} \thinspace t \end{pmatrix}$$

\appendix
\numberwithin{equation}{section}
\section{force coefficients}
\subsection{second force}
The coefficients of bla bla
\begin{align}
A_{0} & = 15 \nonumber  \\
B_{1} & = 1
\end{align}

\subsection{first force}
\begin{align}
A_{0} & = 14 \nonumber  \\
B_{1} & = 2
\end{align}


On compiling the main document (with paper1.tex and paper2.tex in the same folder as main.tex), you will note that in paper B (article 2) in section B.2, the equation numbering starts as B.1 again..which is wrong. It should be B.2. What am I doing wrong?

Thanks a lot...

-
That's impossible to say unless you give is more information. How do you turn the individual articles into a single document? What document class do you use for your final thesis? If your main file is not too long (hopefully, just a few declaration and \include statements) maybe you could post it here, and we can take it from there. Do you use \renewcommand{\theequation} anywhere? –  Harald Hanche-Olsen Oct 30 '10 at 12:37
Thanks. I'm not using \renewcommand{\theequation} anywhere. I'm using the book class for the thesis. I will try to reproduce the error in a minimal document and post it here. –  yCalleecharan Oct 30 '10 at 12:40
You may also look at this post tex.stackexchange.com/questions/4451/… on how I'm putting the articles together in a single document. –  yCalleecharan Oct 30 '10 at 12:45
Not to be "that guy", but as @Harald suggested, a small working example would be highly appreciated. –  Martin Tapankov Oct 30 '10 at 13:33
By the way, thanks for providing a working example , but his one is far from minimal. For example, the fancyhdr stuff is useless here. It is important to remove as many packages as possible since problems are often caused by package interactions. Also, the contents of the equations could be shorter: 1 + 1 = 2 is a nice equation for an example. (Sorry for nitpicking, but as think the ability to make good MWE is essential in *TeX troubleshooting.) –  mpg Oct 31 '10 at 12:44

Looks like \numberwithin is not meant to be used the way you are trying to use it. (I'm surprised too!) The problem is that it essentially does two things:

1. Calls \renewcommand*\the<slave counter>{\the<master counter>.\arabic{slave counter}}
2. Calls \@addtoreset{<slave counter>}{<master counter>}

The second one is what change the numbering. Notice that it is "addtoreset". Which means that it will make the increment operation on the master counter trigger a reset of the slave counter. So after calling \numberwithin{equation}{section}, the trigger has been laid. And a second call of \numberwithin{equation}{chapter} does not remove the previous trigger. Therefore the equation counter will still be reset everytime the section counter increases.

So much for why it does what you see that it does. For a solution, clearly you must stop using the convenient \numberwithin command. A substitute is the \counterwithin and \counterwithout commands provided by the chngcntr package.

So you should define, in your preamble, the following commands:

\usepackage{chngcntr}

\newcommand*\startappendixeqnumbering{%
\counterwithout{equation}{chapter}%
\counterwithin{equation}{section}%
\renewcommand*\theequation{\thechapter.\thesection.\arabic{equation}}}
\newcommand*\startnormaleqnumbering{%
\counterwithout{equation}{section}%
\counterwithin{equation}{chapter}%
\renewcommand*\theequation{\thechapter.\arabic{equation}}}

\counterwithin{equation}{chapter}  %Set the default


And call \startappendixeqnumbering before the first appendix and \startnormaleqnumbering after the last appendix, before the start of the next chapter.

-
(And no, the counterpart to \@addtoreset is not defined for the reverse operation. There is, however, a package that provides \@removefromreset, which you could use instead.) Also, the redefinition of \theequation is not strictly necessary: \counterwithin and \counterwithout should take care of that. But in case you want numbering schemed different from the default, you know what to do. –  Willie Wong Oct 30 '10 at 21:48
Ack, of course; sorry about the two mistakes. The \$ sign was just a typo (hit shift 4 instead of shift 5), good that you caught it. The the display of the equation number, I forgot that you may have already \thesection to include the chapter number in its display. So yes, in that case you can just use \thesection.\arabic{equation}. Else you can also explicitly reference \Alph{chapter}.\Alph{section}.\arabic{equation}, but I don't see a need to do that. Cheers. –  Willie Wong Oct 31 '10 at 10:29
Great! both your answer and that of mpg work. –  yCalleecharan Oct 31 '10 at 12:29
I'm accepting your answer as it's "more" complete than the one from mpg. Both answers are right and you have both helped me to fix my problem. Wish that I could accept 2 answers! –  yCalleecharan Oct 31 '10 at 12:36

LaTeX is including the section number because you told it to do so :-) In the cited post, it appears you're using \numberwithin{equation}{section} which precisely means:

• reset equation numbering at each section;
• include the section number in the equation number.

This command is effective for the rest of the document (hence for your second included article). You can switch back to per-chapter numbering by issuing \numberwithin{equation}{chapter} just before including this article (again, this will possibly change equation numbering for all future included articles).

Warning the above solution doesn't work as advertised unless you add \@removefromreset{equation}{section} every time you use \numberwithin{equation}{chapter}. The package remreset is needed for this. (Precision added after reading the comments by Willie Wong and his answer (which works too).)

Clarification You need to make sure @ is a letter when using \@removefromreset, so the complete invocation looks like \makeatletter\@removefromreset{equation}{section}\makeatother. (Imported from the comments for the sake of readability.)

-
Correct! I use \numberwithin{equation}{section} for the Appendices. So you are right to say that this command carried on to Article 2 as well, hence the trouble. Now, it's fine again. In the minimal example that I was creating to post here, I excluded the appendices and hence the command \numberwithin{equation}{section} that I was issuing...meaning I couldn't reproduce my mistake. Thanks a lot... 1 vote up also. –  yCalleecharan Oct 30 '10 at 14:29
By the way, in this case a good minimal (not-)working example would have been enough to identify the problem. As you point out, it's not as easy as it may look. A good way to obtain such an example is the "dichotomy search": at one end, you have a small example not exhibiting the problem, at the other end your complete document. Then you need to either grow the small example until it exhibits the problem or comment things out of your big document until it works, and iterate. –  mpg Oct 30 '10 at 15:52
Thanks. Yes you are indeed right about the "dichotomy search". As the articles are quite long, I lost track that at the end of each article in the appendix section I was using \numberwithin{equation}{section}. And I made the mistake of not including the appendices in my "minimal" example. Hence it was not a good minimal as you pointed out. Thanks for your kind help. –  yCalleecharan Oct 30 '10 at 16:58
@mpg,I still have problems and I have taken away that I'm giving you marks for a good answer. U still have my 1 vote up. U have helped me but perhaps there's another problem in my code. I have put a good minimal example this time. –  yCalleecharan Oct 30 '10 at 17:49
@yCalleecharan: try following mpg's advice to the letter. Put the \numberwithin command before the \chapter command in the file for chapter 2, not after. –  Willie Wong Oct 30 '10 at 18:50