3

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?

jub0bs
  • 58,916
sergej
  • 6,461
  • 32
  • 62

1 Answers1

4

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}

enter image description here

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.

Gonzalo Medina
  • 505,128