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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to cycle through the terms in a \foreach function. Here's my code:

\foreach \n in {cdof,cc,cdus,ads,aiw} \draw [->] (\n) |- ($(\n)+1$);

However, as expected, this breaks down as there is no term after 'aiw'. I've tried putting in a condition ($((\n)+1)<6$) but this doesn't work. Is it possible to put conditions in the \foreach function? Or is there an alternative solution?

share|improve this question
    
What exactly do you want to do with that condition? I don't think I get it exactly. – Alenanno Feb 22 at 0:32
up vote 3 down vote accepted

In my first try, I did not notice you want to connect nodes via arrows. There are two options (see the figure below). You can connect all of the nodes via one zig-zag line with an arrow at the and (red line) or with multiple lines with an arrow at each node (black).

Code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}

    \begin{tikzpicture} 

    \coordinate (A) at (0,0);
    \coordinate (B) at (1,1);
    \coordinate (C) at (2,2);
    \coordinate (D) at (3,3);

    % FIRST OPTION
    \draw[red,thick,->] (A)
    \foreach \i in {B,C,D}{
        |- (\i)
        };

    % SECOND OPTION
    \foreach \n [count=\i] in {A,B,C,D}
    {       
        \ifnum\i>1
            \draw[thick,->] (Q) -| (\n);
        \else \fi
        \coordinate (Q) at (\n);
    }

    \end{tikzpicture}
\end{document}

Result:

enter image description here


Older version - I will leave it here since it uses a slightly different method and may also be interesting to consider.

Code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture} 
\foreach \i in {0,...,3} % prepare dummy coordinates
    \coordinate (A-\i) at (\i,\i);

\draw[thick] (A-0) node[fill=black] {\color{white}0}
foreach \i in {1,...,3}{
    |- (A-\i) node[fill=black] {\color{white}\i}
    };      
\end{tikzpicture}
\end{document}

Sometimes, (here too) you can even further simplify the code as

\draw[thick] (A-0)
foreach \i in {0,...,3}{ % different range than in the original code
    |- (A-\i) node[fill=black] {\color{white}\i}
    };

Result:

enter image description here

share|improve this answer

Also without any conditions, provided that you name your nodes in a sequentially numbered way like a-x with x ranging from 0 to 4, e.g., if you have five nodes.

\documentclass[border=5pt,tikz]{standalone}
\begin{document}    
\begin{tikzpicture}[line width=.7pt] 

\foreach \i in {0,...,4} % prepare dummy coordinates as @Ondrian
  \coordinate (a-\i) at (\i,\i);

\foreach \i [count=\j] in {0,...,3}
  \draw [->] (a-\i) |- (a-\j);

\end{tikzpicture}    
\end{document}

enter image description here

share|improve this answer

If you want to cycle through a list and remember the first one to be used after the last one (or the last one to be used before the first one), remember option is what you need.

Following code taken from AboAmmar answer has been slightly modified to remember previous element from the list. initially sets the element previous to first element.

\documentclass[border=5pt,tikz]{standalone}
\begin{document}    
\begin{tikzpicture}[line width=.7pt] 

\foreach \i in {0,...,4} % prepare dummy coordinates as @Ondrian
  \coordinate (a-\i) at (\i,\i);

\foreach \i [remember=\i as \j (initially 4)] in {0,...,4}
  \draw [->] (a-\j) |- (a-\i);

\end{tikzpicture}    
\end{document}

enter image description here

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.