4

Hi and many thanks in advance!

I'm trying to perfom an \ifthenelse check on an element, which was extracted out of a list using a macro found here. Unfortunately I cannot figure out how to do it: Undefined control sequence is thrown when trying to do the check. Probably expansion is the problem, but this exceeds my knowledge. 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}
Pontis
  • 269

2 Answers2

3

These are expansion issues but since you are loading TikZ: why don't you just use the built in extraction mechanism?

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

enter image description here

Notice that if the list does not only contain numbers you need to wrap the items in "...".

  • Many thanks! I just did not know about that mechanism. A minor disadvantage is that members of the list cant be accessed directly, but have to be assigned to some command. Nevertheless, this is what I asked for and your solution works very fine for me! – Pontis Nov 24 '19 at 11:50
3

You can use xparse, which has functions to expandably extract items from comma separated list of values.

You just need to take care of the fact that expl3 lists are indexed starting from one (which explains the +1 in the second argument to \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}

enter image description here

egreg
  • 1,121,712