第一的:我发现“检查两个条件是否满足“但我不知道如何改变它才能满足我的需要。
通缉:我有八个不同的预定义值,并且我想要根据这八个值对常数进行不同的定义。
\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}
我猜可能是etoolbox
是这里的解决方法,但是(如前所述)我不知道如何正确地使用它来完成这项任务。
答案1
在此更新中,我添加了第二个简单的无包方法。与其他答案一样,我自然会使用它,\ifdim
因为我需要比较定点数。
首先让我指出,你的条件应该读作\cumuA >= 75
,然后\cumuA<75<=\cumuB
然后\cumuB<75<=\cumuC
等等......(即不仅仅是严格的不平等)。
请注意,TeX
没有直接的测试条件>=
或<=
:程序员应该分别否定测试结果<
或>
感谢\else
。或者有\unless
来自e-TeX(我的答案中没有使用)。
在这两种方法中,我都安排事情在每一步只测试一个不等式。第一种方法,最简单的方法:
\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}
第二种方法使用了一些从代码中借用的分隔宏xint
。
\long\def\DOTHIS #1#2\ORTHAT #3{\fi #1}%
\makeatletter
\let\ORTHAT\@firstofone
\makeatother
第二种方法的优点是,如果需要,还可以轻松定义一个可扩展的单参数宏,\mypercentile
该宏将用作
\edef\quartileC {\mypercentile{75}}
例如。这是在底部添加的。
为此,我需要
pt
在第一个发布的版本中的每个后面添加空格,因为\relax
如果没有,TeX 会插入一个不可扩展的标记。(这不会改变原来的功能,但如果想要这个功能,就必须这样做\mypercentile
)。可扩展性是最强的(
\romannumeral-`0
将完全扩展\mypercentile
)。
代码:
\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}
\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}}
答案2
以下是etoolbox
使用实现
\ifdimcomp{<dimA>}{<relation>}{<dimB>}{<true>}{<false>}
四分位数C: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}
嵌套条件允许进行and
类似布尔运算。
答案3
修改艾哈迈德的回答在检查两个条件是否满足对于数字,使其适用于这种情况并使用“尺寸”:
四分位数C: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}