Skip to content
Permalink
Browse files

Stretchable dash patterns #629

  • Loading branch information...
hmenke committed Jul 10, 2019
1 parent 9d18b41 commit d640cd539f9e2735e25709d88ecdd3e6106346c1
@@ -361,6 +361,25 @@ \subsubsection{Graphic Parameters: Dash Pattern}
%
\end{key}

\begin{key}{/tikz/dash expand off}
Makes the |off| part of a dash pattern expandable such that it can stretch.
This only works when there is a single |on| and a single |off| field and
requires the |decorations| library. Right now this option has to be
specified on the path where it is supposed to take effect after the |dash
pattern| option because the dash pattern has to be known at the point where
it is applied.
%
\begin{codeexample}[]
\begin{tikzpicture}[|-|, dash pattern=on 4pt off 2pt]
\draw [dash expand off] (0pt,30pt) -- (26pt,30pt);
\draw [dash expand off] (0pt,20pt) -- (24pt,20pt);
\draw [dash expand off] (0pt,10pt) -- (22pt,10pt);
\draw [dash expand off] (0pt, 0pt) -- (20pt, 0pt);
\end{tikzpicture}
\end{codeexample}
%
\end{key}

As for the line thickness, some predefined styles allow you to set the dashing
conveniently.

@@ -148,6 +148,42 @@
\expandafter\def\expandafter\tikz@dashpattern\expandafter{\tikz@dashpattern{#1}}%
\pgfutil@ifnextchar\@nil{\pgfutil@gobble}{\tikz@scandashon}}%

% use a decoration to expand the `off' section of a dash pattern.
% https://tex.stackexchange.com/a/133357
\tikzset{
dash expand off/.code={%
\ifcsname tikz@library@decorations@loaded\endcsname\else
\tikzerror{You need \string\usetikzlibrary{decorations} for ``dash offexpand''}%
\fi
\tikz@addoption{%
\pgfgetpath\currentpath
\pgfprocessround{\currentpath}{\currentpath}%
\pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
% Parse \on and \off from the current path
\edef\on{\expandafter\pgfutil@firstoftwo\tikz@dashpattern}%
\edef\off{\expandafter\pgfutil@secondoftwo\tikz@dashpattern}%
% \dashphase = max(\on - \dashphase, 0)
\pgfmathsubtract@{\on}{\tikz@dashphase}%
\pgfmathmax@{\pgfmathresult}{0}%
\edef\dashphase{\the\dimexpr\pgfmathresult pt\relax}%
% \rest = \pgf@decorate@totalpathlength - \on
\edef\rest{\expandafter\pgf@sys@tonumber\dimexpr\pgf@decorate@totalpathlength - \on + 2\dimexpr\dashphase\relax\relax}%
% \onoff = \on + \off
\edef\onoff{\expandafter\pgf@sys@tonumber\dimexpr\on+\off\relax}%
% \nfullonoff = max(floor(\rest/\onoff), 1)
\pgfmathdivide@{\rest}{\onoff}%
\pgfmathfloor@{\pgfmathresult}%
\pgfmathmax@{\pgfmathresult}{1}%
% \offexpand = max(\rest/\nfullonoff - \on, \off)
\pgfmathdivide@{\rest}{\pgfmathresult}%
\pgfmathsubtract@{\pgfmathresult}{\on}%
\pgfmathmax@{\pgfmathresult}{\off}%
\edef\tikz@marshal{\noexpand\pgfsetdash{{\on}{\pgfmathresult}}{\dashphase}}%
\tikz@marshal
}%
}
}

\tikzoption{draw opacity}{\tikz@addoption{\pgfsetstrokeopacity{#1}}}%

% Double draw options

0 comments on commit d640cd5

Please sign in to comment.
You can’t perform that action at this time.