TransWikia.com

Include colored box, rotated text in booktabs styled table

TeX - LaTeX Asked by matais on February 11, 2021

I am trying to make a latex table that looks like the picture. There should basically be 4 columns of rotated text under Header 1, one grey cell that that contains additional information. I am new to latex and have no idea how to make this table in booktabs style. enter image description here

documentclass{article}
newcommand*turn{rotatebox[origin=b]{90}}
usepackage{array}
usepackage{booktabs}
usepackage{multirow}
usepackage{array, caption}
usepackage{graphicx}
usepackage{makecell}
usepackage[table,xcdraw]{xcolor}


begin{document}
 
begin{table}[]
centering
begin{tabular}{@{}llllll@{}}
toprule
& multicolumn{4}{c}{Header 1} &      
cmidrule(lr){2-5}
&turn{Text1}      &turn{Text2}     &turn{Text3}     & turn{Text4}     & cellcolor{gray!25} makecell[l]{Some line 1 Some line 1 Some line 1 Some line 1 Some line 1} 
Header 3 &    &      &        &       &Header 3
midrule

&        &       &       &       &    
&        &       &       &       &    
bottomrule

end{tabular}
end{table}

end{document}

3 Answers

enter image description here

MWE, which reproduce this (unusual) table (as you shown in question) is:

documentclass{article}
usepackage[table,xcdraw]{xcolor}
usepackage{rotating}
usepackage{makecell, multirow, tabularx}
newcolumntype{L}{>{raggedrightarraybackslash}X}
usepackage[skip=1ex]{caption}

begin{document}
    begin{table}[htb]
    centering
    setlengthextrarowheight{2pt}
    settowidthrotheadsize{Text 4}
    renewcommandrotheadgape{}
begin{tabularx}{0.6linewidth}{ l llll c@{,}X }
    Xhline{1pt}
            & multicolumn{4}{c}{Header 1} 
                                && cellcolor{gray!25}     
    Xcline{2-5}{0.6pt}
                &   rothead{Text 1}
                    &   rothead{Text 2}
                        &   rothead{Text 3}
                            &   rothead{Text 4}
                                &&  cellcolor{gray!25}%
                                    multirow{-2.2}{=}{noindent
                                    Some line 1 Some line 2
                                    Some line 3}         
Header 3    &   &   &   &   &&  Header 3                
    Xhline{0.6pt}
A           &   &   &   &   &&  1                       
B           &   &   &   &   &&  2                       
    Xhline{1pt}
end{tabularx}
    end{table}
end{document}

Addendum:

documentclass{article}
usepackage[table,xcdraw]{xcolor}
usepackage{rotating}
usepackage{makecell, multirow, tabularx}
newcolumntype{L}{>{raggedrightarraybackslash}X}
newcommandmrrc[1]{multirow{-2.4}{*}{rothead{#1}}} % <---
usepackage[skip=1ex]{caption}

begin{document}
    begin{table}[htb]
    centering
    setlengthextrarowheight{2pt}
    settowidthrotheadsize{Text 4}
    renewcommandrotheadgape{}
begin{tabularx}{0.6linewidth}{ l llll c@{,}L }
    Xhline{1pt}
            & multicolumn{4}{c}{Header 1} 
                            && cellcolor{gray!25}    
    Xcline{2-5}{0.6pt}
            &   &   &   &   &&  cellcolor{gray!25}                 % <---
                                smalllinespread{0.84}selectfont  % <---
                                multirow{-1.8}{=}{noindent
                                    Some line 1 
                                    Some line 2
                                    Some line 3}    [1ex]
Header 3    &   mrrc{Text 1}       % <--- rotated headers moved down
                &   mrrc{Text 2}
                    &   mrrc{Text 3}
                        &   mrrc{Text 4}
                                   &&  Header 3     
    Xhline{0.6pt}
A           &   &   &   &   &&  1                   
B           &   &   &   &   &&  2                   
    Xhline{1pt}
end{tabularx}
    end{table}
end{document}

hopefully produce what you like to have:

enter image description here

Answered by Zarko on February 11, 2021

Is it what you want to have?

documentclass{article}
newcommand*turn{rotatebox[origin=b]{90}}
usepackage{array}
usepackage{booktabs}
usepackage{multirow}
usepackage{array, caption}
usepackage{graphicx}
usepackage{makecell}
usepackage[table,xcdraw]{xcolor}

begin{document}

begin{table}[]
centering
begin{tabular}{@{}llllll@{}}
toprule
& multicolumn{4}{c}{Header 1} & 
cmidrule(lr){2-5}
&multirowcell{3.5}{turn{Text1}} &multirowcell{3.5}{turn{Text2}} &multirowcell{3.5}{turn{Text3}} & multirowcell{3.5}{turn{Text4}} & cellcolor{gray!25} makecell{Some line 1 Some line 1 Some line 1 Some line 1 Some line 1} 
addlinespace
Header 3 & & & & &Header 3
midrule

& & & & & 
& & & & & 
bottomrule
end{tabular}
end{table}

end{document} 

enter image description here

Answered by Bernard on February 11, 2021

Here is a solution with {NiceTabular} of nicematrix.

documentclass{article}
usepackage{xcolor}
usepackage{makecell}
usepackage[skip=1ex]{caption}
usepackage{nicematrix}
usepackage{booktabs}

begin{document}
begin{table}[htb]
centering
setlengthextrarowheight{2pt}
begin{NiceTabular}{llllll}[code-before = rectanglecolor{gray!20}{1-6}{2-6}]
toprule
 & multicolumn{4}{c}{Header 1} 
 & hphantom{Some line 3}
   Block{2-1}{makecell{Some line 1 Some line 2  Some line 3}}    
cmidrule(lr){2-5}
 & rotate Text 1
 & rotate Text 2
 & rotate Text 3
 & rotate Text 4 
Header 3    &   &   &   &   & Header 3 
midrule
A           &   &   &   &   &  1 
B           &   &   &   &   &  2 
bottomrule
end{NiceTabular}
end{table}
end{document}
  • This solution uses booktabs (nicematrix is compatible with booktabs). Hence, it's easy to use shorten the cmidrule with cmidrule(rl) (there is no need of a dummy column).

  • The command Block of nicematrix put its content at the mathematical center of the merge cells. There is no need to adjust manually as in multirow.

  • In nicematrix, the colored panels are drawn before all the rules (the output is better in the PDF viewers: the rules won't seem to disappear at some levels of zoom in some PDF viewers as Adobe Reader).

Ouput of the above code

Answered by F. Pantigny on February 11, 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