2

I would like to draw a diagram with boxes of different heights such that the bottom of each box is on the same height. Additionally, I would like to connect the nodes by horizontal arrows. My approach is to use relative positioning (positioning library).

Here is a MWE that illustrates the problem:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz,pgfplots}
\usetikzlibrary{positioning}

\tikzset{ % from http://tex.stackexchange.com/a/49909/78282
    flowbox/.style={
        rectangle, rounded corners, minimum width=1cm, minimum height=1cm,text centered, draw=black
    },
    stdarrow/.style={->,>=stealth}
}

\begin{document}
\begin{tikzpicture}[node distance=1cm]
    \node[center,flowbox] (A) {A};
    \node[flowbox,right=of A] (B) {\tikz \draw (0,0)--(0,3);};
    \draw [stdarrow] (A) -- (B);
\end{tikzpicture}
\end{document}

first version Note that here, the nodes are centered vertically, the arrow is as I want it.

My idea was to use the base for relative positioning:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz,pgfplots}
\usetikzlibrary{positioning}

\tikzset{ % from http://tex.stackexchange.com/a/49909/78282
    flowbox/.style={
        rectangle, rounded corners, minimum width=1cm, minimum height=1cm,text centered, draw=black
    },
    stdarrow/.style={->,>=stealth}
}

\begin{document}
\begin{tikzpicture}[node distance=1cm]
    \node[flowbox] (A) {A};
    \node[flowbox, base right=of A] (B) {\tikz \draw (0,0)--(0,3);};
    \draw [stdarrow] (A) -- (B);
\end{tikzpicture}
\end{document}

second version The problem is that here, the bottom of the second box is higher than that of the first one (since it takes the text as a reference) and the arrow is not horizontal. As I said, I would like to have the bottom edges of the two boxes on the same horizontal height and the arrow starting from the vertical center of box A going horizontal to the right. How is this feasible?

1 Answer 1

7

The following code should give the desired result (comments below):

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{positioning}

\tikzset{ % from https://tex.stackexchange.com/a/49909/78282
    flowbox/.style={
        rectangle, rounded corners, minimum width=1cm, minimum height=1cm,text centered, draw=black
    },
    stdarrow/.style={->,>=stealth}
}

\begin{document}
\begin{tikzpicture}[node distance=2cm]
    \coordinate (A-Coord) at (0,0);
    \coordinate[right=of A-Coord] (B-Coord);
    \node[flowbox, anchor=south] (A-Node) at (A-Coord) {A};
    \node[flowbox, anchor=south] (B-Node) at (B-Coord) {\tikz \draw (0,0)--(0,3);};
    \draw [stdarrow] (A-Node.east) -- (A-Node-|B-Node.west);
\end{tikzpicture}

\end{document}

Result:

enter image description here

Comments:

  • To adjust the placing, I defined two coordinates and the node option anchor=south (probably there exist better solutions for this aspect).
  • To force the arrow to be horizontal, I used a orthogonal coordinate system identifier (A-Node|-B_Node.west) (see this answer of percusse)
  • To keep the spacing, I had to adjust the node distance [node distance=2cm]
4
  • 1
    Very clever solution, thanks a lot! However, I need to remove the center option from the nodes, otherwise I get the error "I do not know the key '/tikz/center' and I am going to ignore it. Perhaps you misspelled it.". No real problem though, since it is still working as intended. Commented Mar 2, 2016 at 23:58
  • @riddleculous Your right! I just removed center option, since it was the artefact from a first (unsuccessfull) attempt. Thanks for spotting that and mentioning that!
    – crixstox
    Commented Mar 3, 2016 at 0:46
  • I have a strange follow-up problem (should I post a new question for this?). When labeling the edge of the arrow in your example using \draw [stdarrow] (A-Node.east) edge["AB"] (A-Node-|B-Node.west);, I get an unwanted upwards arrow tip at the start of the arrow as marked in red here (for this edge command, I need to load \usetikzlibrary{quotes}). I don't understand why this makes a difference. Commented Mar 3, 2016 at 13:26
  • problem solved: I added tips=proper as an option to my stdarrow/.style, see pgfmanual Commented Mar 3, 2016 at 13:37

You must log in to answer this question.

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