[Tex/LaTex] tikz and tables: node increases row height

arrowstablestikz-arrowstikz-pgf

I'm trying to add arrows to a table in my beamer presentation using the package tikz. Because I don't want the table to look different from the other tables in the presentation, I didn't use \matrix (or is it feasible to create matrices that look exactly like tables?). The problem ist that the \node command which defines the target of the arrow increases the row height (see screenshot). This slight difference in height is very annoying in the context of animations. How can I avoid that?

I'm using tikz for the first time. Please let me know if I'm doing it completely wrong or if there are better solutions. Thank you!

Screenshot:

enter image description here

My code:

\documentclass[12pt]{beamer}

\usepackage[american]{babel}
\usepackage[utf8]{inputenc}

%tables
\usepackage{booktabs}
\usepackage{array}

% arrows
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,fit,positioning}
\tikzset{every picture/.style={remember picture}}


\begin{document}

\frame{
  \frametitle{frame}

  \begin{center}
    \begin{tabular}{ccc}
      \textbf{foo} & \textbf{bar} & \textbf{baz}\\
      \hline
      2 & 3 & 4 \\
      3 & 3 & 6 \\
      3 & 1 & \tikz[baseline]{\node[rounded corners,fill=blue!15,anchor=base] (main_arrow_target) {9};} \\
    \end{tabular}
  \end{center}

  \begin{center}

  \tikz[baseline]{ \node[rounded corners,fill=blue!15,anchor=base] (arrow_main_start) {
    \begin{tabular}{c>{\centering\arraybackslash}p{2cm}>{\centering\arraybackslash}p{2cm}}
      \textbf{x} & \textbf{y} & \textbf{z}\\
      \hline
      a & 2 & 7 \\
      b & 3 & 4 \\
      c & 8 & 6 \\
      d & \tikz[baseline]{\node (chose_option) {7};} & \tikz[baseline]{\node (z_start) {2};} \\
      & & \\
      & \multicolumn{2}{c}{\tikz[baseline]{\node (value_left) {};} index: 9 \tikz[baseline]{\node (value_right) {};}}\\
    \end{tabular}
  };}
  \end{center}

  \begin{tikzpicture}[overlay]
    \path[very thick,->] (arrow_main_start.north east) edge [bend right] (main_arrow_target.east);
    \path[very thick,->] (chose_option.south) edge [bend right] (value_left.north);
    \path[very thick,->] (z_start.south) edge [bend left] (value_right.north);
  \end{tikzpicture}
}

\frame{
  \frametitle{frame}

  \begin{center}
    \begin{tabular}{ccc}
      \textbf{foo} & \textbf{bar} & \textbf{baz}\\
      \hline
      2 & 3 & 4 \\
      3 & 3 & \tikz[baseline]{\node[rounded corners,fill=blue!15,anchor=base] (main_arrow_target) {6};} \\
      3 & 1 & 9 \\
    \end{tabular}
  \end{center}

  \begin{center}

  \tikz[baseline]{ \node[rounded corners,fill=blue!15,anchor=base] (arrow_main_start) {
    \begin{tabular}{c>{\centering\arraybackslash}p{2cm}>{\centering\arraybackslash}p{2cm}}
      \textbf{x} & \textbf{y} & \textbf{z}\\
      \hline
      a & 3 & 2 \\
      b & 9 & 6 \\
      c & 0 & 7 \\
      d & \tikz[baseline]{\node[] (chose_option) {2};} & \tikz[baseline]{\node[] (z_start) {8};} \\
      & & \\
      & \multicolumn{2}{c}{\tikz[baseline]{\node (value_left) {};} index: 6 \tikz[baseline]{\node (value_right) {};}}\\
    \end{tabular}
  };}
  \end{center}

  \begin{tikzpicture}[overlay]
    \path[very thick,->] (arrow_main_start.north east) edge [bend right] (main_arrow_target.east);
    \path[very thick,->] (chose_option.south) edge [bend right] (value_left.north);
    \path[very thick,->] (z_start.south) edge [bend left] (value_right.north);
  \end{tikzpicture}
}

\end{document}

Best Answer

Here are three approaches to compare.

The first one is your original code, which increases the size of the row.

The second one uses overlay for the node, and this causes that cell to be "as empty" for the tabular environment, so it does not increase the row height, but if the content of the cell is too wide, it can overlap neighbourgh columns.

The third is my proposed solution, which draws the node with a coloured background, but uses only the textual contents of the node as the bounding box:

\documentclass{article}
\usepackage{inputenc}
\usepackage{booktabs}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,fit,positioning}
\begin{document}
\tikzset{every picture/.style={remember picture}}
\tikzset{
 remark/.style= {
   rounded corners,fill=blue!15,anchor=base, inner sep=1.3mm,
  }
}

\def\remarkedoriginal#1#2{% name, contents
\tikz[baseline]{
  \node[remark] (#1)  {#2};
}}

\def\remarkedoverlay#1#2{% name, contents
\tikz[baseline,overlay]{
  \node[remark] (#1)  {#2};
}}

\def\myremarked#1#2{% name, contents
\tikz[baseline]{
  \node[anchor=base,inner sep=0pt] (aux) {#2};
  \path[use as bounding box] (aux.south west) rectangle (aux.north east);
  \node[remark] (#1)  {#2};
}}

    \begin{tabular}{ccc}
      \textbf{foo} & \textbf{bar} & \textbf{baz}\\
      \hline
      2 & 3 & 4 \\
      3 & 3 & 6 \\
      3 & 1 & \remarkedoriginal{main arrow target}{wide content} \\
    \end{tabular}

\vskip 5mm

    \begin{tabular}{ccc}
      \textbf{foo} & \textbf{bar} & \textbf{baz}\\
      \hline
      2 & 3 & 4 \\
      3 & 3 & 6 \\
      3 & 1 & \remarkedoverlay{main arrow target}{wide content} \\
    \end{tabular}

\vskip 5mm

    \begin{tabular}{ccc}
      \textbf{foo} & \textbf{bar} & \textbf{baz}\\
      \hline
      2 & 3 & 4 \\
      3 & 3 & 6 \\
      3 & 1 & \myremarked{main arrow target}{wide content} \\
    \end{tabular}
\end{document}

Result