# Calculating the minimum height for a node based on the size of other nodes

Consider the following example:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{xcolor}

\begin{document}

\def\mydist{1mm}
\begin{tikzpicture}[
mystyle/.style={
rectangle,
rounded corners,
draw=black,
very thick,
text width=2cm,
}
]
\node [mystyle] (A) {A\\text\\text\\text};
\node [mystyle, anchor=north] (B) at ($(A.south) - (0,\mydist)$) {B\\text\\text};
\node [mystyle, anchor=south west] (C) at ($(B.south east) + (\mydist,0)$) {C\\text};

% here is the problem
\node [mystyle, anchor=south, red] (D) at ($(C.north) + (0,\mydist)$) {D\\???};
\end{tikzpicture}

\end{document}


How can I calculate the minimum height for node D so that the upper edges of the nodes A and D are aligned?

Or should I use a different approach?

-
This is very similar to Fit only the height on a node. Also check out the positioning library which makes placing nodes relative to each other very easy. – Qrrbrbirlbel Apr 9 '13 at 18:32

Here's one possibility, using the let syntax; the node's text depth is used to give the right size:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage{xcolor}

\begin{document}

\begin{tikzpicture}[
mystyle/.style={
rectangle,
rounded corners,
draw=black,
line width=1pt,
text width=2cm,
},
node distance=2mm
]
\node [mystyle] (A) {A\\text\\text\\text\\text};
\node [mystyle,below=of A] (B) {B\\text\\text};
\node [mystyle, right=of B.south east,anchor=south west] (C) {C\\text};
\path let \p1=([yshift=2mm]C.north), \p2=(A.north) in
node [mystyle, anchor=south,red,text depth={\y2-\y1-\pgflinewidth-1.1\baselineskip},above=of C]
(D) {A\\B\\C\\D};
\end{tikzpicture}

\end{document}


Not related to the problem, but I used node distance (this saves the definition of a new length) and the features provided by the positioning library to position the nodes.

-
Many thanks for your answer. – sergej Apr 10 '13 at 10:54
@sergej please see my updated answer. – Gonzalo Medina Apr 10 '13 at 13:54