删除特定单元格的边框

删除特定单元格的边框

如何让表格中的某些单元格周围没有边框?具体来说,对于我的表格,我希望右上单元格(行总和)和左下单元格(列总和)周围不出现线条?

这是我的代码:

\begin{table}[ht!]
\centering
\begin{tblr}{
    colspec = {|c|c|c|c|c|},
    cell{1}{3} = {lightgray},
    cell{3}{1} = {lightgray},
    cell{1}{1} = {c=2}{c},
    cell{2}{1} = {c=2}{c},
    cell{1}{1} = {r=2}{r},
    cell{1}{2} = {r=2}{r},
    cell{1}{5} = {r=2}{r},
    cell{1}{3} = {c=2}{c},
    cell{3}{1} = {r=2}{r},
    cell{5}{1} = {c=2}{c},
    hspan = even,
  }
\hline
&  & Z &  & row sum\\
\hline
 & & ja & nein & \\
\hline
\textbf{A} & B & 253 & 59 & 312 \\
\hline
 & C & 189 & 226 & 415 \\
\hline
column sum& & 442 & 285 & 727 \\
\hline
  \end{tblr}
\end{table}

结果如下

在此处输入图片描述

答案1

这是一个可能的解决方案

\documentclass{article}

\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
    
    \begin{tblr}{
            colspec = {|c|c|c|c|c|},
            cell{1}{3} = {lightgray},
            cell{3}{1} = {lightgray},
            cell{1}{1} = {c=2}{c},
            cell{2}{1} = {c=2}{c},
            cell{1}{1} = {r=2}{r},
            cell{1}{2} = {r=2}{r},
            cell{1}{5} = {r=2}{r},
            cell{1}{3} = {c=2}{c},
            cell{3}{1} = {r=2}{r},
            cell{5}{1} = {c=2}{c},
            hspan = even,
            hlines,
            hline {1} = {5}{white,leftpos = -1},
            hline {6} = {1-2}{white},
            vline {6} = {1-2}{white},
            vline {1} = {5-6}{white}
        }
        &  & Z &  & row sum\\
        & & ja & nein & \\
        \textbf{A} & B & 253 & 59 & 312 \\
        & C & 189 & 226 & 415 \\
        column sum& & 442 & 285 & 727 \\
    \end{tblr}
\end{document}

或者,不需要用白色打印一些线条,这是另一种方法

\documentclass{article}

\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
    
    \begin{tblr}{
            colspec = {c|c|c|c|c},
            cell{1}{3} = {lightgray},
            cell{3}{1} = {lightgray},
            cell{1}{1} = {c=2}{c},
            cell{2}{1} = {c=2}{c},
            cell{1}{1} = {r=2}{r},
            cell{1}{2} = {r=2}{r},
            cell{1}{5} = {r=2}{r},
            cell{1}{3} = {c=2}{c},
            cell{3}{1} = {r=2}{r},
            cell{5}{1} = {c=2}{c},
            hspan = even,
            hline{2-5},
            hline{1} = {1-4}{},
            hline{6} = {3-5}{},
            vline{1} = {1-4}{},
            vline{6} = {3-6}{}
        }
        &  & Z &  & row sum\\
        & & ja & nein & \\
        \textbf{A} & B & 253 & 59 & 312 \\
        & C & 189 & 226 & 415 \\
        column sum& & 442 & 285 & 727 \\
    \end{tblr}
\end{document}

均产生

在此处输入图片描述

答案2

与@Udi Fogiel 有趣的答案中的第二个例子类似(+1),但代码更加一致和简洁:

\documentclass[margin=3mm]{standalone}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
    \begin{tblr}{hline{1} = {1-Y}{}, hline{2-Y}, hline{Z} = {3-Z}{},
                 vline{1} = {1-Y}{}, vline{2-Y}, vline{Z} = {3-Z}{},
                 colspec = {*{5}{c}},
                 cell{1}{1} = {c=2, r=2}{},
                 cell{1}{3} = {c=2}{   bg=gray8, font=\bfseries},
                 cell{1}{5} = {r=2}{},
                 cell{3}{1} = {r=2}{r, bg=gray8, font=\bfseries},
                 cell{5}{1} = {c=2}{},
                 hspan = even,
                }
            &   & Z     &       & row sum   \\
            &   & ja    & nein  &           \\
A           & B & 253   & 59    & 312       \\
            & C & 189   & 226   & 415       \\
column sum  &   & 442   & 285   & 727       \\
    \end{tblr}
\end{document}

在此处输入图片描述

答案3

供参考,这里有一个{NiceTabular}的解决方案nicematrix。在该环境中,密钥hvlines绘制除空角(由密钥 计算corners)之外的所有规则。

我使用 TikZ 在空角row sumcolumn sum数组构建后添加了这些。但是,必须“手动”指定表格中某些列的宽度(因为{NiceTabular}没有考虑row sumcolumn sum数组构建后添加的列)。

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{NiceTabular}
  [hvlines,corners,color-inside]    
  {w{r}{7mm}w{c}{7mm}ccw{c}{15mm}}
\Block{2-2}{}
  &          & \Block[fill=gray!50]{1-2}{\textbf{Z}} \\
  &          & ja   & nein \\
  \Block[fill=gray!50]{2-1}{\textbf{A}} 
             & B   & 253  & 59   & 312 \\
  & C        & 189 & 226  & 415 \\
  &          & 442 & 285  & 727 \\
\CodeAfter
  \tikz \node at (2-|5.5) { row sum } ; 
  \tikz \node at (5.5-|2) { column sum } ;
\end{NiceTabular}

\end{document}

上述代码的输出

相关内容