2

I've been trying to google and this seems like a silly thing that should be obvious, but everywhere I look is answering how to use counter values inside a conditional statement, which I can do fine.

Basically I want to set a counter value, but inside the section where the value goes, put a if/then/else hierarchy. I know I can do it by reversing the order (putting an if/then/else hierarchy and in each result putting a new set-count command) but for both ease of reading and some programming structure reasons I'd rather have it the other way.

Basically I want to do something like the following;

\newcounter{TestCounter}
\setcounter{TestCounter}
{
\ifthenelse{5 > 6}{7}{2}
}

Is such a thing possible, or do I need to reorder it the other way?

Thanks!

For a more explicit MWE:

I want to use the following;

\documentclass{article}
\usepackage{xifthen}
\begin{document}

\newcounter{TestCounter}
\setcounter{TestCounter}{
\ifthenelse{5 > 6}{7}{2}}
\end{document}

Where (ideally) the above would result in assigning to the count "TestCounter" the value "2" (since 5 < 6).

In practice I would be using more complicated code, but it would all resolve to checking to see if one number is bigger than another, where one of those would be stored in a counter. For example

\documentclass{article}
\usepackage{xifthen}
\begin{document}

\newcounter{Temp}
\newcounter{TestCounter}
\setcounter{Temp}{5}
\setcounter{TestCounter}{
     \ifthenelse{\arabic{Temp} > 2}{4}{7}
}
\end{document}
2
  • First you should provide us a MWE. See if tex.stackexchange.com/questions/61598/… helps you.
    – cacamailg
    Apr 26 '16 at 17:37
  • Thought I had provided enough, but I have expanded it to have the pseudo-code I am after, with explanation of exactly how it should work. Let me know if you want more.
    – Jason
    Apr 26 '16 at 17:44
5

\ifthenelse is not expandable and can therefore not be used inside the arguments of \setcounter. However, the TeX primitive \ifnum is expandable:

\setcounter{TestCounter}{%
  \ifnum 5>6 % space ends number 6
    7%
  \else
    2%
  \fi
}

\arabic should not be used in numeric contexts, because it could contain formatting stuff like font settings. The value of a counter is accessable by \value{countername}:

\setcounter{TestCounter}{%
  \ifnum\value{Temp}>2 % space ends number 2
    4%
  \else
    7%
  \fi
}
2
  • Exactly the information I was looking for, thanks!
    – Jason
    Apr 26 '16 at 18:39
  • As a followup, my \arabic was entirely lazy and you bring up a good point. Would using \theTemp be a good way to pull the value, or does that possibly have formatting attached as well?
    – Jason
    Apr 26 '16 at 18:41
2

A simple wrapper may work here (at least for this simple example)

\documentclass{article}
%\usepackage{xifthen}


\newcommand{\ifelse}[3]{%
  \ifnum#1  
  #2%
  \else
  #3%
  \fi
}

\begin{document}


\newcounter{TestCounter}
\setcounter{TestCounter}{\ifelse{5 > 6}{7}{2}}


\theTestCounter


\setcounter{TestCounter}{\ifelse{6 > 5}{7}{2}}

\theTestCounter


\end{document}
1

Why not?

\documentclass{article}

\newcounter{Temp}
\newcounter{TestCounter}
\setcounter{Temp}{5}

\begin{document}

\ifnum\theTemp>2
    \setcounter{TestCounter}{4}
\else
    \setcounter{TestCounter}{7}
\fi

\theTemp
\theTestCounter

\end{document}
2
  • Like I said, I know I can do it this way, and I may end up doing so if I haven't other choices, but it would be more tractable code if I could do it the other way, which is why I was asking if it was possible.
    – Jason
    Apr 26 '16 at 17:53
  • 1
    @Jason I do not understand what you mean by tractable. You can simply define a command with \ifnum...\fi and use that command anywhere in the text.
    – cacamailg
    Apr 26 '16 at 18:01
1

You can do it with a different set of “if-then-else” macros, for instance those provided by etoolbox or by expl3.

etoolbox

Do \usepackage{etoolbox} in the preamble and use

\newcounter{TestCounter}
\setcounter{TestCounter}{\ifnumcomp{5}{>}{6}{7}{2}}

The relation symbol in between can be <, = or >.

expl3

Do \usepackage{expl3} in the preamble, along with

\ExplSyntaxOn
\cs_set_eq:NN \intcompare \int_compare:nTF
\ExplSyntaxOff

in order to have a “user level version” of the programming level function. Then

\newcounter{TestCounter}
\setcounter{TestCounter}{\intcompare{5 > 6}{7}{2}}

will do what you want.

With this version you can use (A and B represent integers, which can also be \value{<counter>})

\intcompare{A < B}{true}{false}
\intcompare{A <= B}{true}{false}
\intcompare{A > B}{true}{false}
\intcompare{A >= B}{true}{false}
\intcompare{A = B}{true}{false}
\intcompare{A != B}{true}{false}

with obvious meanings; = can also be ==.

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.