4

I would like to split a path evenly (in three parts) in a way that works when there is an arrow tip that hides part of the path. In other words, I would like the segments of each color to be of equal length in the following document:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
    
\newcommand{\withthirdscoordinates}[1]{
    \begin{scope}
        \coordinate (n0) at (\tikzinputsegmentfirst);
        \coordinate (n3) at (\tikzinputsegmentlast);
        \coordinate (n1) at (barycentric cs:n0=2,n3=1);
        \coordinate (n2) at (barycentric cs:n0=1,n3=2);
        #1
    \end{scope}
}

\tikzset{
  mystyle/.style={
    decoration={
      show path construction,
      lineto code={
        \withthirdscoordinates{
          \draw [-, color=red] (n0) -- (n1);
          \draw [-, color=green] (n1) -- (n2);
          \draw [color=blue] (n2) -- (n3);
        }
      },
    },
    decorate
  }
}

\tikzset{
  every node/.style={circle, draw}
}

\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[-{Triangle[open]}, mystyle] (A) -- (B);
\end{tikzpicture}

\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[-{Triangle[open, length=35pt]}, mystyle] (A) -- (B);
\end{tikzpicture}

\end{document}   
4
  • Sounds like a meta decoration thing.
    – Symbol 1
    Aug 20, 2021 at 18:14
  • In your example, your path is just a straight line. Is it sufficient to provide an answer that works just for single straight lines? (polylines would be a little more complicated, and bézier curves more complicated still) Aug 20, 2021 at 20:00
  • @AndrewStacey Yes, I only need to draw straight arrows
    – xavierm02
    Aug 21, 2021 at 6:05
  • 1
    The perfect answer is this one: tex.stackexchange.com/a/623891/101651
    – CarLaTeX
    Nov 25, 2021 at 11:47

1 Answer 1

3

This answer is very partial since it works only for horizontal straight lines, not for sloped ones.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}
    
\tikzset{
  mystyle/.style={
    -{Triangle[open, length=#1]},
    decoration={
      show path construction,
      lineto code={
        \coordinate (n0) at (\tikzinputsegmentfirst);
        \coordinate (n3) at ([xshift=-#1]\tikzinputsegmentlast);
        \coordinate (n4) at (\tikzinputsegmentlast);
        \coordinate (n1) at (barycentric cs:n0=2,n3=1);
        \coordinate (n2) at (barycentric cs:n0=1,n3=2);
        \draw [-, color=red] (n0) -- (n1);
        \draw [-, color=green] (n1) -- (n2);
        \draw [-, color=blue] (n2) -- (n3);
        \draw (n3) -- (n4);
        }
      },
    decorate
  },
  mystyle/.default=5pt,
  every node/.style={circle, draw}
}

\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[mystyle] (A) -- (B);
\end{tikzpicture}

\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[mystyle={35pt}] (A) -- (B);
\end{tikzpicture}
\end{document}

enter image description here

See also here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .