[Tex/LaTex] Multiple \if condition using etoolbox

conditionalsetoolbox

First: I've found "Checking if two conditionals are met" but I don't know how to change it do suit my needs.

Wanted: I have eight different predefined values and I want different definitions of a constant dependnig on the eight values.

wanted

\documentclass{article}

\usepackage{etoolbox}
\usepackage{verbatim}

\begin{document}

\noindent Define the following:
\begin{verbatim}
\def\cumuA{33.3}
\def\cumuB{59.5}
\def\cumuC{73.8}
\def\cumuD{83.3}
\def\cumuE{88.1}
\def\cumuF{90.5}
\def\cumuG{97.6}
\def\cumuH{100}
\end{verbatim}
What I want:
\begin{verbatim}
\cumuA > 75         : \def\quartileC{0}
\cumuA < 75 < \cumuB: \def\quartileC{1}
\cumuB < 75 < \cumuC: \def\quartileC{2}
\cumuC < 75 < \cumuD: \def\quartileC{3}
\cumuD < 75 < \cumuE: \def\quartileC{4}
\cumuE < 75 < \cumuF: \def\quartileC{5}
\cumuF < 75 < \cumuG: \def\quartileC{6}
\cumuG < 75 < \cumuH: \def\quartileC{7}
\end{verbatim}
This should then result in
\begin{verbatim}
\def\quartileC{3}
\end{verbatim}

\end{document}

I guess that etoolbox is the way to go here but (as said before) I don't know how use it properly for this task.

Best Answer

In this update I add a second simple no package method. As in the other answers naturally I use \ifdim because I need to compare fixed point numbers.

Let me first point out that your conditions should read \cumuA >= 75, then \cumuA<75<=\cumuB then \cumuB<75<=\cumuC etc... (i.e. not only strict inequalities).

Notice that TeX has no direct proviso for testing >= or <= : the programmer is suppose to negate the result of testing, respectively < or > thanks to the \else. Or there is \unless from e-TeX (not used in my answer).

In both approaches I arrange things to test only one inequality at each step. First approach, the most simple minded:

\documentclass{article}

\begin{document}

\def\cumuA{33.3}
\def\cumuB{59.5}
\def\cumuC{73.8}
\def\cumuD{83.3}
\def\cumuE{88.1}
\def\cumuF{90.5}
\def\cumuG{97.6}
\def\cumuH{100}

\def\quartileC{7}%
\ifdim 75pt>\cumuG pt\else\def\quartileC{6}\fi
\ifdim 75pt>\cumuF pt\else\def\quartileC{5}\fi
\ifdim 75pt>\cumuE pt\else\def\quartileC{4}\fi
\ifdim 75pt>\cumuD pt\else\def\quartileC{3}\fi
\ifdim 75pt>\cumuC pt\else\def\quartileC{2}\fi
\ifdim 75pt>\cumuB pt\else\def\quartileC{1}\fi
\ifdim 75pt>\cumuA pt\else\def\quartileC{0}\fi

quartileC: \quartileC
\end{document}

image

The second approach uses some delimited macros borrowed from code of xint.

\long\def\DOTHIS #1#2\ORTHAT #3{\fi #1}%
\makeatletter
\let\ORTHAT\@firstofone
\makeatother

The advantage of the second approach is to allow also, if desired, the easy definition of an expandable one-parameter macro \mypercentile which will be used as

\edef\quartileC {\mypercentile{75}}

