# Tikz: draw a line between nodes in a loop

Code:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}

\def\names{{"1", "2"}}
\foreach \i in {0,1} {%
\ifnum\i=0\relax
\node (\i) [draw] {\pgfmathparse{\names[\i]}\pgfmathresult};
\else
\pgfmathparse{int(\i-1)}
\node (\i) [draw,right= 1cm of \pgfmathresult]
{\pgfmathparse{\names[\i]}\pgfmathresult};

%this doesn't work:
%\draw[dotted] ($\pgfmathresult.south east !.5! (\i.south east)$)
%      -- ($\pgfmathresult.south east !.5! (\i.south east) + (0,1cm)$);
\fi
}
\end{tikzpicture}
\end{document}


Creates this:

How can I create a dotted line between the blocks 1cm long? I tried to do it, using the commented code, but it doesn't work, gives an error:

Runaway argument?

\pgfmathresult .south east !.5! (\i .south east)$) -- ($\pgfmathresult \ETC.

-

Essentially your only error is not placing brackets () around your coordinate expression including \pgfmathresult.south east. However, your code for the dotted line can be simplified by only calculating the start point and specifiying the end via the relative coordinate expression -- +(0,1cm). Below I have made slightly different choice of the start position for the line to be halfway between the right edge of the previous node and the left edge of the current one; I am not sure what your actual application is.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}
\def\names{{"1", "2"}}
\foreach \i in {0,1} {%
\ifnum\i=0\relax
\node (\i) [draw] {\pgfmathparse{\names[\i]}\pgfmathresult};
\else
\pgfmathparse{int(\i-1)}
\node (\i) [draw,right= 1cm of \pgfmathresult]
{\pgfmathparse{\names[\i]}\pgfmathresult};
\draw[dotted] ($(\pgfmathresult.south east) !.5! (\i.south west)$)
-- +(0,1cm);
\fi
}
\end{tikzpicture}
\end{document}

-