[Tex/LaTex] Adding a rectangular box with tikz to table (beamer)

beamertablestikz-pgf

I would like to add a rectangular box to my table to highlight some rows. I'm trying to use tikz with the second lot of code from Harish Kumar's answer to a similar question. However, unlike the earlier question, I'm using beamer and the box appears in the wrong place after compiling several times. I'm attaching a picture with the desired result and what I obtain instead.

enter image description here

enter image description here

This is my code:

\documentclass{beamer}
\usepackage[english]{babel}
\usepackage{tabularx,booktabs,dcolumn}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\pdfmapfile{+sansmathaccent.map}
\newcolumntype{d}{D..{-1}}

\newcounter{nodecount}
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node                (\arabic{nodecount}) {#1};}

% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[inner sep=0pt,anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

\begin{document}

\begin{frame}
\frametitle{Title of my slide}
\begin{table}
\resizebox{\textwidth}{!}{   
\begin{tabular}{c*{7}{d}}
\midrule 
\multicolumn{1}{c}{$CCCCC$} 
& \multicolumn{1}{c}{$CCCC$}
& \multicolumn{1}{c}{$CCCC$}
& \multicolumn{1}{c}{$CCC$}
& \multicolumn{1}{c}{$CCCCCCC$}
& \multicolumn{1}{c}{$CCCCC$}
& \multicolumn{1}{c}{$CCCCC$}
& \multicolumn{1}{c}{$CCCCCCC$}
\\
\midrule
\multicolumn{8}{c}{\textbf{Panel A: AAA}} \\
\midrule
\tabnode{0}     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 &            \tabnode{0.00} \\
\tabnode{}    & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) &          \tabnode{0.00} \\
  &       &       &       &       &       &       &  \\
  0     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
  & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\
  &       &       &       &       &       &       &  \\
  0     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
  & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\ 
 \multicolumn{8}{c}{\textbf{Panel B: BBB}} \\
 \midrule
 0     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
   & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\
   &       &       &       &       &       &       &  \\
 0     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
   & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\
   &       &       &       &       &       &       &  \\
 0     & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
   & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\ 
 \midrule
 \end{tabular}
 }
 \begin{tikzpicture}[overlay]
 \node[draw=red,rounded corners = 1ex,fit=(1)(4),inner sep = 1pt] {};
 \end{tikzpicture}

\end{table}
\end{frame}
\end{document}  

Best Answer

Here's the revised version.

Changes:

  1. Fixed your newcolumntype. I also had fixed the command for the \tabnode, but this solution didn't allow the numbers inside the node to align by the decimal separator so I removed the command altogether and replaced with something else, see below.

    Furthermore, now if you try adding random numbers to your values, they should all be aligned at the separator ., I just tested it myself. In the output below, the first number is long and seems not to be aligned, but if you add more numbers in the column, they will be aligned.

  2. I used a tweaked version of Caramdir's solution for drawing the rectangle. Basically you need to mark the top left with \marktopleft{a1} where a1 is the first shape, for example, and the bottom right with \markbottomright{a1}.

    Each shape needs to be coupled like that.

  3. In case you don't know, booktabs supports also \toprule and \bottomrule. I have added them.

As usual with Tikz, remember that if you add a new rectangle, you might need to typeset twice to fix the positioning.

Here is the output:

enter image description here

And here is the code:

\documentclass{beamer}
\usepackage[english]{babel}
\usepackage{lmodern}% http://ctan.org/pkg/lm
\usepackage{tikz}
\usepackage{booktabs,dcolumn}
\usepackage{tabularx}

\usetikzlibrary{matrix,shapes,arrows,fit,tikzmark}
\newcolumntype{.}{D{.}{.}{-1}}

\begin{document}

% Some options common to all the nodes and paths
\tikzset{   
        every picture/.style={remember picture,baseline},
        every node/.style={anchor=base,align=center,outer sep=1.5pt},
        every path/.style={thick},
        }

\newcommand\marktopleft[1]{%
    \tikz[overlay,remember picture] 
        \node (marker-#1-a) at (.1em,.3em) {};%
}
\newcommand\markbottomright[1]{%
    \tikz[overlay,remember picture] 
        \node (marker-#1-b) at (.1em,.3em) {};%
    \tikz[overlay,remember picture,inner sep=3pt]
        \node[draw=red,rounded corners,fit=(marker-#1-a.north west) (marker-#1-b.south east)] {};%
}


\begin{frame}
    \frametitle{Title of my slide}
%
\begin{table}
     \resizebox{\textwidth}{!}{%
%
\begin{tabular}{.*{8}{.}}
    \toprule 
%
\multicolumn{1}{c}{$CCCCC$} 
& \multicolumn{1}{c}{$CCCC$}
& \multicolumn{1}{c}{$CCCC$}
& \multicolumn{1}{c}{$CCC$}
& \multicolumn{1}{c}{$CCCCCCC$}
& \multicolumn{1}{c}{$CCCCC$}
& \multicolumn{1}{c}{$CCCCC$}
& \multicolumn{1}{c}{$CCCCCCC$}
\\
    \midrule
\multicolumn{8}{c}{\textbf{Panel A: AAA}} \\
    \midrule
  \marktopleft{a1}134,343.50 & 0.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.030 \\
  & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & 12510.00\markbottomright{a1} \\
  &       &       &       &       &       &       &  \\
  0     & 103.00  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.440 \\
  & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (10.00) \\
  &       &       &       &       &       &       &  \\
  \marktopleft{b1}0     & 0.0435  & 0.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
  & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00)\markbottomright{b1} \\
  &       &       &       &       &       &       &  \\
 \multicolumn{8}{c}{\textbf{Panel B: BBB}} \\
    \midrule
 0     & 0.00  & 404.00  & 0.34300 & 0.00 & 0.00  & 0.00 & 0.00 \\
   & (70.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\
   &       &       &       &       &       &       &  \\
 0     & 32.00  & 0.2300  & 54540.00 & 0.0045 & 0.00  & 0.00 & 0.00 \\
   & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\
   &       &       &       &       &       &       &  \\
 0     & 0.00  & 10.00  & 0.00 & 0.00 & 0.00  & 0.00 & 0.00 \\
   & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) & (0.00) \\ 
    \bottomrule
\end{tabular}
}
\end{table}
\end{frame}
\end{document}