2

I would like to draw some more or less advanced grid, using three consecutive (non-nested) foreach loops. For unknown reasons, the third loop seems to be ignored... Here is the code:

\begin{tikzpicture}
    \node at (22,3) (F) {$F$};

\foreach \i in {0,...,10}
{
    \pgfmathtruncatemacro{\label}{\i}
    \node at (20-2*\i,4) (A\label) {$v_{\label}$};
}

\foreach \i in {0,...,9}
{
    \pgfmathtruncatemacro{\label}{\i}
    \pgfmathtruncatemacro{\labelp}{\i + 1}
    \node at (19-2*\i,2) (E\i) {$u_{\label}$}
    (A\labelp) edge (E\label)
    (E\label) edge (A\label)
    (A\labelp) edge (A\label);
}

\foreach \i in {0,...,8}
{
    \pgfmathtruncatemacro{\label}{\i}
    \pgfmathtruncatemacro{\labelpp}{\i + 2}
    (E\label) edge[bend left = 50] (E\labelpp);
}
\end{tikzpicture}

Thank you very much for your help!

2
  • Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it).
    – Jesse
    Jan 20, 2015 at 9:32
  • There is no tikz command in the third loop. Add \draw before (E\label.... (and lower the upper bound to 7)
    – Harald
    Jan 20, 2015 at 9:56

2 Answers 2

2

Harald already commented that a \draw command was missing in third loop. So, next code just want to show a possibility to avoid \pgfmathtruncatemacro for computing labels based in index values.

\documentclass[border=2mm,tikz]{standalone}

\begin{document}

\begin{tikzpicture}
\node at (22,3) (F) {$F$};

\foreach \i [count=\label from 0] in {0,...,10}
{
%    \pgfmathtruncatemacro{\label}{\i}
    \node at (20-2*\i,4) (A\label) {$v_{\label}$};
}

\foreach \i [count=\label from 0, count=\labelp] in {0,...,9}
{
%    \pgfmathtruncatemacro{\label}{\i}
%    \pgfmathtruncatemacro{\labelp}{\i + 1}
    \node at (19-2*\i,2) (E\i) {$u_{\label}$}
    (A\labelp) edge (E\label)
    (E\label) edge (A\label)
    (A\labelp) edge (A\label);
}

\foreach \i [count=\label from 0, evaluate=\label as \labelpp using \label+2] in {0,...,7}
{
%    \pgfmathtruncatemacro{\label}{\i}
%    \pgfmathtruncatemacro{\labelpp}{\i + 2}
    \draw (E\label) edge[bend left=50] (E\labelpp);
}
\end{tikzpicture}
\end{document}

enter image description here

0

Do you want someting like this? Set the third \foreach loop's bounds to {-1,...,7} and add the \node statement:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \node at (22,3) (F) {$F$};
      \foreach \i in {0,...,10}
{
    \pgfmathtruncatemacro{\label}{\i}
    \node at (20-2*\i,4) (A\label) {$v_{\label}$};
}

\foreach \i in {0,...,9}
{
    \pgfmathtruncatemacro{\label}{\i}
    \pgfmathtruncatemacro{\labelp}{\i + 1}
    \node at (19-2*\i,2) (E\i) {$u_{\label}$}
    (A\labelp) edge (E\label)
    (E\label) edge (A\label)
    (A\labelp) edge (A\label);
}

\foreach \i in {-1,...,7}
{
    \pgfmathtruncatemacro{\label}{\i}
    \pgfmathtruncatemacro{\labelpp}{\i + 2}
    \node at (17-2*\i,2) (E\label) {}
    (E\label) edge[bend left = 50] (E\labelpp);
}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .