42

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?

1
  • You could maybe achieve this with one of the decorations library? Commented Mar 7, 2013 at 8:25

2 Answers 2

59

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}
1
  • thank you very much. Works perfect. I'm just playing a bit with the parameters to get what I need :) Commented Mar 7, 2013 at 8:38
17

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
      {
       \fill[radius=#1,\circlecolor] (0,0) circle;
      }
    }
  },
  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:

enter image description here

and

enter image description here

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.

6
  • 1
    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
    Commented Mar 7, 2013 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). Commented Mar 7, 2013 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! Commented Mar 7, 2013 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. Commented Mar 7, 2013 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... Commented Mar 12, 2013 at 10:27

You must log in to answer this question.

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