Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am searching for a way to conditionally handle some items in a TikZ \foreach specially using an \ifthenelse condition. However, the compiler responds

! Undefined control sequence.
<argument> \isin 

when processing this document.

\documentclass{standalone}
\usepackage{tikz, xifthen}

\newcommand\groupedges[2]{
  \begin{tikzpicture}
    \foreach \name/\position in {a/30, b/40, c/20}
      \node (\name) at (\position:5cm) {\name};
    \foreach \name/\position in {d/120, e/150, f/170, g/90, h/100}
      \node (\name) at (\position:5cm) {\name};
    \node (left) at (-1, 0) {left};
    \node (right) at (1, 0) {right};
    \foreach \name in {#1}
      \path (\ifthenelse{\isin{\name}{#2}}{right}{left}) edge (\name);
  \end{tikzpicture}
}

\begin{document}
  \groupedges{a,b,d,e,g,h}{abc}
\end{document}

I initially thought of expressing the conditional with pgfkeys, but found this to be too cumbersome, especially when used in a \newcommand, which I need for the more complex intended case this serves as an example for.

I also tried to implement it using pure TeX, which has an \ifin@ conditional. This results in the same error.

Are TikZ and ifthen (or xifthen) combinable? Is there an easy pgfkeys or pure TeX solution? Or may there even be more suggestions to implement decisions on the \foreach elements?

share|improve this question

2 Answers 2

Quite probably, the issue is with the (non) expandability of these tests. But you can do this [thanks to the moderator who corrected the edge, I had copied pasted from the OP without double-check]

\documentclass{standalone}
\usepackage{tikz, xifthen}

\newcommand\groupedges[2]{
  \begin{tikzpicture}
    \foreach \name/\position in {a/30, b/40, c/20}
      \node (\name) at (\position:5cm) {\name};
    \foreach \name/\position in {d/120, e/150, f/170, g/90, h/100}
      \node (\name) at (\position:5cm) {\name};
    \node (left) at (-1, 0) {left};
    \node (right) at (1, 0) {right};
    \foreach \name in {#1}
      {\ifthenelse{\isin{\name}{#2}}{\path (right) edge (\name);}
                                    {\path (left)  edge (\name);}}
  \end{tikzpicture}
}


\begin{document}\thispagestyle{empty}%
  \groupedges{a,b,d,e,g,h}{abc}
\end{document}

(updated image)

ifthen1

But, is apparent, this does not work as expected. I thus was sort of reluctantly coerced into again making propaganda for xint :):

\documentclass{standalone}
\usepackage{tikz, xifthen}
\usepackage{xint}

\newcommand\groupedges[2]{%
  \begin{tikzpicture}
    \foreach \name/\position in {a/30, b/40, c/20}
      \node (\name) at (\position:5cm) {\name};
    \foreach \name/\position in {d/120, e/150, f/170, g/90, h/100}
      \node (\name) at (\position:5cm) {\name};
    \node (left) at (-1, 0) {left};
    \node (right) at (1, 0) {right};
    % \foreach \name in {#1}
    %   {\ifthenelse{\isin{\name}{#2}}{\path (right) edge (\name);}
    %                                 {\path (left)  edge (\name);}}
    \xintFor ##1 in {#1}
    \do  {\ifthenelse{\isin{##1}{#2}}{\path (right)}
                                     {\path (left)}  edge (##1);}
  \end{tikzpicture}%
}


\begin{document}\thispagestyle{empty}%
  \groupedges{a,b,d,e,g,h}{abc}
\end{document}

xifthen2

share|improve this answer
    
The conditional does not seem to work right. All the nodes are still connected to left, while a and b should in fact be connected to right. –  XZS Nov 14 '13 at 0:47
    
@XZS I had copied pasted the original code without double-checking so I did not become aware of the problem, sorry. It seems the problem is more with \foreach than with the conditional? I don't know, but I have added a method not using \foreach, and it works. –  jfbu Nov 14 '13 at 7:38

Two remarks :

  • \isin seems impossible to put inside path command. So you have to do (as proposed by @jfbu) :
    {\ifthenelse{...}{\path (right) edge (\name);}
                     {\path (left)  edge (\name);}} 
  • to be able to use \isin{\name}{#2} you have to expand \name befor to pass it to \isin like this \expandafter\isin\name{#2} so finaly you can do this :
    {\ifthenelse{\expandafter\isin\name{#2}}{\path (right) edge (\name);}
                                            {\path (left)  edge (\name);}} 


Here is the full code that works for me :

\documentclass{standalone}
\usepackage{tikz, xifthen}

\newcommand\groupedges[2]{
  \begin{tikzpicture}
    \foreach \name/\position in {a/30, b/40, c/20}
      \node (\name) at (\position:5cm) {\name};
    \foreach \name/\position in {d/120, e/150, f/170, g/90, h/100}
      \node (\name) at (\position:5cm) {\name};
    \node (left) at (-1, 0) {left};
    \node (right) at (1, 0) {right};
    \foreach \name in {#1}
      {\ifthenelse{\expandafter\isin\name{#2}}{\path (right) edge (\name);}
                                    {\path (left)  edge (\name);}}
  \end{tikzpicture}
}

\begin{document}
  \groupedges{a,b,d,e,g,h}{abc}
\end{document}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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