3

In tikz, I can make an arrow connecting two nodes with straight lines and a 90 degree corner by using |- or -|. But is there an easy way to make a straight horizontal/vertical segment and then a 45 degree (or radially inward or some other angle) segment, like the red line in the figure?

\documentclass[12pt]{extarticle}
\usepackage[paperwidth=7.4cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzstyle{circle1} = [draw, circle, minimum size = 1.5mm]

\begin{document}
\begin{tikzpicture}[auto, node distance = 1.2cm]
        \node (first) [circle1] {first};
        \node (second) [circle1, right of = first, node distance = 3.5 cm, below=2.5 cm] {second};

        \draw[->, thick] (first.0) -- node [above] {$--$} (second.135);
        \draw[->, thick] (first.0) |- node [below] {$|-$} (second.160);
\end{tikzpicture}
 \end{document}

Output from sample code with desired addition in red

3
\documentclass[12pt]{extarticle}
\usepackage[paperwidth=8.6cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzset{circle1/.style={draw, circle, minimum size = 1.5mm}}
\tikzset{pics/perp/.style={code={\draw[-] (0,0) -- (1,0) (0,-0.5) -- (0,0.5);}},
pics/line/.style={code={\draw[-] (0,0) -- (1,0);}}}

\begin{document}
\begin{tikzpicture}[auto]
        \node (first) [circle1] {first};
        \node (second) [circle1, below right=1.5cm and 5.5cm of first] {second};
        \draw[->, thick] (first.0) -- pic[yshift=3mm,scale=0.3]{line} (second.135);
        \draw[->, thick] (first.0) |- pic[yshift=-3mm,scale=0.3]{perp} (second.160);
        \draw[->, thick,red] let \p1=(first.east), \p2=(second.120) in
        (first.east) -- ++({\x2-\x1-\y1+\y2},0) -- (second.120);
\end{tikzpicture}
\end{document}

enter image description here

After the suggestion by EL_DON to generalize: here is a version with an edge style.

\documentclass[12pt]{extarticle}
\usepackage[paperwidth=8.6cm, paperheight=5.55cm, top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}
\tikzset{circle1/.style={draw, circle, minimum size = 1.5mm}}
\tikzset{pics/perp/.style={code={\draw[-] (0,0) -- (1,0) (0,-0.5) -- (0,0.5);}},
pics/line/.style={code={\draw[-] (0,0) -- (1,0);}}}


\begin{document}

\begin{tikzpicture}[auto,connect with angle/.style=
{to path={let \p1=(\tikztostart), 
\p2=(\tikztotarget) in -- ++({\x2-\x1-(\y1-\y2)*tan(#1-90)},0) -- (\tikztotarget)}}]
        \node (first) [circle1] {first};
        \node (second) [circle1, below right=1.5cm and 5.5cm of first] {second};
        \draw[->, thick] (first.0) -- pic[yshift=3mm,scale=0.3]{line} (second.135);
        \draw[->, thick] (first.0) |- pic[yshift=-3mm,scale=0.3]{perp} (second.160);
        \draw[red,-latex] (first.east) edge[connect with angle=135] (second.120);
        \draw[blue,-latex] (first.east) edge[connect with angle=105] (second.120);
\end{tikzpicture}
\end{document}

enter image description here

Here, by EL_DON's convention, the angle is the angle between the two stretches. Other conventions may be accounted for by adjusting tan(#1-90) appropriately.

  • 1
    You could generalize this by defining \newcommand\exitangle{135} and then changing ({\x2-\x1-\y1+\y2},0) to ({\x2-\x1-(\y1-\y2)*tan(\exitangle-90)},0) and also using \exitangle in contexts like \p2=(second.\exitangle). – EL_DON Jun 28 '18 at 20:37
  • 1
    @EL_DON I added a generalization, hope it is OK. ;-) – marmot Jun 28 '18 at 21:07
  • 1
    That's great. Setting up connect with angle like this would be a lot cleaner in a picture that needed several of these angled arrows. – EL_DON Jun 28 '18 at 21:16
2

with use of intersections library:

\documentclass[tikz, margin=4mm]{standalone}
\usetikzlibrary{intersections,
                positioning,
                shapes}
\tikzstyle{circle1} = [draw, circle, minimum size = 1.5mm]

\begin{document}
    \begin{tikzpicture}[auto,
node distance = 11mm and 22mm]
\node (first) [circle1] {first};
\node (second) [circle1, below right=of first] {second};
%
\draw[->, thick] (first) -- node [above] {$--$} (second);
\draw[->, thick] (first) |- node [below] {$|-$} (second);
%
\path[name path=A] (first) -- (first -| second);
\path[name path=B] (second) -- ++ (135:33mm);
\draw [name intersections={of=A and B, by=a},very thick, blue]
    (first) -- (a) -- (second);
\end{tikzpicture}
 \end{document}

enter image description here

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.