2

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.

1 Answers1

3

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

Ignasi
  • 136,588
  • But it seems that I can just predefine the dot diameters of pre- and post-action, right? The creating of the variable dot innerdiameter/.store in=\dot@innerdiameter, and use it with pre- and post-action does not seem to work. Also preaction={draw=black, dot diameter=\dot@diameter, dots}, does not work. Somehow the size of the dots must be controllable, either relative to each other by specifying just one of the dot sizes or all three absolute. – Robert Seifert Nov 24 '15 at 12:12
  • @thewaywewalk I'm sorry but I don't understand your question. In any case I've updated the answer with a parametric dots style. Is something like this what you want? – Ignasi Nov 24 '15 at 17:31
  • you haven't updated the answer like mentioned in you comment as there is no edit, have you? – Robert Seifert Dec 04 '15 at 06:44
  • @thewaywewalk: Sorry, I did it, but probably forgot to click save button. Now it's ok. – Ignasi Dec 04 '15 at 07:50
  • This works great. But the automatic sapcing is not trivial to realize, is it? – Robert Seifert Dec 04 '15 at 11:23