My goal is to get rectangles around cells in tables as was already asked in Rectangles around cells in table. I adapted the solution suggested in https://tex.stackexchange.com/a/140224/81936, which in principle works fine. However, if I do this in a longer document with multiple tables the rectangles are sometimes vertically misaligned as shown in the following picture:
This code reproduces the error. For the first table, the vertical alignment is correct but not for the second.
\documentclass{article}
\usepackage{tikz}
\usepackage{tabularx}
\usetikzlibrary{fit}
\usepackage{lipsum}
\begin{document}
% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount counter
\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 path}+=[thick,line width=0.5mm]
\begin{table}
\centering
\renewcommand*{\arraystretch}{1.4}
\begin{tabular}{l|ccc}
$A \searrow$& $B$ & $C$ & $D$ \\\hline
$Y=0$ & \tabnode{$.79$} & \tabnode{$.25$} & $.05$ \\
$Y=1$ & \tabnode{$.20$} & \tabnode{$.70$} & \tabnode{$ .74$} \\
$Y=2$ & \tabnode{$.01$} & \tabnode{$.05$} & $.21$ \\\hline
\textcolor{blue}{Rowsum} & $\textcolor{blue}{.79}$ & $\textcolor{blue}{.70}$ & $\textcolor{blue}{.74}$ \\
\textcolor{red}{Rowsum} & $\textcolor{red}{1}$ & $\textcolor{red}{.75}$ & $\textcolor{red}{0}$
\end{tabular}
\caption{Caption}
\end{table}
\begin{tikzpicture}[overlay]
% Define the circle paths
\node[draw=blue,rounded corners = .5ex,fit=(1)(1),inner sep = 2pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(1)(1),inner sep = 0pt,] {};
\node[draw=blue,rounded corners = .5ex,fit=(4)(5),inner sep = 2pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(3)(4),inner sep = 0pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(6)(7),inner sep = 0pt,] {};
\end{tikzpicture}
\setcounter{nodecount}{0}
\lipsum[3-10]
\begin{table}
\centering
\renewcommand*{\arraystretch}{1.4}
\begin{tabular}{l|ccc}
$A \searrow$& $B$ & $C$ & $D$ \\\hline
$Y=0$ & \tabnode{$.79$} & \tabnode{$.25$} & $.05$ \\
$Y=1$ & \tabnode{$.20$} & \tabnode{$.70$} & \tabnode{$ .74$} \\
$Y=2$ & \tabnode{$.01$} & \tabnode{$.05$} & $.21$ \\\hline
\textcolor{blue}{Rowsum} & $\textcolor{blue}{.79}$ & $\textcolor{blue}{.70}$ & $\textcolor{blue}{.74}$ \\
\textcolor{red}{Rowsum} & $\textcolor{red}{1}$ & $\textcolor{red}{.75}$ & $\textcolor{red}{0}$
\end{tabular}
\caption{Caption}
\end{table}
\begin{tikzpicture}[overlay]
% Define the circle paths
\node[draw=blue,rounded corners = .5ex,fit=(1)(1),inner sep = 2pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(1)(1),inner sep = 0pt,] {};
\node[draw=blue,rounded corners = .5ex,fit=(4)(5),inner sep = 2pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(3)(4),inner sep = 0pt,] {};
\node[draw=red,rounded corners = .5ex,fit=(6)(7),inner sep = 0pt,] {};
\end{tikzpicture}
\end{document}
Removing the \lipsum[3-10] command, which just adds filler text, resolves the error. Any ideas how to fix that properly?





\tikzstylecommands by\tikzset{every picture/.append style={remember picture,baseline} every path/.append style={thick,line width=0.5mm}}. – Nov 15 '18 at 16:18