TransWikia.com

How to make a table with multiple columns having different number of elements under different rows?

TeX - LaTeX Asked by latexuser on March 9, 2021

I tried using the following code, with an unexpected outcome shown below (I’ve also attached the outcome I am expecting):

documentclass{article}

usepackage{siunitx}
usepackage{array,booktabs}


begin{document}
begin{table}

    begin{tabular*}{textwidth}{@{extracolsep{fill}}lSS@{}} toprule
    &{textit{Score}}midrule
        textbf{class 1}  & 0 
        midrule
        &multicolumn{2}{cc}{textbf{Model A}textbf{Model B}}
        midrule
        textbf{class A}   0  &  0 
        textbf{class B}  0  &  0
        textbf{class C}  0  &  0
        textbf{class D}  0  &  0
        midrule
        textbf{class X}  0  &  0
        textbf{class Y}  0  &  0
        textbf{class Z}  0  &  0
        bottomrule
    end{tabular*}


    caption{caption here}
    label{table:results}
end{table}

end{document}

This has three major issues shown below: (1) the alignment of "Score" should be centered, (2) Model A Model B are repeating, and (3) the 0 values are shifted towards the left

enter image description here

I am trying to get an output which looks like:

enter image description here

3 Answers

The following should serve as a place to start from:

enter image description here

documentclass{article}

usepackage{siunitx}
usepackage{array,booktabs}


begin{document}
begin{table}
    begin{tabular*}{textwidth}{@{extracolsep{fill}}lSS@{}}
      toprule
      &multicolumn{2}{c}{textit{Score}}
      midrule
      textbf{class 1}  & multicolumn{2}{c}{0} 
      midrule
      &textbf{Model A} & textbf{Model B}
      midrule
      textbf{class A} & 0  &  0 
      textbf{class B} & 0  &  0
      textbf{class C} & 0  &  0
      textbf{class D} & 0  &  0
      midrule
      textbf{class X} & 0  &  0
      textbf{class Y} & 0  &  0
      textbf{class Z} & 0  &  0
      bottomrule
    end{tabular*}
    caption{caption here}
    label{table:results}
end{table}

end{document}

Answered by leandriis on March 9, 2021

The nicematrix package is the only one needed to reproduce the drawn table.

The package offer the Block command that:

(1) replaces both multirow and multicolumn;

(2) allow the use of (new lines) inside the cell;

(3) the content is centered horizontally and vertically;

Block{1-2}{..} will generate a cell 1 rows high x 2 columns wide.

Vertical lines are generally discouraged in technical publications, but they are of course useful in many situations. Some packages have difficulty joining vertical and horizontal lines. This is not the case with nicematrix.

As you can appreciate the final code is much simpler, so it is easy to understand and change.

Two compilations are required the first time or if the table layout is changed.

See also https://tex.stackexchange.com/a/584492/161015

a

documentclass{article}

usepackage{nicematrix}

begin{document}

NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt} %expand the cells vertically and horizontally

begin{table}
centering
    begin{NiceTabular}{|>bfseries{c}|c|c|}  % first column in bf
        hline
                & Block{1-2}{textit{Score}}        
        hline
        class 1 & Block{1-2}{0}                     
        hline
                &textbf{Model A} & textbf{Model B} 
        hline
        class A & 0 & 0         
        class B & 0 & 0         
        class C & 0 & 0          
        hline
        class X & 0 & 0         
        class Y & 0 & 0         
        class Z & 0 & 0         
        hline
    end{NiceTabular}
caption{Score of classes}
label{tbl:scoreii}
end{table}

end{document}

Answered by Simon Dispa on March 9, 2021

Using array and booktabs packages is sufficient for write your table ... (it is not clear, why you use ``S` columns, especially since you not specify any its formatting possibilities):

documentclass{article}
usepackage{array, makecell}

begin{document}
    begin{table}
    setcellgapes{4pt}
    makegapedcells
    centering
begin{tabular}{|>{bfseries}l | c | c | }
    Xhline{1.2pt}
        & multicolumn{2}{c|}{textit{Score}}   
class 1 & multicolumn{2}{c|}{0}                
    Xhline{0.8pt}
        &   textbf{Model A}
                &   textbf{Model B}            
    hline
class A &   0   &   0   
class B &   0   &   0   
class C &   0   &   0   
class D &   0   &   0   
    hline
class X &   0  &    0   
class Y &   0  &    0   
class Z &   0  &    0   
    Xhline{1.2pt}
end{tabular}
caption{caption here}
label{table:results}
    end{table}
end{document}

After one compilation the rezult is:

enter image description here

Answered by Zarko on March 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP