[Tex/LaTex] Dotted lines in TikZ with round dots

tikz-pgf

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?

Best Answer

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}