您好,提前谢谢您!
我正在尝试\ifthenelse
对一个元素进行检查,该元素是使用找到的宏从列表中提取出来的这里。不幸的是,我不知道该怎么做:Undefined control sequence
在尝试进行检查时抛出。扩展可能是问题所在,但这超出了我的知识范围。MWE:
\documentclass{minimal}
\usepackage{ifthen,tikz,xstring}
\usetikzlibrary{calc}
% define check
\newcommand{\checkValue}[1]{\ifthenelse{\equal{#1}{1}}{#1: true}{#1: false}}
% routine to extract list member
\newcommand*{\getListMember}[2]{%
\edef\dotheloop{%
\noexpand\foreach \noexpand\a [count=\noexpand\i] in {#1} {%
\noexpand\IfEq{\noexpand\i}{#2}{\noexpand\a\noexpand\breakforeach}{}%
}}%
\dotheloop
}
\begin{document}
\noindent
\checkValue{0}\\ % works
\checkValue{1}\\ % works
\noindent
\def\theItem{\getListMember{0,1}{1}}%
Member to check: \theItem\\
Do check: %\checkValue{\theItem} % fails: Undefined control sequence.
\end{document}
答案1
这些是扩展问题,但由于您正在加载 Ti钾Z:为什么你不直接使用内置的提取机制?
\documentclass{article}
\usepackage{ifthen,tikz,xstring}
\usetikzlibrary{calc}
% define check
\newcommand{\checkValue}[1]{\ifthenelse{\equal{#1}{1}}{#1: true}{#1: false}}
% routine to extract list member
\newcommand*{\getListMemberPgf}[3]{%
\pgfmathsetmacro{#1}{{#2}[\the\numexpr#3-1]}%
}
\begin{document}
\noindent
\checkValue{0}\\ % works
\checkValue{1}\\ % works
\noindent
\getListMemberPgf{\theItem}{0,1}{1}%
Member to check: \theItem\\
Do check: \checkValue{\theItem} % no longer fails.
\end{document}
请注意,如果列表不仅包含数字,则需要将项目包装在其中"..."
。
答案2
您可以使用xparse
,它具有以下功能:可扩展从逗号分隔的值列表中提取项目。
您只需要注意expl3
列表是从一开始索引的事实(这解释了+1
第二个参数中的\clist_item:nn
)。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\getListMember}{mm}
{% #1 = clist, #2 = index
\clist_item:nn { #1 } { #2+1 }
}
\NewDocumentCommand{\saveListMember}{mmm}
{% #1 = clist, #2 = index, #3 = macro
\tl_if_exist:NF #1
{
\tl_new:N #1
\tl_set:Nx #3 { \clist_item:nn { #1 } { #2+1 } }
}
}
\NewExpandableDocumentCommand{\checkValue}{m}
{
\str_if_eq:eeTF { #1 } { 1 } {#1:~true} {#1:~false}
}
\ExplSyntaxOff
\begin{document}
\noindent
\checkValue{0}\\ % works
\checkValue{1} % works
% directly access the member
\noindent
Member to check: \getListMember{0,1}{1}\\
Do check: \checkValue{\getListMember{0,1}{1}}
% alternative, saving the member in a control sequence
\saveListMember{0,1}{1}{\theItem}
\noindent
Member to check: \theItem\\
Do check: \checkValue{\theItem}
\end{document}