# Draw several horizontal lines using tikz

I would like to draw 5 horizontal lines, each of length 0.5cm and separated by 0.2cm. I have the following code, however, instead of getting 5 individual lines, I get one long continous line. What could be the problem?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfmath}

\title{Horizontal Lines}

\begin{document}
\begin{tikzpicture}
\begin{scope}

\draw[-] (0,0) -- coordinate (a1) (0,0); %Draw first line

\foreach \x in {2,3,4,5}{
%Compute next starting point. 0.7 = 0.5 (line length) + 0.2 (line spacing)
\pgfmathparse{0 + (\x-1) * 0.7}
\draw[-] (\pgfmathresult,0) -- coordinate (a\x) (\pgfmathresult+0.5,0);
}
\end{scope}

\end{tikzpicture}

\end{document}


The problem might be to further computation with the result of \pgfmathresult?. Despite the dash-patternof Torbjørn, another idea (which is i think more readable than the OPs approach) is the TikZ calclibrary

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,2,3,4}{
\draw (.7*\x,0) -- coordinate(a\x) (.5+.7*\x,0);
}
\end{tikzpicture}
\end{document}

• You don't actually need the calc library for that. – Torbjørn T. Mar 8 '15 at 8:09
• Oh, i just wasn't sure, thanks for noting that. – Ronny Mar 8 '15 at 8:10
• All great answers, but I choose this one because it appears pgfmath wasn't required to solve the problem :) – picasso Mar 8 '15 at 13:28

I don't see immediately what's wrong, but if you save the calculation to a macro and use that instead, it works fine. However, a much more concise way of doing the same is to use a custom dash pattern.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\draw (0,0) -- coordinate (a1) (0.5,0); %Draw first line

\foreach \x in {2,3,4,5}{
%Compute next starting point. 0.7 = 0.5 (line length) + 0.2 (line spacing)
\pgfmathsetmacro\tmp{0 + (\x-1) * 0.7}
\draw (\tmp,0) -- coordinate (a\x) (\tmp+0.5,0);
}

\draw [dash pattern={on 0.5cm off 0.2cm}] (0,0.5) -- (5*0.7,0.5);

\end{tikzpicture}

\end{document}


You are too late to catch the right value of the \pgfmathresult. By the time, you refer to it, TikZ already uses a lot of math operations and stores it in \pgfmathresult. Hence if you want to do some calculations, it is almost always required that you keep the expression \pgfmathparse{....} and the result \pgfmathresult back to back.

For example here

\begin{tikzpicture}
\draw[-] (0,0) -- coordinate (a1) (0,0); %Draw first line

\foreach \x in {2,3,4,5}{
%Compute next starting point. 0.7 = 0.5 (line length) + 0.2 (line spacing)
\pgfmathparse{(\x-1)*0.7}
\draw[] (\pgfmathresult,-\x) node {\pgfmathresult} --
(\pgfmathresult+0.5cm,\x) node[left] {\pgfmathresult};
}
\end{tikzpicture}


we can see who hijacked the computation it is the second coordinate computations. To fix that you need to move things closer before TikZ attempts to do any calculations or you can save the result to macro of yours via \pgfmathsetmacro<macro name>{expression}. And for that we need to pause the path parsing

\begin{tikzpicture}
\draw[-] (0,0) -- coordinate (a1) (0,0); %Draw first line

\foreach \x in {2,3,4,5}{
\draw[] \pgfextra{\pgfmathparse{(\x-1)*0.7}} (\pgfmathresult,0)  -- ++ (0.5,0);
}
\end{tikzpicture}


Or better, you can leave it to foreach

\begin{tikzpicture}
\draw[-] (0,0) -- coordinate (a1) (0,0); %Draw first line
\foreach \x[evaluate=\x as \xm using {(\x-1)*0.7}] in {2,3,4,5}{
\draw[-] (\xm,0) -- coordinate (a\x) (\xm+0.5,0);
}
\end{tikzpicture}