2

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?

  • 2
    \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 07 '17 at 15:48
  • I would simply do \ifnum\x=\thesection, works fine with beamer, see for example https://tex.stackexchange.com/a/313579/36296 for an application. – samcarter_is_at_topanswers.xyz Sep 07 '17 at 15:48
  • 1
    @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. –  Sep 07 '17 at 15:51
  • 1
    @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 –  Sep 07 '17 at 15:51
  • @Manuel I think that's exactly the answer to both parts of the question:-) – David Carlisle Sep 07 '17 at 15:52
  • @ChristianHupfer All standard beamer themes use numerical representations - so one normally gets away with this ... – samcarter_is_at_topanswers.xyz Sep 07 '17 at 15:53
  • @samcarter... for standard ones yes... I don't trust it, however –  Sep 07 '17 at 15:54
  • @DavidCarlisle If it's true, please, confirm. I really did not read the question :) – Manuel Sep 07 '17 at 15:55
  • @ChristianHupfer Somewhere the supply of new questions for the counter wizard has to come from :) – samcarter_is_at_topanswers.xyz Sep 07 '17 at 15:55
  • @samcarter: Yes, but there's nothing to answer for me here ... it has been answered already in comments. –  Sep 07 '17 at 15:57
  • @ChristianHupfer I prefer not to answer, I really did not read the question. You can answer yourself; or may be jfbu (who corrected me). – Manuel Sep 07 '17 at 15:58
  • @ChristianHupfer Not in this questions, but with my careless usage of \the... it will break somewhere for someone and the user will come back to this site and ask a questions you can answer. – samcarter_is_at_topanswers.xyz Sep 07 '17 at 16:00
  • @Manuel thx, your suggestion was the solution to my proplem. So, '\ifnum\x=\value{section} ' did the job. – Blubberblase Sep 08 '17 at 06:32

2 Answers2

2

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.

Manuel
  • 27,118
0

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.

enter image description here

egreg
  • 1,121,712