# Strange behaviour of ifnumequal?

If I use \cfoot to evaluate and print this section counter, it shows the correct section number in my appendix.

However, when I evaluate the section counter using \ifnumequal, it shows that the expression of equality is never met. Why?

A not so minimum example:

\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage[heightrounded]{geometry}
\usepackage[toc,titletoc,page]{appendix}
\usepackage{blindtext}

\usepackage[pageanchor]{hyperref}

\newrobustcmd{\PreviousSectionTitle}{}%
\newrobustcmd{\CurrentSectionTitle}%

\newcommand{\Contentsname}{Inhoudsopgave}

\newrobustcmd{\GotoName}{Naar}
\newrobustcmd{\DoIfNumInequal}[3]{%
\ifnumequal{#1}{#2}{}{#3}%  Drop last
}%
\newrobustcmd{\IfNumInequal}[4]{%
\ifnumequal{#1}{#2}{#3}{#4}%
}%

\newcounter{PreviousSectionCounter}% No resetting%%%
\setcounter{PreviousSectionCounter}{0}%
\newcounter{NextSectionCounter}% No resetting%%%
\setcounter{NextSectionCounter}{0}
\newcounter{CurrentSectionCounter}% No resetting%%%
\setcounter{CurrentSectionCounter}{0}
\newcounter{LastSectionValue}

\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\footrulewidth}{.4pt}
\lfoot{\CurrentSectionTitle}

}%

\AtEndDocument{%
\makeatletter
\def\LastSectionInFile{%
\immediate\write\@auxout{\string
\setcounter{LastSectionValue}{\number\value{CurrentSectionCounter}}%
}%
}%
\makeatother
}%

\newrobustcmd{\NextSectionHyperLinkFormat}[1]{\GotoName~#1~$$\rightarrow$$}%
\newrobustcmd{\PreviousSectionHyperLinkFormat}[1]{$$\leftarrow$$~\GotoName~#1}%

\begin{document}

\tableofcontents
\cleardoublepage

\newpage

\let\LaTeXStandardSection\section%
\renewcommand{\section}[2][]{%
\stepcounter{CurrentSectionCounter}%
%%%%% Check first whether there is a already a previous section
\ifnumequal{\number\value{CurrentSectionCounter}}{1}{% Yes, section number is zero
\setcounter{PreviousSectionCounter}{1}%
\expandafter\renewrobustcmd{\CurrentSectionTitle}{%
#2}%
}{%
\makeatletter
\edef\@@oldtitle{\CurrentSectionTitle}
\expandafter\renewrobustcmd{\PreviousSectionTitle}{%
\@@oldtitle
}%
\expandafter\renewrobustcmd{\CurrentSectionTitle}{%
#2%
}%
\setcounter{PreviousSectionCounter}{\number\value{CurrentSectionCounter}}
}% CurrentSectionCounter > 1
% Set Target to current section
\phantomsection
\hypertarget{CurrentSection::\number\value{CurrentSectionCounter}}{}%
%\makeatletter
\immediate\write1{%
\string\phantomsection%
\string\newlabel{CurrentSection::\number\value{CurrentSectionCounter}::Title}{{\thesection}{\thepage}{#2}}%
}%
\makeatother
% Set Link to previous section%
\setcounter{NextSectionCounter}{\number\value{CurrentSectionCounter}}
\ifnumequal{\number\value{CurrentSectionCounter}}{\number\value{LastSectionValue}}{}{%
}%
\phantomsection
\LaTeXStandardSection[#2]{#2}%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% This code is behaving weird? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\ifnumequal{\number\value{CurrentSectionCounter}}{2}{\cfoot{\number\value{CurrentSectionCounter}}}{\cfoot{Wrong number}}% This line always yields "wrong number"
\cfoot{Current section number is: \number\value{CurrentSectionCounter}}% This line displays the counter value correctly

\section{test}
\newpage
\blindtext
\newpage

\begin{appendices}

\section{Section Number A}
\blindtext
\clearpage
\section{B}
\blindtext
\clearpage
\section{Section Number C}
\blindtext
\clearpage

\section{D}
\blindtext

\end{appendices}

\end{document}

• An obvious error is in the placement of \makeatletter. Also, for having a label referring to the last section number you should use the totcount package. – egreg May 4 '14 at 23:17

\ifnumequal{\number\value{CurrentSectionCounter}}{2}{%

The comparison is done only once, at the time, when \ifnumequal is executed. Apparently the value of CurrentSectionCounter was different at this time, thus the footer is set by \cfoot{Wrong number} for all following pages.