7

Hello helpful community

I am trying to create a custom node to use in industrial drawing symbolizing interrupted parts looking like that :

Broken line

Here is my MWE :

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz,esvect}
\usetikzlibrary{calc}

\tikzset{%
    VueInterromp/.style={
      rectangle,
      draw=none,
      minimum height=2cm,
      minimum width=1cm,
      inner sep=2pt,
     append after command={
            (\tikzlastnode.west) -- ($(\tikzlastnode.west)!0.5!(\tikzlastnode.east)+(-0.3,0)$) -- ++(75:1) -- ($(\tikzlastnode.west)!0.5!(\tikzlastnode.east)+(+0.3,0)+(75-180:1)$) -- ($(\tikzlastnode.west)!0.5!(\tikzlastnode.east)+(+0.3,0)$) -- (\tikzlastnode.east)
        }
    }
}

\begin{document} 
\begin{tikzpicture}

\draw[blue,thick] (-3,0) -- (3,0) node[pos=0.3,VueInterromp] {};

\end{tikzpicture}
\end{document}

Question 1

And the result is :

failed broken line

As one could expect, the line appears behind the node. I've tried fill=white, it doesn't seem to do the job since it covers the node itself.

If you have an idea to solve the problem or to improve my code I am listening.

Thank you.

Question 2

I have noticed also that if the line is not horizontal it doesn't work, sloped doesn't cut it, so probably I am not doing it the right way...

\draw[blue,thick] (-3,0) -- (0,3) node[pos=0.3,VueInterromp,sloped] {} ;

gives :

enter image description here

Question 3

To go further : drawing several lines :

\draw[blue,thick] (-3,0) -- (0,0) node[pos=0.3,VueInterromp,sloped] {} -- ++(-1.5,-3) ;

gives :

continuity failed

You see the next line draw starts a bit after the node instead of the end of the line like I'd wish. Got an idea for that ?

Question 3.5

Have a look at what cycle does :

\draw[blue,thick] (-3,0) -- (0,0) node[pos=0.5,VueInterromp,sloped] {} node[pos=1] {} -- ++(-1.5,-3) -- cycle;

gives:

broken cycle

Any contribution is appreciated. Thank you very much.

  • 1) & 2) I would look at how tikz and circuitikz uses to[...] to effect electrical components. 3) That definitely looks like a bug. – John Kormylo May 30 at 13:34
  • @JohnKormylo I tried to have a look at circuitikz's source code, it's utterly unreadable (for me at least). – LMT-PhD May 30 at 14:52
7

It is much straight to customize the style of to path.

VueInterromp has 4 passes about the custom part:

  • location of the center point
  • half width of the part / length of the path (fixed width is ok, but a bit more complicated)
  • angle between the outer side and the path
  • length of outer side

And we need to calculate the slope of the path to get the slop of the outer side.

