# TikZ: Centering dash pattern along the path

In this MWE, I would like the dash pattern to be symmetric about the center of the right side.

Additionally, the code doesn't work after some experimentation, so I would like to know what is wrong with it.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\tikzset{domain/.style={
append after command={
\pgfextra{
\draw[dash pattern={on 7pt off 2pt on 1pt off 2pt}] (\tikzlastnode.north east) -- (\tikzlastnode.south east);
\draw[] (\tikzlastnode.south east) -| (\tikzlastnode.north west) -- (\tikzlastnode.north east);
}}}}

\begin{tikzpicture}
\node at (3,0) [domain, minimum size = 3cm] (eq) {$y=mx+c$};
\draw[black, thick, ->] (eq.west) -- ++(-40pt,0pt) node[above ,align=center, ]{text};
\end{tikzpicture}
\end{document}


• Why do you expect that to centrally align? – cfr Jun 28 '18 at 1:14
• @cfr I don't expect, however, I need the dash pattern to be symmetric with respect to the right side center. – Diaa Jun 28 '18 at 1:15

You need to pick a name other than domain, which is already in use. I'm not sure why you expect the dash to be centred especially, but here's a way:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}

\tikzset{my domain/.style={
append after command={
\pgfextra{
\draw (\tikzlastnode.east) +(0,.5pt) -- +(0,-1pt) +(0,-2pt) |- (\tikzlastnode.south west) |- (\tikzlastnode.north east) -- ([yshift=1.5pt]\tikzlastnode.east);
}}}}

\begin{tikzpicture}
\node at (3,0) [my domain] (eq) {$y=mx+c$};
\draw[black, thick, ->] (eq.west) -- ++(-40pt,0pt) node[above ,align=center, ]{text};
\end{tikzpicture}
\end{document}


# EDIT

If you need the whole pattern to display for larger nodes, just cut the drawing into two pieces:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}

\tikzset{my domain/.style={
append after command={
\pgfextra{
\draw (\tikzlastnode.east) ++(0,.5pt) -- ++(0,-1pt) ++(0,-2pt) edge [dash pattern=on 7pt off 2pt on 1pt off 2pt] (\tikzlastnode.south east) (\tikzlastnode.south east) -| (\tikzlastnode.north west) -- (\tikzlastnode.north east);
\draw [dash pattern=on 7pt off 2pt on 1pt off 2pt] (\tikzlastnode.east) ++(0,2.5pt) -- (\tikzlastnode.north east);
}}}}

\begin{tikzpicture}
\node at (3,0) [my domain, align=center] (eq) {$y=mx+c$\\a\\b};
\draw[black, thick, ->] (eq.west) -- ++(-40pt,0pt) node[above ,align=center, ]{text};
\draw[black, thick, ->] (eq.east) -- ++(40pt,0pt) node[above ,align=center, ]{text};
\end{tikzpicture}
\end{document}


• Thanks for your answer. Your solution won't work in case the node size is big. For example, when setting the node size to be minimum size=3cm, I would like to get the full pattern as I specified while making it symmetric about the right side center. – Diaa Jun 28 '18 at 1:27
• @Diaa Well, you didn't say that in the question. You just said you wanted to centre 'the dash'. – cfr Jun 28 '18 at 1:28
• No problem. You want something like this ^^? @Diaa This should work with arbitrary heights/depths of nodes, but note that it is always centred on the .center and not the centre of the text line or the baseline or anything like that. – cfr Jun 28 '18 at 1:39
• If you don't mind, I noticed that when drawing a horizontal arrow from the right side \draw[black, thick, ->] (eq.east) -- ++(40pt,0pt) node[above ,align=center, ]{text};, the arrow base doesn't perfectly align with the 1pt dash as shown here. What is the possible reason of this? – Diaa Jun 28 '18 at 2:09
• @Diaa Because I drew a 1.5pt line rather than a 1pt line by mistake. Fixed above. – cfr Jun 28 '18 at 2:19