# Dotted lines in TikZ with round dots

Well, the title explains all, I guess. Sorry if this is too simple for all the TikZ gurus here: but I want a dash pattern like "dotted" for drawning but with round dots and not the little squares. How can I achieve this in a simple way?

• You could maybe achieve this with one of the decorations library? – Lionel MANSUY Mar 7 '13 at 8:25

You could define a custom dash pattern with an on length of 0pt. If you set line cap=round, you'll end up with perfect circles. The dash pattern approach is much faster than using decorations for the same purpose.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw [line width=3pt, line cap=round, dash pattern=on 0pt off 2\pgflinewidth] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\end{document}


The whole thing can of course be made into a new style so you just have to use dots, and which allows you to comfortably set the dot diameter using dot diameter and the dot spacing using dot spacing

\draw [dots] ...


\draw [red, dot diameter=5pt, dot spacing=5pt, dots] ...


\documentclass{article}

\usepackage{tikz}

\begin{document}

\makeatletter
\tikzset{
dot diameter/.store in=\dot@diameter,
dot diameter=3pt,
dot spacing/.store in=\dot@spacing,
dot spacing=10pt,
dots/.style={
line width=\dot@diameter,
line cap=round,
dash pattern=on 0pt off \dot@spacing
}
}
\makeatother

\begin{tikzpicture}
\draw [dots] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\begin{tikzpicture}
\draw [red, dot diameter=5pt, dot spacing=5pt, dots] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\end{document}

• thank you very much. Works perfect. I'm just playing a bit with the parameters to get what I need :) – Stefan Waldmann Mar 7 '13 at 8:38

Another possibility is to exploit the decorations libraries as Lionel suggested: one example presented will be based on decorations.markings while the other one on decorations.shapes.

Decorations.markings

The code:

\documentclass[png,tikz,border=3pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\pgfkeys{/tikz/.cd,
circle color/.initial=black,
circle color/.get=\circlecolor,
circle color/.store in=\circlecolor,
}

\tikzset{dotted pattern/.style args={#1 and #2}{
postaction=decorate,
decoration={
markings,
mark=
between positions 0 and 1 step #2
with
{
}
}
},
dotted pattern/.default={1pt and 1.5mm},
}

\begin{document}
\begin{tikzpicture}
\path [dotted pattern]
(0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\begin{tikzpicture}
\path [circle color=red,
dotted pattern=1.5pt and 4mm]
(0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}
\end{document}


provides:

and

Indeed, with the dotted pattern style you can customize the radius and the distance between the circles respectively while with the circle color key you may change the color.

Decorations.shapes

The code:

\documentclass[png,tikz,border=3pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.shapes}

\pgfkeys{/tikz/.cd,
circle color/.initial=black,
circle color/.get=\circlecolor,
circle color/.store in=\circlecolor,
}

\tikzset{dotted pattern/.style args={#1 and #2}{
decorate,
fill=\circlecolor,
decoration={
shape backgrounds,
shape=circle,
shape size=#1,
shape sep={#2, between center},
}
},
dotted pattern/.default={1pt and 1.5mm},
}

\begin{document}
\begin{tikzpicture}
\path [dotted pattern]
(0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\begin{tikzpicture}
\path [circle color=red,
dotted pattern=1.5pt and 4mm]
(0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}
\end{document}


The result are still the pictures shown above. In order to use \draw rather than \path, one might add a draw=none to the dotted pattern style.

• Note that this approach is orders of magnitudes slower than using dash pattern: Compiling a document containing only a tikzpicture with \draw [dot diameter=2pt, dot spacing=4pt, dots] (0,0) grid (10,10); takes about 0.13 seconds on my system, but \path [dotted pattern=1pt and 4pt] (0,0) grid (10,10); takes more than 5 seconds. – Jake Mar 7 '13 at 9:56
• Yes, it's true (made a test and I got something near 9 seconds to be honest): and it suffers also when the space between two consecutive dots is high (as per the second example). – Claudio Fiandrino Mar 7 '13 at 10:20
• @Claudio. Thanks also to you. Compilation time might really be an issue, so I think I will stick to the solution of Jake. But anyway: amazing what one can do with TikZ. Thanks again! – Stefan Waldmann Mar 7 '13 at 10:49
• @StefanWaldmann: the answer was more intended as another way to do it; moreover, as all efficiency-related issues, the compilation time is mainly based on what you're doing: if the purpose is to use a simple path, then Jake's approach and mine are comparable. For complicated and heavy things, like the grid, this is not more true. – Claudio Fiandrino Mar 7 '13 at 11:32
• NOTICE: the decorations.shapes is even slower than decorations.markings (in order to compile the grid I had to use LuaLaTeX). @Jake: I was wondering, just for decorations.markings, if adding a reset marks will be of help... – Claudio Fiandrino Mar 12 '13 at 10:27