[Tex/LaTex] Annotating tables

highlightingtablestabularxtikz-pgf

I'm trying to annotate a table by highlighting and annotating the final column, yet once I use the\tabnode together with coloring I get misalignment. Either the content of the cell marked with \tabnode is misplaced outside of the table, or the cell is stretched in width (whereas all cells should be equal width). What is the right way to fix this? Here's a sample code I'm using:

   \documentclass{beamer}
   \usepackage[beamer,customcolors]{hf-tikz}
   \usepackage{tabularx}
   \begin{document}

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

     \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]


   \newcolumntype{g}{>{\columncolor{red}}c}
   \begin{table}[ht]
     \centering
     \begin{tabular}{|c|c|c|g|}\hline
      bla   &  bla  & bla   & \tabnode{$b_1$}& \hline
  bla   &  bla  & bla   & bla            & \hline       
  bla   &  bla  & bla   & bla            & \hline   
  bla   &  bla  & bla   & bla            & \hline
  bla   &  bla  & bla   & bla            & \hline       
  bla   &  bla  & bla   & bla            & \hline
     \end{tabular}
   \end{table}


   \begin{tikzpicture}[overlay]

    \node [right=2cm,above=2cm,minimum width=0pt] at (1) (A) {A};
    \draw [<-,out=5,in=180] (1) to (A);

   \end{tikzpicture}

   \end{frame}

Best Answer

Your code has some mistakes that will produce errors (in particular, you declared four columns but were using five and you forgot the line changing commands): below I corrected them and fixed other aspects:

\documentclass{beamer}
\usepackage[beamer,customcolors]{hf-tikz}
\usepackage{tabularx}
\usepackage{colortbl}

\newcounter{nodecount}
\newcolumntype{g}{>{\columncolor{red}}c}

\newcommand\tabnode[1]{%
  \addtocounter{nodecount}{1}%
  \tikz[remember picture,overlay]\node[inner sep=0pt] (\thenodecount) {#1};}

\begin{document}

\begin{frame}{Title}

\tikzset{
every picture/.style={remember picture,baseline},
every node/.style={
  inner sep=0pt,
  anchor=base,
 minimum width=1.8cm,
 align=center,
 text depth=.25ex,
 outer sep=1.5pt},
every path/.style={
  thick, 
  rounded corners
  }
}  

\begin{table}
\centering
\begin{tabular}{|c|c|c|g|c|}
 \hline
  bla   &  bla  & bla   & \tabnode{$b_1$} \\ \hline
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline
 \end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
  \node [right=2cm,above=2cm,minimum width=0pt] at (1) (A) {A};
  \draw [<-,out=5,in=180] (1) to (A);
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

I'd like to propose you, however, another approach using the tikzmark library: the general idea is still the same; using \tikzmark you place a "mark", as in \tikzmark{a} and then you can use this mark using pic cs. as in \draw (pic cs:a) -- ++(1,0);. A complete example:

\documentclass{beamer}
\usepackage[beamer,customcolors]{hf-tikz}
\usepackage{tabularx}
\usepackage{colortbl}
\usetikzlibrary{tikzmark}

\newcolumntype{g}{>{\columncolor{red}}c}

\begin{document}

\begin{frame}{Title}

\tikzset{
every picture/.style={remember picture,baseline},
every node/.style={
  inner sep=0pt,
  anchor=base,
 minimum width=1.8cm,
 align=center,
 text depth=.25ex,
 outer sep=1.5pt},
every path/.style={
  thick, 
  rounded corners
  }
}  

\begin{table}
\centering
\begin{tabular}{|c|c|c|g|c|}
 \hline
  bla   &  bla  & bla   & $b_1$\tikzmark{a} \\ \hline
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline
  bla   &  bla  & bla   & bla \\\hline   
  bla   &  bla  & bla   & bla \\\hline
 \end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
  \node [right=2cm,above=2cm,minimum width=0pt] at (pic cs:a) (A) {A};
  \draw [<-,out=5,in=180] ([xshift=15pt]{pic cs:a}) to (A);
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

It's not clear why are you loading the hf-tikz package, since it doesn't pay any rol in your MWE.