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}
The problem is that \gpdashlength
is a length, not a macro, and then it is not expanded, but replaced by a length value. So, if you want to change that value, an assignment has to be made before the length is evaluated, and you cannot do it inside .code
key because, as you guessed, it is not executed before evaluating the style.
A solution is to change \gpdashlength
from a lengt to a macro, i.e:
\def\gpdashlength{0.5*\pgflinewidth}
This way, when this macro is found as part of the dash pattern, it is expanded, so the current value of \pgflinewidth
is used at that point. You have to change the dash pattern to include the multiplication sign (*
) before \gpdashlength
. I.e:
\documentclass[tikz]{standalone}
\begin{document}
\def\gpdashlength{0.5*\pgflinewidth}
\begin{tikzpicture}
\tikzset{
gp path/.style={dash pattern=on 7.5*\gpdashlength
off 7.5*\gpdashlength}}
\draw[thin, gp path] (1.012,3.528)--(11.947,5.469);
\draw[very thick, gp path] (1.012,2.557)--(11.947,6.440);
\end{tikzpicture}
\end{document}
Which produces:

Note that this works because tikz parses the mathematical expression containing *
. That is, after all expansions are performed, tikz "sees" the following dash pattern (assuming that, for example, \pgflinewidth
is 0.8pt
in this example):
dash pattern=on 7.5*0.5*0.8pt off 7.5*0.5*0.8pt
Tikz parser is able to evaluate that product. Otherwise this won't work. You cannot generally change a length register by a macro as I did here.
Best Answer
The various pre-defined dash patterns are documented in section 15.3.2 Graphic Parameters: Dash Pattern of the manual (for version 3.0.1.a dated 29 August 2015). They are
dotted
,dashed
,dash dot
anddash dot dot
. Each of these have denser and looser variants, e.g.densely dashed
andloosely dotted
. Equivalently for the others.In addition you can specify a custom pattern using e.g.
dash pattern={on 4pt off 1pt on 2pt off 3pt}
, which I guess is self explanatory.Addendum
If you want to put a line like this in the text, and aligned better to the surrounding text, then you can add the
baseline=<length>
option to thetikzpicture
. By default the bottom end of thetikzpicture
is placed on the baseline of the surrounding text. If you addbaseline=10pt
then thetikzpicture
will be placed so that y=10pt in its internal coordinate system is on the baseline of the surrounding text.Here is an example.
\tikz
is a short form of thetikzpicture
environment, intended for simple pictures placed in the text.