for example. This is added at bottom.

  1. for this I needed to add spaces after each pt from the first posted version, as TeX inserts a non expandable \relax token if not. (This changed nothing to the original functioning, but has to be done if one wants the \mypercentile thing).

  2. the expandability is in the strongest sense (\romannumeral-`0 will fully expand \mypercentile).

The code:

\documentclass{article}

\usepackage{verbatim}

% MACROS TAKEN WITH NEW NAMES FROM XINT CODE BY JFBU
\long\def\DOTHIS #1#2\ORTHAT #3{\fi #1}%
\makeatletter
\let\ORTHAT\@firstofone
\makeatother

\begin{document}
\noindent Define the following:
\begin{verbatim}
\def\cumuA{33.3}
\def\cumuB{59.5}
\def\cumuC{73.8}
\def\cumuD{83.3}
\def\cumuE{88.1}
\def\cumuF{90.5}
\def\cumuG{97.6}
\def\cumuH{100}
\end{verbatim}
\def\cumuA{33.3}
\def\cumuB{59.5}
\def\cumuC{73.8}
\def\cumuD{83.3}
\def\cumuE{88.1}
\def\cumuF{90.5}
\def\cumuG{97.6}
\def\cumuH{100}

What I want:
\begin{verbatim}
\cumuA > 75         : \def\quartileC{0}% I meant \cumuA>=75 here
\cumuA < 75 < \cumuB: \def\quartileC{1}% I want  \cumuA < 75 <= \cumuB
\cumuB < 75 < \cumuC: \def\quartileC{2}% rather: \cumuB < 75 <= \cumuC
\cumuC < 75 < \cumuD: \def\quartileC{3}
\cumuD < 75 < \cumuE: \def\quartileC{4}
\cumuE < 75 < \cumuF: \def\quartileC{5}
\cumuF < 75 < \cumuG: \def\quartileC{6}
\cumuG < 75 < \cumuH: \def\quartileC{7}
\end{verbatim}
This should then result in
\begin{verbatim}
\def\quartileC{3}
\end{verbatim}

I do:
\begin{verbatim}
\ifdim 75pt>\cumuA pt \else\DOTHIS{\def\quartileC{0}}\fi
\ifdim 75pt>\cumuB pt \else\DOTHIS{\def\quartileC{1}}\fi
\ifdim 75pt>\cumuC pt \else\DOTHIS{\def\quartileC{2}}\fi
\ifdim 75pt>\cumuD pt \else\DOTHIS{\def\quartileC{3}}\fi
\ifdim 75pt>\cumuE pt \else\DOTHIS{\def\quartileC{4}}\fi
\ifdim 75pt>\cumuF pt \else\DOTHIS{\def\quartileC{5}}\fi
\ifdim 75pt>\cumuG pt \else\DOTHIS{\def\quartileC{6}}\fi
\ORTHAT{\def\quartileC{7}}%
\end{verbatim}

(The spaces after ``pt'' were added in an edit; they do not matter here but
are useful for expandable wrapper \string\mypercentile).
% EDIT ADDS SPACE AFTER pt TO AVOID TEX INSERTION OF \relax
% (which does not matter here but does in the expanable wrapper
% \mypercentile next)

\ifdim 75pt>\cumuA pt \else\DOTHIS{\def\quartileC{0}}\fi
\ifdim 75pt>\cumuB pt \else\DOTHIS{\def\quartileC{1}}\fi
\ifdim 75pt>\cumuC pt \else\DOTHIS{\def\quartileC{2}}\fi
\ifdim 75pt>\cumuD pt \else\DOTHIS{\def\quartileC{3}}\fi
\ifdim 75pt>\cumuE pt \else\DOTHIS{\def\quartileC{4}}\fi
\ifdim 75pt>\cumuF pt \else\DOTHIS{\def\quartileC{5}}\fi
\ifdim 75pt>\cumuG pt \else\DOTHIS{\def\quartileC{6}}\fi
\ORTHAT{\def\quartileC{7}}%

and I obtain that \texttt{\string\quartileC} has meaning
\texttt{\meaning\quartileC}.


\clearpage

\def\mypercentile #1{%
\ifdim #1pt>\cumuA pt \else\DOTHIS{0}\fi
\ifdim #1pt>\cumuB pt \else\DOTHIS{1}\fi
\ifdim #1pt>\cumuC pt \else\DOTHIS{2}\fi
\ifdim #1pt>\cumuD pt \else\DOTHIS{3}\fi
\ifdim #1pt>\cumuE pt \else\DOTHIS{4}\fi
\ifdim #1pt>\cumuF pt \else\DOTHIS{5}\fi
\ifdim #1pt>\cumuG pt \else\DOTHIS{6}\fi
\ORTHAT{7}%
}

\edef\quartileC{\mypercentile{75}}
\edef\quartileB{\mypercentile{50}}
\edef\quartileA{\mypercentile{25}}

\ttfamily

\string\quartileC{} has meaning \meaning\quartileC.

\string\quartileB{} has meaning \meaning\quartileB.

\string\quartileA{} has meaning \meaning\quartileA.

\end{document}

page 1

\def\mypercentile #1{%
\ifdim #1pt>\cumuA pt \else\DOTHIS{0}\fi
\ifdim #1pt>\cumuB pt \else\DOTHIS{1}\fi
\ifdim #1pt>\cumuC pt \else\DOTHIS{2}\fi
\ifdim #1pt>\cumuD pt \else\DOTHIS{3}\fi
\ifdim #1pt>\cumuE pt \else\DOTHIS{4}\fi
\ifdim #1pt>\cumuF pt \else\DOTHIS{5}\fi
\ifdim #1pt>\cumuG pt \else\DOTHIS{6}\fi
\ORTHAT{7}%
}

\edef\quartileC{\mypercentile{75}}
\edef\quartileB{\mypercentile{50}}
\edef\quartileA{\mypercentile{25}}

page 2