[Tex/LaTex] tikz put text under rectangle

tikz-pgf

I want to draw a figure like this
enter image description here

But what I get now is this
enter image description here

There are two problems: (1) the red and blue rectangle are not same height, (2) the label stack and buffer are not aligned center. I'm new to tikz, can anyone show some directions that I can improve the figure?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[->,>=stealth',node distance=1cm]
    \draw [help lines] (0,0) grid (6,2);
    \node [text] (1) {I};
    \node [text] (2) [right of=1] {booked};
    \node [text] (3) [right of=2] {a};
    \node [text] (4) [right of=3] {ticket};
    \node [text] (5) [right of=4] {to};
    \node [text] (6) [right of=5] {China};

    \path (2) edge[bend right=70] node [above]{nsubj} (1);
    \path (2) edge[bend left=90] node [above]{dobj} (4);
    \path (4) edge[bend right=70] node [left]{det} (3);
    \path (4) edge[bend left=70] node [above]{prep} (5);
    \path (5) edge[bend left=70] node [above]{pobj} (6);

    \draw [red,thick,minimum height=2cm] (stack) ($(1.north west)$) rectangle ($(2.south east)$);
    \node [text,align=center] [below=stack] {stack};

    \draw [blue,thick,minimum height=2cm] (buffer) ($(3.north west)$) rectangle ($(6.south east)$);
    \node [text,align=center] [below=buffer] {buffer};
\end{tikzpicture}

\end{document}

Best Answer

Do you like to obtain something like this?

enter image description here

I significantly change your MWE:

  • introduce new libraries: chains, fit and quotes,
  • delete calc, since it is not used
  • for red and blue rectangles is used node, which fit txt nodes
  • define new styles: txt (text is used name in TikZ and had not to be used for locally defined styles) and redefine edge styles
  • for edge labels is used library quotes
  • text below red and blue rectangles are labels to respective nodes

Complete MWE:

\documentclass[tikz,
               border=3mm]{standalone}
\usetikzlibrary{arrows, chains, fit, quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 10mm,
  start chain = A going right,
   txt/.style = {text height=2ex, text depth=0.25ex,
                 on chain},
every edge/.append style = {draw, -stealth'}
                        ]
\draw [help lines] (-1,-1) grid (10,2);
\node [txt] {I};     % nodes are in the chain
\node [txt] {booked};
\node [txt] {a};
\node [txt] {ticket};
\node [txt] {to};
\node [txt] {China};
%
\node (f1)  [draw=red, inner sep=0pt, % red rectangle
             label=below:stack, % "node below rectangle done by node label
             fit=(A-1) (A-2)] {};
\node (f2)  [draw=blue,inner sep=0pt, % blue rectangle
             label=below:buffer,% "node below rectangle done by node label
              fit=(A-3) (A-6)] {};
%
\path   (A-2) edge[bend right=70, "nsubj" '] (A-1)
        (A-2) edge[bend  left=90, "dobj"   ] (A-4)
        (A-4) edge[bend right=70, "det"   '] (A-3)
        (A-4) edge[bend  left=70, "prep"   ] (A-5)
        (A-5) edge[bend  left=70, "pobj"   ] (A-6);
    \end{tikzpicture}
\end{document}
Related Question