Proportional width

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
  % #1 center pos, #2 half width/path width, #3 angle, #4 length
  VueInterromp/.style n args={4}{to path={
    let \p1 = (\tikztostart), \p2 = (\tikztotarget) in
    let \n1 = {\x2 - \x1}, \n2 = {\y2 - \y1} in
      \pgfextra{\pgfmathsetmacro{\diffx}{\n1 / 1cm}}
      \pgfextra{\pgfmathsetmacro{\diffy}{\n2 / 1cm}}
    let \n{sqrt} = {sqrt((\diffx)^2 + (\diffy)^2)},
      \n{sign} = {\diffy > 0 ? 1 : (\diffy < 0 ? -1 : (\diffx > 0 ? 1 : -1))} in
    let \n{ang} = {\n{sign} * acos(\diffx / \n{sqrt})} in
    \foreach \pos in {#1} {
      % left part
      -- ($(\tikztostart)!\pos - #2!(\tikztotarget)$)
      -- ++({\n{ang} + #3}:#4)
      -- ($(\tikztostart)!\pos!(\tikztotarget)$)
      % right part
      -- ($(\tikztostart)!\pos + #2!(\tikztotarget) + ({\n{ang} + #3 + 180}:#4)$)
      -- ($(\tikztostart)!\pos + #2!(\tikztotarget)$)
    }
    -- (\tikztotarget) \tikztonodes
  }}
}

\begin{document}
\ExplSyntaxOn
\ExplSyntaxOff
\begin{tikzpicture}
  \foreach \a [evaluate=\a as \x using \a / 14] in {1, ..., 12}{
    \draw (0, 0) \pgfextra{\edef\temp{\x}}
      to[VueInterromp={\temp}{.05}{70}{2mm}] ++({\a * 30}:3)
      to[VueInterromp={.5}{.1}{60}{2mm}] ++({\a *30 - 90}:1); % use ++ instead of +
  }
  \draw (-5, -5) to[VueInterromp={.2, .5, .8}{.08}{70}{1cm}] (5, -5);
\end{tikzpicture}
\end{document}

Fixed width

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
  % #1 center pos, #2 half width, #3 angle, #4 length
  VueInterromp/.style n args={4}{to path={
    let \p1 = (\tikztostart), \p2 = (\tikztotarget) in
    let \n1 = {\x2 - \x1}, \n2 = {\y2 - \y1} in
      \pgfextra{\pgfmathsetmacro{\diffx}{\n1 / 1cm}}
      \pgfextra{\pgfmathsetmacro{\diffy}{\n2 / 1cm}}
    let \n{sqrt} = {sqrt((\diffx)^2 + (\diffy)^2)},
      \n{sign} = {\diffy > 0 ? 1 : (\diffy < 0 ? -1 : (\diffx > 0 ? 1 : -1))} in
    let \n{ang} = {\n{sign} * acos(\diffx / \n{sqrt})} in
    \foreach \pos in {#1} {
      % left part
      -- ($(\tikztostart)!\pos!(\tikztotarget) - (\n{ang}:#2)$)
      -- ++({\n{ang} + #3}:#4)
      -- ($(\tikztostart)!\pos!(\tikztotarget)$)
      % right part
      -- ($(\tikztostart)!\pos!(\tikztotarget) + (\n{ang}:#2) + ({\n{ang} + #3 + 180}:#4)$)
      -- ($(\tikztostart)!\pos!(\tikztotarget) + (\n{ang}:#2)$)
    }
    -- (\tikztotarget) \tikztonodes
  }}
}

\begin{document} 
\begin{tikzpicture}
  \foreach \a [evaluate=\a as \x using \a / 14] in {1, ..., 12}{
    \draw (0, 0) \pgfextra{\edef\temp{\x}}
      to[VueInterromp={\temp}{1.5mm}{70}{2mm}] ++({\a * 30}:3)
      to[VueInterromp={.5}{.1}{60}{2mm}] ++({\a *30 - 90}:1); % use ++ instead of +
  }
  \draw (-5, -5) to[VueInterromp={.2, .5, .8}{.8cm}{70}{1cm}] (5, -5);
\end{tikzpicture}
\end{document}

enter image description here

Change log

  • 5.31-1 change atan2 to acos to judge the direction of the path
  • 5.31-2 change example
  • 6.1 add for loop to draw multiple vueinterromps
| improve this answer | |
  • I was hoping there would be a generic way to create a node that would follow a line with 'sloped' and avoid calculating by hand, but I guess it's a too precise need for it to be user-friendly. Anyway, thank you very much for your work, I'll test it out and use it. – LMT-PhD May 31 at 12:37
  • I understand '(\A)!#1!(\B)' but not quite '(\A)!#1 + #2!(\B)' , could you explain to me what it evaluates ? I suppose '(\A)!#1' if a fraction of the vector \A , but #2 is a length.... – LMT-PhD May 31 at 12:47
  • 1
    In Proportional width version, #2 is the ratio of half of the width of the custom part to the length of the whole path, so #1 - #2 is the end of part near to the start point of the path, and #1 + #2 is the end of the part near to the end point of the path – ZhiyuanLck May 31 at 14:00
  • Can I put two/several on the same path ? or do I have to use intermediate points ? – LMT-PhD May 31 at 14:54
  • 1
    Now passing a pos list is supported. – ZhiyuanLck Jun 1 at 4:31

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.