2

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.

  • 1
    When using basic (kernel) conditionals, the key here is to compare dimensions, since \cumuX contain decimals. As such, \ifdim (with some unit of measure like pt as an appendage) rather that \ifnum. – Werner Mar 17 '15 at 22:41
  • @Werner Good point. Any chance I can make you work some magic using etoolbox regarding my question, even though I've already accepted another answer? :) – Svend Tveskæg Mar 17 '15 at 22:44
3

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

2

The following is an etoolbox implimentation using

\ifdimcomp{<dimA>}{<relation>}{<dimB>}{<true>}{<false>}

quartileC: 3

\documentclass{article}
\usepackage{etoolbox}
\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}

% \ifdimcomp{<dimA>}{<relation>}{<dimB>}{<true>}{<false>}
\ifdimcomp{\cumuA pt}>{75pt}{\def\quartileC{0}}{}
\ifdimcomp{\cumuA pt}<{75pt}{\ifdimcomp{\cumuB pt}>{75pt}{\def\quartileC{1}}{}}{}
\ifdimcomp{\cumuB pt}<{75pt}{\ifdimcomp{\cumuC pt}>{75pt}{\def\quartileC{2}}{}}{}
\ifdimcomp{\cumuC pt}<{75pt}{\ifdimcomp{\cumuD pt}>{75pt}{\def\quartileC{3}}{}}{}
\ifdimcomp{\cumuD pt}<{75pt}{\ifdimcomp{\cumuE pt}>{75pt}{\def\quartileC{4}}{}}{}
\ifdimcomp{\cumuE pt}<{75pt}{\ifdimcomp{\cumuF pt}>{75pt}{\def\quartileC{5}}{}}{}
\ifdimcomp{\cumuF pt}<{75pt}{\ifdimcomp{\cumuG pt}>{75pt}{\def\quartileC{6}}{}}{}
\ifdimcomp{\cumuG pt}<{75pt}{\ifdimcomp{\cumuH pt}>{75pt}{\def\quartileC{7}}{}}{}

quartileC: \quartileC

\end{document}

Nested conditions allow for an and-like boolean operation.

2

An alteration to Ahmed's answer in Checking if two conditionals are met for numbers to make it work with this case and using "dimensions":

quartileC: 3

\documentclass{article}
\usepackage{catoptions}
\makeatletter
\def\dimtest#1and#2#{%
  \ifdim\dimexpr\cpt@dimlt\cpt@dimgt\cpt@dimeq#1%
    \cpt@removetonnil<=>\cpt@nnil\relax
    \ifdim\dimexpr\cpt@dimlt\cpt@dimgt\cpt@dimeq#2%
      \cpt@removetonnil<=>\cpt@nnil\relax
      \expandafter\expandafter\expandafter\@firstoftwo
    \else
      \expandafter\expandafter\expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

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

\dimtest \cumuA pt>75pt and 0pt=0pt {\def\quartileC{0}}{}
\dimtest \cumuA pt<75pt and 75pt<\cumuB pt{\def\quartileC{1}}{}
\dimtest \cumuB pt<75pt and 75pt<\cumuC pt{\def\quartileC{2}}{}
\dimtest \cumuC pt<75pt and 75pt<\cumuD pt{\def\quartileC{3}}{}
\dimtest \cumuD pt<75pt and 75pt<\cumuE pt{\def\quartileC{4}}{}
\dimtest \cumuE pt<75pt and 75pt<\cumuF pt{\def\quartileC{5}}{}
\dimtest \cumuF pt<75pt and 75pt<\cumuG pt{\def\quartileC{6}}{}
\dimtest \cumuG pt<75pt and 75pt<\cumuH pt{\def\quartileC{7}}{}

quartileC: \quartileC

\end{document}

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.