14

When a closed curve is dashed, the dash spacing at the start/end point can be very unfortunate. Is there an option to tell TikZ that it should slightly adapt the dash spacing to avoid such cases? (I could of course do this myself, but I have many such curves thus it is tedious. )

Here is an example problem:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw [dashed, line width=1pt]circle(0.655cm);
\end{tikzpicture}
\end{document}

enter image description here

1
10

Use dash pattern

\documentclass[border=5]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw [line width=.5pt,dash pattern=on 1pt off 2pt]circle(0.655cm);
\end{tikzpicture}
\end{document}

enter image description here

See also pag. 168 of the manual

3
  • 2
    That's the "I could of course do it myself" solution.
    – gTcV
    Nov 28 '14 at 13:11
  • @gTcV Yeah, sorry, I actually missed that part of your question. Would you like me to delete my answer so that you get more visibility?
    – Old Nick
    Nov 28 '14 at 14:24
  • For those who do not know dash pattern, it explicitly shows the "by hand" solution. So I guess your answer does contribute something. After all, you've been upvoted, so it obviously is useful to someone.
    – gTcV
    Nov 28 '14 at 15:09
6

Here is a solution:

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}

\tikzset{
    cycled dash pattern/.code args={on #1 off #2}{
        % Use csname so catcode of @ doesn't have do be changed.
        \csname tikz@addoption\endcsname{%
            \pgfgetpath\currentpath%
            \csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
            % Length of path
            \pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname}\let\lc=\pgfmathresult%
            % Length of pattern
            \pgfmathparse{#1+#2}\let\lp=\pgfmathresult%
            % Scaling factor for pattern
            \pgfmathparse{\lc/(\lp*round(\lc/\lp))}\let\f=\pgfmathresult%
            % Actually scale the pattern
            \pgfmathparse{#1*\f}\let\on=\pgfmathresult%
            \pgfmathparse{#2*\f}\let\off=\pgfmathresult%
            % Tell PGF to dash this line
            \pgfsetdash{{\on}{\off}}{0pt}}%
    }
}

\begin{document}
\begin{tikzpicture}
    % The built-in version for comparison
    \draw [line width=1pt, dash pattern=on 4pt off 4pt] (0,0) circle(0.655);
    % Our version with automatically adapted pattern length
    \draw [line width=1pt, cycled dash pattern=on 4pt off 4pt] (2,0) circle(0.655);
\end{tikzpicture}
\end{document}

Most of the code is taken from Can TikZ dashed lines emulate PSTricks dashed lines?. I only adapted it such that the dashing starts with an on and ends with an off, in contrast to the referenced answer where the dashing starts and ends with an on).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.