[Tex/LaTex] Draw dotted line with circled dots and automatic spacing

tikztikz-pgftikz-styles

I'm trying to create dotted lines with where every dot has a defined size and a circle around it, with also a defined size.

First I applied this answer to create a dash pattern:

\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
    }
}

Then I plot these dotted lines in black – white – black on top of each other to achieve the desired effect. How can I change the tikzset to get this in one step?

The next problem is that it requires a defined dot-spacing which is fine if I'd knew the distance between the endpoints of my line, which I usually don't, so I'm fiddling around with the spacing manually. But I'd like to have a circled dot at the beginning and end node and the rest of the dots equally spaced inbetween. How can I achieve that equal distribution? Is it possible to define, say, exactly 11 dots on that line, equally distributed?

Thank you!


MWE

\documentclass{article}
\usepackage{tikz}

\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{document}
    \begin{tikzpicture}

    \coordinate (A1) at (1,1);
    \coordinate (B1) at (5,1);
    \coordinate (A2) at (1,1.5);
    \coordinate (B2) at (5,1.5);

    % for reference frame
    \draw [black, thick]   (A1.center) -- (B1.center) -- (B2.center) -- (A2.center) -- (A1.center);

    % three dot layers #1
    \draw [black, dot diameter=3mm, dot spacing=3.8mm, dots]   (A1.center) -- (B1.center);
    \draw [white, dot diameter=2.5mm, dot spacing=3.8mm, dots]  (A1.center) -- (B1.center);
    \draw [black, dot diameter=1mm, dot spacing=3.8mm, dots]    (A1.center) -- (B1.center);

    % three dot layers #2
    \draw [black, dot diameter=3mm, dot spacing=4mm, dots]   (A2.center) -- (B2.center);
    \draw [white, dot diameter=2.5mm, dot spacing=4mm, dots]  (A2.center) -- (B2.center);
    \draw [black, dot diameter=1mm, dot spacing=4mm, dots]    (A2.center) -- (B2.center);


    \end{tikzpicture}
\end{document}

enter image description here

The solid line is just for orientation, there shouldn't be a connect between the dots. The upper dotted line is manually adjusted to get the correct spacing.

Best Answer

This answer proposes to use preaction and|or postaction to define a style which draws all three layers in one command but doesn't solve the problem about equally distributed 11 dots between two points.

\documentclass{article}
\usepackage{tikz}

\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
    },
     mydots/.style={
        dot spacing=3.8mm,
        preaction={draw=black,
                        dot diameter=3mm,
                        dots},
        draw = white,
        dot diameter=2.5mm,
        dots,
        postaction={draw=black,
                        dot diameter=1mm,
                        dots},
     }
}
\makeatother

\begin{document}
    \begin{tikzpicture}

    \draw [mydots]   (0,0) -- (4,0);

    \draw [mydots]   (0,1) .. controls (3,1) and (4,2).. (3,3);

    \end{tikzpicture}
\end{document}

enter image description here

Update: Parametric version: \mydots={dot dimension}{distance}

\documentclass[tikz,border=2mm]{standalone}

\makeatletter
\tikzset{
    dot diameter/.store in=\dot@diameter,
    dot diameter=3pt,
    dot spacing/.store in=\dot@spacing,
    dot spacing=10pt,
    dots/.style={
        line width=#1,
        line cap=round,
        dash pattern=on 0pt off \dot@spacing
    },
     mydots/.style 2 args={
        dot spacing=#2,
        preaction={draw=black,
                        dots=#1},
        draw = white,
        dots=.8*#1,
        postaction={draw=black,
                        dots=.333*#1},
     }
}
\makeatother

\begin{document}
    \begin{tikzpicture}

    \draw [mydots={3mm}{3.8mm}]   (0,0) -- (4,0);

    \draw [mydots={2mm}{5mm}]   (0,1) .. controls (3,1) and (4,2).. (3,3);

    \end{tikzpicture}
\end{document}

enter image description here

Related Question