1

Whenever I try to use an element of an array as an entry for a \ifthenelse statement I get the error Incomplete \iffalse; all text was ignored after line 6. Here's a 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}

Any idea why this happens?

noibe
  • 2,084

1 Answers1

0

I propose a reimplementation of \ifthenelse along the lines of https://tex.stackexchange.com/a/467527/4427 and a couple of array management macros.

\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}

enter image description here

You can even use expressions in the second argument to \get.

If your array items contain commas, you can change the separator at runtime, for instance

\newarray{commas}[|]{
  abc,def |
  xy |
  end,end
}

This way item 1 will be abc,def.

egreg
  • 1,121,712
  • I realised that in my code I only need to check whether the array element is empty or not, so I ended up using your code from [https://tex.stackexchange.com/questions/42280/expand-away-empty-macros-within-ifthenelse] and it works fine. – noibe Feb 01 '19 at 12:00