7

I'd like to test the value of counters (preferably as vanilla TeX as it gets).

\documentclass{article}
\newcounter{counterA}
\newcounter{counterB}
\setcounter{counterA}{2}
\setcounter{counterB}{2}

\newcommand\maccommand{
  \ifx\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

Yet in TeX you can write:

\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
}

Why can't you write

\ifx\counterA=2 
  Just so you know, counterA is equal to 2.
\else 
  Just so you know, counterA is not equal to 2.
\fi
}

OR

\ifx\counterA=\counterB 
  Just so you know, counterA is equal to counterB.
\else 
  Just so you know, counterA is not equal to counterB.
\fi
}
  • related tex.stackexchange.com/q/51135 – LaRiFaRi Jan 12 '16 at 15:01
  • not remarking on the tex code, but the logic -- if \counterA=2 then it's absurd to report that "counterA is greater than 2." (this is such a non sequitur that it's distracting me from the question itself ... i get distracted easily.) – barbara beeton Jan 12 '16 at 15:32
  • @barbarabeeton Haha, very true! I copied the code from the last block without thinking about the text. Fixed :) – Jonathan Komar Jan 12 '16 at 15:34
6

\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.

  • Why the \renewcommand? – Alenanno Jan 12 '16 at 15:19
  • @Herbert He is demonstrating that the macro \counterA does not work if renewed. You need to use \value{counterA}. Quite an important detail Herbert! Thanks. – Jonathan Komar Jan 12 '16 at 15:20
  • @Herbert But why does \ifx \somecounter>2 \else \fi work? – Jonathan Komar Jan 12 '16 at 16:01
  • no, it doesn't work: \newcount\foo \foo=0 foo is \ifx\the\foo<2 less \else greater than \fi 2 \ifx compares \the and \foo: And in your case it compares \somecounter with > – user2478 Jan 12 '16 at 16:13
5

Use \ifnum instead of \ifx.

enter image description here

\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}
  • @LaRiFaRi Good point, I'll add that. – Alenanno Jan 12 '16 at 15:03
  • @LaRiFaRi Although the previous solution was good in case you want to add arguments. – Alenanno Jan 12 '16 at 15:05
  • You answered the question correctly, I would just like to clear some things up. If you have time, maybe have a look at the edits to my question. – Jonathan Komar Jan 12 '16 at 15:13
  • @macmadness86 What about the edit? – Alenanno Jan 12 '16 at 15:18
  • @macmadness86 I think your edits belong on a new question though. – Alenanno Jan 12 '16 at 15:22
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.