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?

  • Never use arrayjob with LaTeX, but arrayjobx; anyway, \ifthenelse does no expansion of its arguments, so it compares the tokens \items(0) and item, which are different. – egreg Feb 1 at 11:32
  • Is there a way to make this work? – noibe Feb 1 at 11:36
  • Yes, but not with arrayjob(x) – egreg Feb 1 at 11:37
  • What package should I look into? – noibe Feb 1 at 11:38
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.