以下是 MWE:
\documentclass{article}
\newcommand{\VarOne}{1500}
\begin{document}
I want this list to have item Two if VarOne is more than 1000. If it is not, this will be a two item list. By the way, I could not get this to print VarOne with a backslash!
\begin{itemize}
\item One
\item Two only if VarOne is more than 1000.
\item Three
\end{itemize}
\end{document}
我如何实现这个目标?
答案1
Ulrike Fischer 给出了关于查询的提示low-level
\ifnum
,此版本使用了包\ifnumgreater
中的宏etoolbox
(并提供了比较)。
在我看来,\VarOne
只要只涉及整数值,就不应该将其用作宏而应该用作计数器。
\documentclass{article}
\newcommand{\VarOne}{1500}
\usepackage{etoolbox}%
\begin{document}
I want this list to have item Two if VarOne is more than 1000. If it is not, this will be a two item list. By the way, I could not get this to print \(\backslash\)VarOne with a backslash!
\begin{itemize}
\item One
\ifnumgreater{\VarOne}{1000}{\item Two only if VarOne is more than 1000.}{}
\item Three
\end{itemize}
\renewcommand{\VarOne}{100}%
Once again -- without the 2nd argument
\begin{itemize}
\item One
\ifnumgreater{\VarOne}{1000}{\item Two only if VarOne is more than 1000.}{}
\item Three
\end{itemize}
\end{document}
答案2
这是您的请求的轻度自动化版本,使用了一些来自enumerate
自动对环境中的物品进行排序:
\documentclass{article}
\usepackage{environ,etoolbox,multido}
% Taken from https://tex.stackexchange.com/a/128318/5764
\makeatletter
\newcounter{varoneitem}\newcounter{listcount}[varoneitem]
\let\olditem\item% Store regular \item macro
\NewEnviron{varoneitem}[1][0]{%
\stepcounter{varoneitem}% New orderenum environment (also resets listcount)
\g@addto@macro{\BODY}{\item\relax\item}% Used to delimit the items; last item identified by \item\relax\item
\def\item##1\item{% Redefine \item to capture contents
\def\itemarg{##1}%
\expandafter\ifx\itemarg\relax\else% Last item not reached
\stepcounter{listcount}% Next item being processed
\csgdef{varoneitem@\thevaroneitem @\thelistcount}{##1}% Store item in control sequence
\expandafter\item% Recursively continue processing items
\fi
}
\BODY% Process environment (save items)
\begin{itemize}
\multido{\i=1+1}{\value{listcount}}{%
\ifnum\i=#1\relax
\ifnum\VarOneCondition\relax
\olditem \csname varoneitem@\thevaroneitem @\i\endcsname
\fi
\else
\olditem \csname varoneitem@\thevaroneitem @\i\endcsname
\fi
}
\end{itemize}
}
\makeatother
\newcommand{\VarOne}{1500}
\newcommand{\VarOneCondition}{\VarOne>1000}
\begin{document}
I want this list to have item Two if \verb|\VarOne| is more than 1\,000. If it is not, this will be a two item list.
By the way, I could not get this to print \verb|\VarOne| with a backslash!
\begin{varoneitem}[2]
\item One
\item Two only if \texttt{\string\VarOne} is more than 1\,000.
\item Three
\end{varoneitem}
\renewcommand{\VarOne}{900}
\begin{varoneitem}[2]
\item One
\item Two only if \texttt{\string\VarOne} is more than 1\,000.
\item Three
\end{varoneitem}
\end{document}
这个想法是处理列表两次。在第一次迭代中,所有\item
s 都被收集并存储在宏中。在第二次迭代中,根据 有条件地输出项目\VarOneCondition
。
由于该过程\item
在宏中“捕获”每个内容,因此逐字内容应以不同的方式传递。我在上面的例子中也展示了这一点。
新创建的varoneitem
环境采用可选参数,该参数标识有条件抑制的项目编号。其他改进也是可能的,但我不确定您的最终用例是什么。