每当我尝试使用数组元素作为语句的条目时,\ifthenelse
都会出现错误Incomplete \iffalse; all text was ignored after line 6
。这是一个 MWE。
\documentclass{memoir}
\usepackage{arrayjob, ifthen}
\newarray\items
\begin{document}
\items(0)={item}
\ifthenelse{\equal{\items(0)}{item}}{they're equal}{they're different}
\end{document}
知道为什么会发生这种情况吗?
答案1
我建议重新实施\ifthenelse
如下https://tex.stackexchange.com/a/467527/4427以及一些数组管理宏。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\xifthenelse}{mmm}
{
\bool_if:nTF { #1 } { #2 } { #3 }
}
\cs_new_eq:NN \numtest \int_compare_p:n
\cs_new_eq:NN \oddtest \int_if_odd_p:n
\cs_new_eq:NN \dimtest \dim_compare_p:n
\cs_new_eq:NN \deftest \cs_if_exist_p:N
\cs_new_eq:NN \namedeftest \cs_if_exist_p:c
\cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN
\cs_new_eq:NN \streqtest \str_if_eq_p:ee
\cs_new_eq:NN \emptytest \tl_if_blank_p:n
\prg_new_conditional:Nnn \xxifthen_legacy_conditional:n { p,T,F,TF }
{
\use:c { if#1 } \prg_return_true: \else: \prg_return_false: \fi:
}
\cs_new_eq:NN \boolean \xxifthen_legacy_conditional_p:n
% implementing arrays
\NewDocumentCommand{\newarray}{mO{,}m}
{% #1 = array name, #2 = separator, #3 = items
\seq_new:c { l_array_#1_seq }
\seq_set_split:cnn { l_array_#1_seq } { #2 } { #3 }
}
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\NewExpandableDocumentCommand{\get}{mm}
{% #1 = array name, #2 = index
\seq_item:cn { l_array_#1_seq } { #2 }
}
\ExplSyntaxOff
\newarray{items}{
item,
another,
OK
}
\begin{document}
\xifthenelse{\streqtest{\get{items}{1}}{item}}{they're equal}{they're different}
\xifthenelse{\streqtest{\get{items}{2}}{item}}{they're equal}{they're different}
\xifthenelse{\streqtest{\get{items}{5*2-7}}{OK}}{they're equal}{they're different}
\end{document}
您甚至可以在第二个参数中使用表达式\get
。
如果数组项包含逗号,则可以在运行时更改分隔符,例如
\newarray{commas}[|]{
abc,def |
xy |
end,end
}
这样,项目 1 将是abc,def
。