[Tex/LaTex] TikZ: How to connect nodes and specify line direction

nodestikz-pgf

When drawing a line between two nodes in TikZ, you can specify the anchors on the beginning and end nodes in order to control how the line is drawn.

I would like to specify the anchor on the beginning node, and the angle at which the line is drawn, and let TikZ figure out where it will intercept the end node. The usual use case is when I'm connecting to a "big node" and I would like the lines to be vertical.

I can cheat by placing a coordinate behind the big node, drawing a line to the coordinate instead, and then drawing the big node so that it covers things up. However this won't work if I need to put an arrow on the end.

I could try to calculate the intercept coordinate on the edge of the big node manually, but that seems against the TikZ spirit.

|- doesn't work in this case.

\documentclass[10pt]{minimal}
\usepackage[active,tightpage,pdftex]{preview}
\usepackage{tikz}

\begin{document}
\begin{preview}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  %%%%%%%%%%%%%%%%%%%%%%%%
  %%  LEFT SIDE
  %%%%%%%%%%%%%%%%%%%%%%%%
  \draw (1,0) node[RectObject] (Small1A) {A};
  \draw (3,0) node[RectObject] (Small1B) {B};
  \coordinate (Cheat1A) at (1,2);
  \coordinate (Cheat1B) at (3,2);
  \path [line] (Small1A.north) -- (Cheat1A);
  \path [line] (Small1B.north) -- (Cheat1B);

  % need to draw Big Node last to cover up our cheating
  \draw (2,2) node[RectObject, inner xsep=1cm] (Big1) {Big Node};

  \draw (2,4) node[text width=4cm] (text1) {L: Cheating to control line orientation. Arrows would be hard.};

  %%%%%%%%%%%%%%%%%%%%%%%%
  %%  RIGHT SIDE
  %%%%%%%%%%%%%%%%%%%%%%%%
  \draw (7,2) node[RectObject, inner xsep=1cm] (Big2) {Big Node};
  \draw (6,0) node[RectObject] (Small2A) {A};
  \draw (8,0) node[RectObject] (Small2B) {B};
  \path [arrow] (Small2A) -- (Big2);
  \path [arrow] (Small2B) -- (Big2);

  \draw (7,4) node[text width=4cm] (text2) {R: No cheating. Arrows easy. Can't control line orientation.};
\end{tikzpicture}
\end{preview}
\end{document}

Here's a side-by-side example of "cheating", which shows what I want to be able to do, and "not cheating", which does not do what I want.

side-by-side example of "cheating", which shows what I want to be able to do, and "not cheating", which does not do what I want

The left side of this example shows what I want to be able to do, except I want to do it without cheating, and I'd like to put arrows on the end of the lines.

Best Answer

May be I missunderstood something but I don't see any problem with |- syntax. Using Jake's code:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzstyle{RectObject}=[rectangle,fill=white,draw,line width=0.5mm]
\tikzstyle{line}=[draw]
\tikzstyle{arrow}=[draw, -latex] 

\begin{tikzpicture}
  \draw (7,2) node[RectObject, inner xsep=1cm] (Big2) {Big Node};
  \draw (6,0) node[RectObject] (Small2A) {A};
  \draw (8,0) node[RectObject] (Small2B) {B};
  \draw[arrow] (Small2A.north)--(Small2A|-Big2.south);
  \draw[arrow] (Small2B.north)--(Small2B|-Big2.south);

\end{tikzpicture}
\end{document}

enter image description here

In case someone needs it, this syntax is explained in pgfmanual section "13.3.1 Intersections of Perpendicular Lines".

(<p> |- <q>) or (<q> -| <p>) represent a coordinate at intersection point between a vertical line passing through coordinate <p> and an horizontal passing through <q>. You can use named coordinates like (Small2B|-Big2.south) or numeric pairs like in (2,1 |- 3,4).