如何在 Latex 中实现以下内容:我希望有一个预定义变量(在序言中),我们称之为 A。
然后我希望有一个像这样的命令
\TwoTexts{Text1}{Text2}
其执行方式如下:如果 A=1,则显示 Text1;如果 A=2,则显示 Text2。
有什么想法吗?谢谢!
答案1
\Amacro
这是带有宏或计数器的版本acntr
。我个人更喜欢计数器版本,因为它不需要重新定义\Amacro
。
否则测试1
通过\ifnum...\fi
这里我假设 之外的任何值1
都会分支到Text2
。
\documentclass{article}
\newcommand{\Amacro}{1}
\newcommand{\TwoTexts}[2]{%
\ifnum\Amacro=1\relax%
#1%
\else
#2%
\fi
}
\newcounter{acntr}
\newcommand{\TwoTextsImproved}[2]{%
\ifnum\value{acntr}=1\relax
#1%
\else
#2%
\fi
}
\begin{document}
\setcounter{acntr}{1}
\TwoTexts{You should see this}{Nope, not this}
\renewcommand{\Amacro}{2}
\TwoTexts{You should see this}{Nope, this is it}
\TwoTextsImproved{You should see this}{Nope, not this}
\stepcounter{acntr}
\TwoTextsImproved{You should see this}{Nope, this is it}
\end{document}
答案2
为了防止出现不平衡的问题\if..
.. \else
..\fi
我会在\Firstoftwo
/方面采取一些措施\Secondoftwo
:
\documentclass{article}
\newcommand\Firstoftwo[2]{#1}%
\newcommand\Secondoftwo[2]{#2}%
\newcommand\Amacro{1}%
\newcommand\setAmacro[1]{\renewcommand\Amacro{#1}}%
\newcommand{\TwoTexts}{%
\ifnum\Amacro>1 %
\expandafter\Secondoftwo
\else
\expandafter\Firstoftwo
\fi
}
\begin{document}
\setAmacro{-3}
\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
{Text in case the value in \texttt{\string\Amacro} is larger than 1}%
\setAmacro{0}
\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
{Text in case the value in \texttt{\string\Amacro} is larger than 1}%
\setAmacro{1}
\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
{Text in case the value in \texttt{\string\Amacro} is larger than 1}%
\setAmacro{2}
\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
{Text in case the value in \texttt{\string\Amacro} is larger than 1}%
\setAmacro{17}
\texttt{\string\Amacro} is \Amacro.\\
\TwoTexts{Text in case the value in \texttt{\string\Amacro} is smaller than 1 or equal to 1}%
{Text in case the value in \texttt{\string\Amacro} is larger than 1}%
\end{document}
顺便说一句:几年前我编写了一个例程\UD@KeepKthOfLArguments
,它允许您从任意数量的未限定参数中选择一个任意的未限定参数,由于递归,它不受通常对 TeX 宏编程施加的 9 个参数限制的限制。
您可以使用该例程创建另一个例程,\choosetext
该例程根据某些变量/占位符/宏/寄存器/等等中保存的数值选择包含文本短语的未限定参数\count
。
\documentclass{article}
\makeatletter
%%=========================================================================
%% Paraphernalia:
%% \UD@firstoftwo, \UD@secondoftwo, \UD@PassFirstToSecond
%%.........................................................................
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
%%-------------------------------------------------------------------------
%% Check whether argument is empty:
%%.........................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
%% A concern in his posting is that the argument is hit with \string
%% after some expansions which in edge cases might result in unbalancing
%% surrounding \if..\fi-constructs if the macro is used inside of such
%% \if..\fi-constructs.
%%
%% That challenging concern sickened me. ;-)
%%
%% Therefore I decided to implerment a variant where this cannot happen
%% as expansion is forced by \romannumeral:
%%
%% After the first expansion-step, \string is not applied yet.
%% After the second expansion-step, any possibly disturbing remainders
%% are already removed due to \romannumeral-expansion.
%%
%% No eTeX- or whatsoever extensions. No \if.. .Only \romannumeral,
%% digit 0, space token for terminating \romannumeral-expansion,
%% \string, \expandafter, \UD@firstoftwo, \UD@secondoftwo, {, }.
%%
%% May 20, 2016
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
\UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-------------------------------------------------------------------------
%% Keep only the K-th of L consecutive undelimited arguments.
%% ( IF K < 1 OR K > L just remove L consecutive undelimited arguments. )
%%.........................................................................
%% \UD@KeepKthOfLArguments{<integer number K>}%
%% {<integer number L>}%
%% {<Tokens to insert before K-th argument>}%
%% <L consecutive undelimited arguments>
%%
%% If K >= 1 and K <= L yields:
%% <Tokens to insert before K-th argument><K-th undelimited argument>
%% If K < 1 or K > L
%% (-> there is no K-th argument in the set
%% of <L consecutive undelimited arguments> )
%% yields:
%% <Tokens to insert before K-th argument>
%%
%% Examples:
%%
%% X\UD@KeepKthOfLArguments{3}{7}{<Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X<Tokens to insert>CX
%%
%% X\UD@KeepKthOfLArguments{5}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\UD@KeepKthOfLArguments{0}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\romannumeral0%
%% \UD@KeepKthOfLArguments{3}{7}{ <Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X\romannumeral0 <Tokens to insert>CX
%% yields: X<Tokens to insert>CX
%% In case of embedding the whole thing in other expansion-contexts,
%% you need to have "hit" \romannumeral by only one \expandafter-chain for
%% obtaining the result...
%%
%% January 17, 2005
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@KeepKthOfLArguments[2]{%
\expandafter\UD@KeepKthOfLArgumentsA
\expandafter{\romannumeral\number\number#1 000\expandafter}%
\expandafter{\romannumeral\number\number#2 000}%
}%
\newcommand\UD@KeepKthOfLArgumentsA[3]{%
\UD@CheckWhetherNull{#1}{%
\UD@RemoveNArguments{#2}{#3}{}%
}{%
\expandafter\UD@PassFirstToSecond
\expandafter{%
\UD@firstoftwo{}#1}{\UD@KeepKthOfLArgumentsB{#1}{#2}}{#2}{#3}%
}%
}%
\newcommand\UD@KeepKthOfLArgumentsB[5]{%
\UD@CheckWhetherNull{#1}{%
\UD@RemoveNArguments{#3}{\UD@RemoveNArguments{#2}{#5}}{}%
}{%
\UD@CheckWhetherNull{#2}{%
\UD@RemoveNArguments{#4}{#5}{}%
}{%
\expandafter\UD@PassFirstToSecond
\expandafter{%
\UD@firstoftwo{}#2}{\expandafter\UD@KeepKthOfLArgumentsB
\expandafter{%
\UD@firstoftwo{}#1}}{#3}{#4}{#5}%
}%
}%
}%
\newcommand\UD@RemoveNArguments[3]{%
\UD@CheckWhetherNull{#1}{#2#3}{%
\UD@firstoftwo{%
\expandafter\UD@RemoveNArguments
\expandafter{\UD@firstoftwo{}#1}{#2}{#3}%
}%
}%
}%
%%-------------------------------------------------------------------------
%% A demo-routine:
%%
%% \choosetext{<number>}%
%% {<Text in case number < 1>}%
%% {<Text in case number = 1>}%
%% {<Text in case number = 2>}%
%% {<Text in case number = 3>}%
%% {<Text in case number = 4>}%
%% {<Text in case number = 5>}%
%% {<Text in case number = 6>}%
%% {<Text in case number = 7>}%
%% {<Text in case number = 8>}%
%% {<Text in case number = 9>}%
%% {<Text in case number = 10>}%
%% {<Text in case number = 11>}%
%% {<Text in case number = 12>}%
%% {<Text in case number = 13>}%
%% {<Text in case number = 14>}%
%% {<Text in case number > 14>}%
%%
%% The <number>-argument can hold anything that _expands_ to the components
%% of a quantity that is subsumable under the term <number> which is
%% explained in Donald E. Knuth's The TeXbook, Chapter 24: Summary of
%% Vertical Mode.
%%
%% Therefore <number> can as well be a macro-token expanding to a digit-
%% sequence, or a \the-call to a \count-register.
%%
%% January 24, 2017
%%
%% Ulrich Diez (e-mail: [email protected])
%%
%%.........................................................................
\newcommand\choosetext[1]{%
\romannumeral0%
\UD@KeepKthOfLArguments{\ifcase\expandafter\UD@firstoftwo
\expandafter{\number\ifnum#1<1 0\else#1\fi}{} %
1\or2\or3\or4\or5\or6\or7\or8\or
9\or10\or11\or12\or13\or14\or15\else16\fi}%
{16}%
{ }%
}%
%%-------------------------------------------------------------------------
%% A "variable" \Amacro and the routine \setAmacro for setting it's value.
%%.........................................................................
\newcommand\Amacro{1}%
\newcommand\setAmacro[1]{\renewcommand\Amacro{#1}}%
\makeatother
\begin{document}
The \verb|<number>|-argument of \verb|\choosetext| can hold anything that
expands to the components of a quantity that is subsumable under the term
\verb|<number>| which is explained in Donald E. Knuth's The TeXbook,
Chapter 24: Summary of Vertical Mode.
\bigskip
Thus the \verb|<number>|-argument of \verb|\choosetext| can hold a sequence
of digits, e.g., the digit \verb|5|:
\bigskip
\choosetext{5}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\bigskip
The \verb|<number>|-argument of \verb|\choosetext| can as well hold a
macro-token which serves as variable/placeholder for numerical values.
\bigskip
\setAmacro{17}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{15}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{14}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{13}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{12}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{11}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{10}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{9}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{8}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{7}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{6}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{5}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{4}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{3}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{2}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{0}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{-1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\end{document}
答案3
身为A
你的“多变的”)是一个常数,你可以定义\TwoTexts
:
\newcommand\TwoTexts[2]{#1} % case A = 1
% \newcommand\TwoTexts[2]{#2} % case A = 2
要选择的值A
,请取消注释正确的行。
答案4
我只是增强了我的\choosetext
日常生活https://tex.stackexchange.com/a/350169/118714。现在您可以指定变量数范围的下限和上限。借助 Heiko Oberdiek 的 bigintcalc 包,例程将计算要处理的无界参数的数量并自动选择正确的参数。
\documentclass{article}
\usepackage{bigintcalc}
\makeatletter
%%=========================================================================
%% Paraphernalia:
%% \UD@firstoftwo, \UD@secondoftwo, \UD@PassFirstToSecond
%%.........................................................................
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
%%-------------------------------------------------------------------------
%% Check whether argument is empty:
%%.........................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
%% A concern in his posting is that the argument is hit with \string
%% after some expansions which in edge cases might result in unbalancing
%% surrounding \if..\fi-constructs if the macro is used inside of such
%% \if..\fi-constructs.
%%
%% That challenging concern sickened me. ;-)
%%
%% Therefore I decided to implerment a variant where this cannot happen
%% as expansion is forced by \romannumeral:
%%
%% After the first expansion-step, \string is not applied yet.
%% After the second expansion-step, any possibly disturbing remainders
%% are already removed due to \romannumeral-expansion.
%%
%% No eTeX- or whatsoever extensions. No \if.. .Only \romannumeral,
%% digit 0, space token for terminating \romannumeral-expansion,
%% \string, \expandafter, \UD@firstoftwo, \UD@secondoftwo, {, }.
%%
%% May 20, 2016
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
\UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-------------------------------------------------------------------------
%% Keep only the K-th of L consecutive undelimited arguments.
%% ( IF K < 1 OR K > L just remove L consecutive undelimited arguments. )
%%.........................................................................
%% \UD@KeepKthOfLArguments{<integer number K>}%
%% {<integer number L>}%
%% {<Tokens to insert before K-th argument>}%
%% <L consecutive undelimited arguments>
%%
%% If K >= 1 and K <= L yields:
%% <Tokens to insert before K-th argument><K-th undelimited argument>
%% If K < 1 or K > L
%% (-> there is no K-th argument in the set
%% of <L consecutive undelimited arguments> )
%% yields:
%% <Tokens to insert before K-th argument>
%%
%% Examples:
%%
%% X\UD@KeepKthOfLArguments{3}{7}{<Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X<Tokens to insert>CX
%%
%% X\UD@KeepKthOfLArguments{5}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\UD@KeepKthOfLArguments{0}{2}{<Tokens to insert>}{A}{B}X
%% yields X<Tokens to insert>X
%%
%% X\romannumeral0%
%% \UD@KeepKthOfLArguments{3}{7}{ <Tokens to insert>}{A}{B}{C}{D}{E}{F}{G}X
%% yields: X\romannumeral0 <Tokens to insert>CX
%% yields: X<Tokens to insert>CX
%% In case of embedding the whole thing in other expansion-contexts,
%% you need to have "hit" \romannumeral by only one \expandafter-chain for
%% obtaining the result...
%%
%% January 17, 2005
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\UD@KeepKthOfLArguments[2]{%
\expandafter\UD@KeepKthOfLArgumentsA
\expandafter{\romannumeral\number\number#1 000\expandafter}%
\expandafter{\romannumeral\number\number#2 000}%
}%
\newcommand\UD@KeepKthOfLArgumentsA[3]{%
\UD@CheckWhetherNull{#1}{%
\UD@RemoveNArguments{#2}{#3}{}%
}{%
\expandafter\UD@PassFirstToSecond
\expandafter{%
\UD@firstoftwo{}#1}{\UD@KeepKthOfLArgumentsB{#1}{#2}}{#2}{#3}%
}%
}%
\newcommand\UD@KeepKthOfLArgumentsB[5]{%
\UD@CheckWhetherNull{#1}{%
\UD@RemoveNArguments{#3}{\UD@RemoveNArguments{#2}{#5}}{}%
}{%
\UD@CheckWhetherNull{#2}{%
\UD@RemoveNArguments{#4}{#5}{}%
}{%
\expandafter\UD@PassFirstToSecond
\expandafter{%
\UD@firstoftwo{}#2}{\expandafter\UD@KeepKthOfLArgumentsB
\expandafter{%
\UD@firstoftwo{}#1}}{#3}{#4}{#5}%
}%
}%
}%
\newcommand\UD@RemoveNArguments[3]{%
\UD@CheckWhetherNull{#1}{#2#3}{%
\UD@firstoftwo{%
\expandafter\UD@RemoveNArguments
\expandafter{\UD@firstoftwo{}#1}{#2}{#3}%
}%
}%
}%
%%-------------------------------------------------------------------------
%% \UD@incrementloop{<lower bound>}%
%% {<upper bound>}%
%% {<count start>}%
%% {<tokens to prepend in front of each number except last number>}%
%% {<tokens to prepend in front of last number>}%
%% {<tokens to prepend at the begnning>}%
%% {<tokens to append at the end>}%
%%
%% Examples:
%% \UD@incrementloop{3}{7}{2}{<Before All>}{<Before Number>}{<Before Last Number>}{<After Last Numbe>r}
%%
%% -> The range 3..7 contains 7-3+1 consecutive integeral numbers/elements
%% = 5 consecutive integeral numbers/elements.
%% -> Gathering 5 consecutive integeral numbers/elements starting with
%% number/element 2 yields the numbers/elements 2,3,4,5,6.
%% (You can gather them by iteratively decrementing from 7-3=4 until
%% reaching -1 while within each iteration before reaching -1
%% "spitting out" a value and incrementing that value, starting
%% with value=2.)
%%
%% -> <Before All><Before Number>2<Before Number>3<Before Number>4<Before Number>5<Before Last Number>6<After Last Number>
%%
%% ,i.e., with
%% <Before All>=\ifcase[<number>] 1} and
%% <Before Number>=\or and
%% <Before Last Number>=\else and
%% <After Last Number>=\fi
%% yields:
%%
%% \UD@incrementloop{3}{7}{2}{\ifcase[<number>] 1}{\or}{\else}{\fi}
%% ->
%% \ifcase[<number>] 1\or2\or3\or4\or5\else6\fi
%%
\newcommand\UD@incrementloop[3]{%
\romannumeral0%
\expandafter\UD@PassFirstToSecond
\expandafter{\number#3}{%
\expandafter\expandafter\expandafter\UD@PassFirstToSecond
\expandafter\expandafter\expandafter{\bigintcalcSub{\number#2}{\number#1}}%
{\UD@@incrementloop}{}%
}%
}%
\newcommand\UD@@incrementloop[7]{%
\ifnum\bigintcalcCmp{0}{#1}=-1 %
\expandafter\UD@secondoftwo
\else
\expandafter\UD@firstoftwo
\fi
{%
\ifnum\bigintcalcCmp{0}{#1}=1 %
\expandafter\UD@secondoftwo
\else
\expandafter\UD@firstoftwo
\fi
{ #4#2#6#3#7}%
{ #4#2#7}%
}{%
\expandafter\expandafter\expandafter\UD@PassFirstToSecond
\expandafter\expandafter\expandafter{\bigintcalcInc{#3}}%
{\expandafter\expandafter\expandafter\UD@@incrementloop
\expandafter\expandafter\expandafter{\bigintcalcDec{#1}}%
{#2#5#3}}{#4}{#5}{#6}{#7}%
}%
}%
%%-------------------------------------------------------------------------
%% A demo-routine:
%%
%% \choosetext{<number from variable>}%
%% {<number denoting lower bound>}%
%% {<number denoting upper bound>}%
%% {<Text in case <number from variable> is smaller than <number denoting lower bound> >}%
%% {<Text in case <number from variable> = <number denoting lower bound> >}%
%% {<Text in case <number from variable> = <number denoting lower bound>+1 >}%
%% {<Text in case <number from variable> = <number denoting lower bound>+2 >}%
%% ...
%% {<Text in case <number from variable> = <number denoting lower bound>+K >
%% ...
%% {<Text in case <number from variable> = <number denoting upper bound>-2 >}%
%% {<Text in case <number from variable> = <number denoting upper bound>-1 >}%
%% {<Text in case <number from variable> = <number denoting upper bound> >}%
%% {<Text in case <number from variable> is larger than <number denoting upper bound> >}%
%%
%% The <number...>-arguments can hold anything that _expands_ to the components
%% of a quantity that is subsumable under the term <number> which is
%% explained in Donald E. Knuth's The TeXbook, Chapter 24: Summary of
%% Vertical Mode.
%%
%% Therefore the <number...>-arguments can as well be a macro-tokens expanding
%% to a digit-sequences, or \the-calls to \count-registers.
%%
%% Due to \romannumeral-expansion the result is delivered within two
%% expansion-steps.
%%
%% January 24, 2017
%%
%% Ulrich Diez (e-mail: [email protected])
%%
%%.........................................................................
\newcommand\choosetext[3]{%
\romannumeral0%
\UD@KeepKthOfLArguments{%
\UD@incrementloop{2}%
{\bigintcalcAdd{\bigintcalcSub{\number#3}{\number#2}}{3}}%
{2}%
{%
\ifcase\expandafter\UD@firstoftwo\expandafter{%
\number
\ifnum\bigintcalcCmp{%
\bigintcalcSub{\number#1}{\number#2}%
}{0}<0 %
\expandafter\UD@firstoftwo
\else
\expandafter\UD@secondoftwo
\fi
{0}{%
\bigintcalcAdd
{\bigintcalcSub{\number#1}{\number#2}}%
{1}%
}%
}{} 1%
}%
{\or}%
{\else}%
{\fi}%
}{%
\bigintcalcAdd{\bigintcalcSub{\number#3}{\number#2}}{3}%
}{ }%
}%
%%-------------------------------------------------------------------------
%% A "variable" \Amacro and the routine \setAmacro for setting it's value.
%%.........................................................................
\newcommand\Amacro{1}%
\newcommand\setAmacro[1]{\renewcommand\Amacro{#1}}%
\makeatother
\begin{document}
The \verb|<number>|-argument of \verb|\choosetext| can hold anything that
expands to the components of a quantity that is subsumable under the term
\verb|<number>| which is explained in Donald E. Knuth's The TeXbook,
Chapter 24: Summary of Vertical Mode.
\bigskip
Thus the \verb|<number>|-argument of \verb|\choosetext| can hold a sequence
of digits, e.g., the digit \verb|5|:
\bigskip
\choosetext{5}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\bigskip
The \verb|<number>|-argument of \verb|\choosetext| can as well hold a
macro-token which serves as variable/placeholder for numerical values.
\bigskip
\setAmacro{9}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{7}{12}%
{Text in case number is smaller than 7}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is larger than 12}%
\hrulefill
\setAmacro{9}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{4}{8}%
{Text in case number is smaller than 4}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is larger than 8}%
\hrulefill
\setAmacro{17}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{14}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{13}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{12}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{11}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{10}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{9}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{8}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{7}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{6}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{5}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{4}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{3}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{2}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{0}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\hrulefill
\setAmacro{-1}%
\verb|\Amacro| expands to the number \Amacro:
\choosetext{\Amacro}{1}{14}%
{Text in case number is smaller than 1}%
{Text in case number is 1}%
{Text in case number is 2}%
{Text in case number is 3}%
{Text in case number is 4}%
{Text in case number is 5}%
{Text in case number is 6}%
{Text in case number is 7}%
{Text in case number is 8}%
{Text in case number is 9}%
{Text in case number is 10}%
{Text in case number is 11}%
{Text in case number is 12}%
{Text in case number is 13}%
{Text in case number is 14}%
{Text in case number is larger than 14}%
\end{document}