11

Often, when drawing block diagrams i want to add an 'input' to a node with a perpendicular line (drawn in red below).

Is there a way to achieve this in a robust way without using calc, etc.? Ideally i want to use something like \draw (input) -| (node).

\begin{tikzpicture}
\node[draw,minimum height=4cm,minimum width=1cm] (node) at (0,0) {ABC};
\draw[->] (-2,1) node[anchor=east] {Some input 1} -- (node);
\draw[->,red] (-2,-1) node[anchor=east] {Some input 2} -- +(1.5,0);
\end{tikzpicture}

example

3 Answers 3

14

You can use the -| and |- operators to form coordinates. Here, you could provide the y value explicity with n.east by naming the node containing Some input 2 as n. Another way is to use the to path operation which automatically sets \tikztostart to the start coordinate of the to operation:

\documentclass[tikz, border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}
\node[draw, minimum height=4cm, minimum width=1cm] (node) at (0,0) {ABC};
\draw[->] (-2,1) node[anchor=east] {Some input 1} -- (node);
\draw[->, red] (-2,-1) node[anchor=east] {Some input 2}
  to (\tikztostart -| node.west);
\end{tikzpicture}
\end{document}

enter image description here

To address some comments, the following shows that the problem is not well-defined with “general shapes”:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc, shapes.misc}

\begin{document}
\begin{tikzpicture}
\node[draw, minimum height=2cm,
      rounded rectangle, rounded rectangle west arc=concave] (n) {Some stuff};
\node[circle, inner sep=1.5pt] (foo) at (n.west) {foo};
\draw[red!60!black, ->]
  let \p1=($(n.north)-(n.center)$) in
  (foo) foreach \angle in {-90, -75, ..., 90} { edge +(\angle:\y1) };
\end{tikzpicture}
\end{document}

enter image description here

But what if?..

So, you have a rotated rectangular node and still want to play? The TikZ calc library can orthogonally project a point p on a line (ab) with its ($(a)!(p)!(b)$) syntax:

\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node[draw, rotate=30] (n) {Some stuff};
\node[draw] (foo) at (-1,0.5) {foo bar};
\foreach \anch in {270, 300, 320, 330, 337, 350} {
  \draw[very thin, ->] (foo.\anch)  --
    ($(n.north west)!(foo.\anch)!(n.north east)$);
}
\end{tikzpicture}
\end{document}

enter image description here

8
  • 1
    Great answer. Luckily, the desired point on the node border is just vertical down. -Do you have any suggestions for a solution, if the ABC node is not rectangular - e.g. a circle? Commented Aug 8, 2021 at 17:12
  • Would really be interesting to extend the idea to arbitrary node shapes. Commented Aug 8, 2021 at 17:17
  • @hpekristiansen Well, if the ABC node is a circle, a simple -- (node) is enough because lines that go through the center of a circle are perpendicular to the circle. For any shape in general, I'm afraid I don't have much to offer, except to say this is not well defined: there can very well be several points that satisfy the perpendicularity condition.
    – frougon
    Commented Aug 8, 2021 at 17:46
  • But -- (node) will not go horizontal to the circle. See also my answer. Commented Aug 8, 2021 at 17:55
  • 1
    @hpekristiansen Right, but the question title says “perpendicular to TikZ node”, not “horizontal.” :-)
    – frougon
    Commented Aug 8, 2021 at 18:05
8

Complementary to the very good answer from frougon, if you don't want to use the \tikztostart macro (which is perfectly fine), you can also name the node containing Some input 2 (here, by aux) and use this name in the construction of the intersecting point (aux-|node.west). This is sometime useful when drawing multiple broken lines.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
    \begin{tikzpicture}
        \node[draw,minimum height=4cm,minimum width=1cm] (node) at (0,0) {ABC};
        \draw[->] (-2,1) node[anchor=east] {Some input 1} -- (node);
        \draw[->,red] (-2,-1) node[anchor=east] (aux) {Some input 2} -- (aux-|node.west);
    \end{tikzpicture}
\end{document}

The output is the same.

4

This is a stupid answer:

\documentclass[tikz]{standalone}        
\begin{document}
\begin{tikzpicture}
\node[draw,circle, minimum height=4cm,minimum width=1cm] (node) at (0,0) {ABC};
\draw[<-] (node.120) -- +(-2,0) node[anchor=east] {Some input 1};
\draw[<-] (node.150) -- +(-2,0) node[anchor=east] {Some input 2};
\draw[<-] (node.190) -- +(-2,0) node[anchor=east] {Some input 3};
\draw[<-] (node.240) -- +(-2,0) node[anchor=east] {Some input 4};
\end{tikzpicture}
\end{document}    

Input nodes pointing horizontal to circle

This method works, even if the "ABC" node is not rectangular. -but the huge problem is that the position of the input node is not set by an explicit coordinate.

You must log in to answer this question.

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