How to apply \ifthenelse check on list member accessed via macro?
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}
Notice that if the list does not only contain numbers you need to wrap the items in "..."
.
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}