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.
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:
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.I used a tweaked version of Caramdir's solution for drawing the rectangle. Basically you need to mark the top left with
\marktopleft{a1}
wherea1
is the first shape, for example, and the bottom right with\markbottomright{a1}
.Each shape needs to be coupled like that.
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:
And here is the code: