3

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:

enter image description here

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?

2 Answers2

2

Moving tikzpicture into table solves this problem. (To tell the truth, preserving the position by a float would be strange. There is no problem with the first tabular only by an accidential good position of the first float).

\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}
                 \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}
        \caption{Caption}
    \end{table}


\end{document}

enter image description here

enter image description here

enter image description here

  • +1 Please consider replacing the \tikzstyle commands by \tikzset{every picture/.append style={remember picture,baseline} every path/.append style={thick,line width=0.5mm}}. –  Nov 15 '18 at 16:18
1

For information, you can easily construct that table with {NiceArray} of nicematrix. That environment constructs for you the PGF/TikZ cells for each row of the cells. Moreover, since, with that structure, you put the environment {tikzpicture} within the environment {NiceArray} (after the keyword \CodeAfter), there is no risk to put it by mistake oustide a potential environment {table}...

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{table} \centering \renewcommand*{\arraystretch}{1.4} \setlength{\arraycolsep}{6pt} $\begin{NiceArray}{l|ccc} A \searrow& B & C & D \\hline Y=0 & .79 & .25 & .05 \ Y=1 & .20 & .70 & .74 \ Y=2 & .01 & .05 & .21 \\hline \RowStyle[color=blue]{} \text{Rowsum} & .79 & .70 & .74 \ \RowStyle[color=red]{} \text{Rowsum} & 1 & .75 & 0 \CodeAfter \begin{tikzpicture} [rounded corners = .5ex, inner sep = 0.2em] \node[draw=blue,fit=(2-2),inner sep = 0.4em] {} ; \node[draw=red,fit=(2-2)] {} ; \node[draw=red,fit=(3-2)(3-3)] {} ; \node[draw=blue,inner sep = 0.4em,fit=(3-3)(3-4)] {} ; \node[draw=red,fit=(4-2)(4-3)] {} ; \end{tikzpicture} \end{NiceArray}$ \caption{Caption} \end{table}

\end{document}

You need several compilations (because of the PGF/TikZ nodes).

Output of the above code

F. Pantigny
  • 40,250