How can I test the value of two counters?
\thecounterA
works only if the counter wasn't redefined:
\documentclass{article}
\newcounter{counterA}\renewcommand\thecounterA{A\arabic{counterA}}
\newcounter{counterB}\renewcommand\thecounterB{B\arabic{counterB}}
\setcounter{counterA}{2}
\setcounter{counterB}{2}
\newcommand\maccommand{%
\ifnum\value{counterA}=\value{counterB}
Just so you know, counterA (= \thecounterA) holds the same value as
counterB (=\thecounterB).%
\else
Sorry, but the counterA (= \thecounterA) is not equal to counterB
(=\thecounterB).%
\fi}
\begin{document}
\maccommand
\stepcounter{counterA}
\maccommand
\end{document}
The TeX way:
\makeatletter
\newcommand\maccommand{%
\ifnum\c@counterA=\c@counterB
Just so you know, counterA (= \thecounterA) holds the same value as
counterB (=\thecounterB).%
\else
Sorry, but the counterA (= \thecounterA) is not equal to counterB
(=\thecounterB).%
\fi}
\makeatother
Internally \c@counterA
holds the value and \thecounterA
is the representation. On LaTeX level \c@counterA
shouldn't be used.
Use \ifnum
instead of \ifx
.
\documentclass{article}
\newcounter{counterA}
\newcounter{counterB}
\setcounter{counterA}{2}
\setcounter{counterB}{2}
\newcommand\maccommand{%
\ifnum\the\value{counterA}=\the\value{counterB}
Just so you know, counterA (= \the\value{counterA}) holds the same value as counterB (= \the\value{counterB}).%
\else
Sorry, but the counterA (= \the\value{counterA}) is not equal to counterB (= \the\value{counterB}).%
\fi}
\begin{document}
\maccommand
\stepcounter{counterA}
\maccommand
\end{document}
You can certainly do, in plain TeX,
\newcount\counterA
\counterA=2
\ifx\counterA>2
Just so you know, counterA is greater to than 2.
\else
Just so you know, counterA is not greater than 2.
\fi
but this will compare the token \counterA
with the token >
and find they've different meanings, so everything up to \else
will be gobbled and you'll get
Just so you know, counterA is not greater than 2.
But if you now try adding
\counterA=1000
\ifx\counterA>2
Just so you know, counterA is greater to than 2.
\else
Just so you know, counterA is not greater than 2.
\fi
}
you'll get exactly the same.
For numeric tests you have to use \ifnum
; the code
\newcount\counterA
\counterA=2
\ifnum\counterA>2
Just so you know, counterA is greater to than 2.
\else
Just so you know, counterA is not greater than 2.
\fi
\counterA=1000
\ifnum\counterA>2
Just so you know, counterA is greater to than 2.
\else
Just so you know, counterA is not greater than 2.
\fi
}
will produce
Just so you know, counterA is not greater than 2.
Just so you know, counterA is greater than 2.
The conditional \ifx
compares the meaning of the two tokens that follow, without doing any expansion on them.
If the first or second token after \ifx
is either \else
or \fi
, TeX considers the code to be an incomplete conditionals and adds one or two “frozen \relax
” tokens, as need be.
Note, conversely, that \ifnum
performs expansion, because it needs to find a <number>
followed by a <relation>
(one among =
12, <
12 or >
12) and another <number>
.
Since in LaTeX the instruction \newcounter{counterA}
performs also \newcount\c@counterA
and the macro \value
is defined by
% latex.ltx, line 2084:
\def\value#1{\csname c@#1\endcsname}
Therefore, doing
\ifnum\value{counterA}>\value{counterB}
will provide the appropriate tokens for the test. Hence your code could be
\newcommand\maccommand{%
\ifnum\value{counterA}=\value{counterB}% <--- don't forget this!
Just so you know, counterA (= \thecounterA) holds the same value as counterB (= \thecounterB).%
\else
Sorry, but the counterA (= \thecounterA) is not equal to counterB (= \thecounterB).%
\fi
}
Note that \the\value{counterA}
is not needed. However, a %
after \value{counterB}
is needed, because \c@counterB
is a counter register, so TeX doesn't look for an <optional space>
after it. This <optional space>
would be looked for and gobbled in case you use \the\value{counterB}
, but this method is conceptually wrong.
In the body of the macro I used \thecounterA
and \thecounterB
, which expand to the current representation of the counter's values (default, decimal number). It would be wrong to use \thecounterA
in the numeric test for \ifnum
.