3

How can I make certain cells in my table have no border around them? Specifically for my table, I want the top right cell (row sum) and the bottom left cell (column sum) not to have the lines around them appear?

Here my code:

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

The result is as follows

enter image description here

4
  • Please don't post code fragments, but instead embed them in a complete compilable document. What package defines tblr for example? But you can use \multicol to redefine a cell or range of cells.
    – Alan Munn
    Feb 14, 2023 at 19:59
  • @AlanMunn I'm sorry.
    – wayne
    Feb 14, 2023 at 20:37
  • @AlanMunn I've already tried that. But always get the error message: Package tabularray Error: \multicol is obsolete; use \SetCell instead.
    – wayne
    Feb 14, 2023 at 20:47
  • Well one imagines that looking up the tabularray documentation on \SetCell would suggest how to do the equivalent thing.
    – Alan Munn
    Feb 14, 2023 at 23:12

3 Answers 3

5

Here is a possible solution

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

Or, without the need of printing some of the lines in white, here is another approach

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

Both produce

enter image description here

0
3

Similar to second example in @Udi Fogiel interesting answer (+1), but with a wee bit more consistent and concise code:

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

enter image description here

2

For information, here is a solution with {NiceTabular} of nicematrix. In that environment, the key hvlines draws all the rules excepted in the empty corners (computed by the key corners).

I have added in the empty corners row sum and column sum after the construction of the array by using TikZ. However, one has to specify by ``by hand'' the width of some columns of the tabular (since {NiceTabular} does not take into account row sum and column sum which are added after the construction of the array).

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

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .