# comparing \x and \value{section} with if

I want to evaluate whether my variable \x (that comes from a foreach loop) is the same as my current section number. I tried

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{totcount}
\regtotcounter{section}             % total amount of sections

\begin{document}

\begin{frame}
\section{This is the first section} \label{sec:this_is_the_first_section}
Some Text.
\section{This is the second section} \label{sec:this_is_the_second_section}
Some Text.
\section{This is the third section} \label{sec:this_is_the_third_section}
Some Text.

\foreach \x in {1,...,\totvalue{section}}{
\arabic{section} =  \x
\ifx\x\value{section}
check
\else
no
\fi}
\end{frame}

\end{document}


but this always returns a "no" where in the last case it should return a "check". Do I have to use a different form of the "if"-case (i tried

   \ifnum{\value{\x}}={\value{section}}


but that didnt even compile) or am I trying to compare different variable types that can't be compared like that? What would be the correct commandline to compare the values?

• \ifnum\x=\value{section}  is more correct (with a space at the end). (Note this doesn't answer any part of the question, it's just a suggestion.) – Manuel Sep 7 '17 at 15:48
• I would simply do \ifnum\x=\thesection, works fine with beamer, see for example tex.stackexchange.com/a/313579/36296 for an application. – user36296 Sep 7 '17 at 15:48
• @Manuel there is no need for space at end, because \value{section} gives a TeX count register. So the space token is not swallowed by the \ifnum test and depending on context may actually show in output. – user4686 Sep 7 '17 at 15:51
• @samcarter: \thesection can expand to something like A or A.2 etc. In this case \ifnum would be useless with \thesection. Don't trust \the... macros expanding to literal numbers – user31729 Sep 7 '17 at 15:51
• @Manuel I think that's exactly the answer to both parts of the question:-) – David Carlisle Sep 7 '17 at 15:52

You are not using \ifnum correctly. It might work, but depending on the situation, it might not work. The correct usage is

\ifnum\x=\value{section}


that way the first number \x is delimited by the = and the second number \value{section} doesn't need to be delimited because it gets an internal TeX count.

• Thx, that did the job. – Blubberblase Sep 8 '17 at 7:24

Coming from different programming languages to TeX can be a tough experience, because conditionals in TeX are a bit weird.

If you want to compare two integers, the conditional to use is \ifnum that wants a syntax

\ifnum<integer><relation><integer>


without braces around the integers, so your test should be

\ifnum\x=\value{section}


(don't forget a trailing % if you don't want a space from the newline character).

Note that \value is not a general purpose ”extractor“, but a very specific LaTeX macro which only works if its argument is a LaTeX counter defined with \newcounter, whose expansion is the ”abstract” value.

Similarly, \arabic is a LaTeX macro for accessing the decimal representation of the counter; it might be used here, but not recommended for several reasons (termination of the <integer> specification is the main one).

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{totcount}
\regtotcounter{section}             % total amount of sections

\begin{document}

\begin{frame}

\section{This is the first section} \label{sec:this_is_the_first_section}
Some Text.

\section{This is the second section} \label{sec:this_is_the_second_section}

Some Text.

\section{This is the third section} \label{sec:this_is_the_third_section}
Some Text.

\foreach \x in {1,...,\totvalue{section}}{%
\ifnum\x=\value{section}%
check
\else
no
\fi
}

\end{frame}

\end{document}


Note the % I added; the lines with check and no will add a space, because the end-of-line is not masked off. The last space will vanish as it's at the end of a paragraph.