表格单元格周围的矩形问题

表格单元格周围的矩形问题

我的目标是在表格中的单元格周围设置矩形,正如在表格单元格周围的矩形。我采用了https://tex.stackexchange.com/a/140224/81936,这在原则上可以正常工作。但是,如果我在包含多个表格的较长文档中执行此操作,则矩形有时会垂直错位,如下图所示:

在此处输入图片描述

此代码重现了错误。对于第一个表,垂直对齐是正确的,但对于第二个表则不正确。

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

删除 \lipsum[3-10] 命令(该命令只会添加填充文本)即可解决错误。有什么方法可以正确修复该问题吗?

答案1

移入解决了这个问题。(说实话,用浮动来保留位置会很奇怪。第一个表格没有问题,只是因为第一个浮动偶然获得了一个好位置)tikzpicturetable

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

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案2

供您参考,您可以使用 轻松构建该表。该环境为您构建单元{NiceArray}nicematrix的每一行的 PGF/TikZ 单元格。此外,由于使用该结构,您将环境放在{tikzpicture}环境内{NiceArray}(关键字 之后\CodeAfter),因此不存在错误地将其放在潜在环境之外的风险{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}

您需要多次编译(因为 PGF/TikZ 节点)。

上述代码的输出

相关内容