TransWikia.com

Table in Latex -- How to make perfect column lines?

TeX - LaTeX Asked on May 27, 2021

The code blow is related to a table. However, the column lines are discontinuous and I need perfect lines from top to bottom. How to do that?

begin{table*}[t!]
    caption{Table} 
    label{tab:tbl2}
    centering % center the table
    begin{tabular}{c | c | c c | c} 
    toprule 
    {} & multicolumn{2}{c}{a}  & multicolumn{2}{c}{b}   
    cmidrule(lr){2-3} cmidrule(lr){4-5}
    {c}  & d  & e & f  & g 
    hline
    multirow{2}{*}{h}  
    & multirow{2}{*}{i}        
    & j
    & multirow{2}{*}{k}
    
    {} & {} & f & {} & h
    
    midrule
    multirow{4}{*}{l} 
    & multirow{4}{*}{m}        
    & n
    & 
    &  
    
    & {} & b & $ c 
     
    & {} & d & f 
    
    & & f & &
    
    bottomrule
    end{tabular}
end{table*} 

3 Answers

I believe that on this website, many (most?) recommendations aimed at making your table look attractive will focus on NOT using vertical lines at all. They're not needed! Really! Put differently, a "perfect" table neither requires nor benefits from vertical lines. In addition, you'll find many recommendations here to use the line-drawing macros of the booktabs package instead of hline and cline.

I, for one, certainly subscribe to these views. In addition to following the recommendations given in the preceding paragraph, I would also eliminate the whitespace padding to the left of the first column and to the right of the final column, and I wouldn't use right-hand trimming on the second cmidrule. And, I'd replace the lone hline directive with midrule.

The following screenshot shows a before-and-after comparison. The table on the left is based on your code, while the one on the right implements the suggestions made above. (To make the tables look less abstract, I tripled all letters.)

enter image description here

documentclass{article}
usepackage{booktabs,multirow}

begin{document}
begin{table}[ht!]
centering
begin{minipage}[t]{0.45textwidth}
    centering
    caption{Before} 
    label{tab:before}
    centering
    begin{tabular}{c | c | c c | c} 
    toprule 
    {} & multicolumn{2}{c}{aaa}  & multicolumn{2}{c}{bbb}   
    cmidrule(lr){2-3} cmidrule(lr){4-5}
    {ccc}  & ddd  & eee & fff  & ggg 
    hline
    multirow{2}{*}{hhh}  
    & multirow{2}{*}{iii}        
    & jjj
    & multirow{2}{*}{kkk}
    
    {} & {} & fff & {} & hhh
    
    midrule
    multirow{4}{*}{lll} 
    & multirow{4}{*}{mmm}        
    & nnn
    & 
    &  
    
    & {} & bbb & $ccc$ 
     
    & {} & ddd & fff 
    
    & & fff & &
    
    bottomrule
    end{tabular}
end{minipage}
quad
begin{minipage}[t]{0.45textwidth}
    centering
    caption{After} 
    label{tab:after}
    centering
    begin{tabular}{@{} *{5}{c} @{}} 
    toprule 
    & multicolumn{2}{c}{aaa} & multicolumn{2}{c@{}}{bbb}   
    cmidrule(lr){2-3} cmidrule(l){4-5}
    {ccc}  & ddd  & eee & fff  & ggg 
    midrule% not "hline"
    multirow{2}{*}{hhh} & multirow{2}{*}{iii} & jjj & multirow{2}{*}{kkk} 
    & & fff & & hhh 
    midrule
    multirow{4}{*}{lll} & multirow{4}{*}{mmm} & nnn 
    & & bbb & $ccc$  
    & & ddd & fff 
    & & fff 
    bottomrule
    end{tabular}
end{minipage}
end{table} 
end{document}

Correct answer by Mico on May 27, 2021

Is this what you are looking for?

MWE:

documentclass[a4paper,12pt]{article}

usepackage{float} 
usepackage{lscape} 
usepackage{booktabs} 
usepackage{tabularx} 
usepackage{multirow}
usepackage{array}
usepackage{enumitem}
begin{table*}[t!]
    caption{Table} 
    label{tab:tbl2}
    centering % center the table
    begin{tabular}{ccccc} 
    hline 
    {}      & multicolumn{2}{c}{a}                 & multicolumn{2}{c}{b}                  cmidrule(lr){2-3} cmidrule(lr){4-5}
    {c}     & d                                     & e                     &   f   & g       hline
    multirow{2}{*}{h}                              & multirow{2}{*}{i}    &   j   & multirow{2}{*}{k}    
    {}      & {}                                    & f                     &   {}  & h       hline
    multirow{4}{*}{l}                              & multirow{4}{*}{m}    &   n   & {}    
     {}     & {}                                    & b                     &   $  & c      
     {}     & {}                                    & d                     &   f   & {}    
     {}     & {}                                    & f                     &   {}  & {}    
    hline
    end{tabular}
end{table*}
end{document}

enter image description here

Answered by js bibra on May 27, 2021

If you actually want vertically rules compatible with the horizontal rules of booktabs (even though this is not at all in the spirit of booktabs), you should use {NiceTabular} of nicematrix.

documentclass{article}
usepackage{booktabs}
usepackage{nicematrix}
usepackage{caption}

begin{document}
begin{table}[ht!]
centering
    caption{Before} 
    label{tab:before}
    centering
    begin{NiceTabular}{c | c | c c | c} 
    toprule 
      & multicolumn{2}{c}{aaa}  & Block{1-2}{bbb}   
    cmidrule(lr){2-3} cmidrule(lr){4-5}
    ccc  & ddd  & eee & fff  & ggg 
    hline
    Block{2-1}{hhh}  
    & Block{2-1}{iii}        
    & jjj
    & Block{2-1}{kkk}
    
    & & fff & & hhh
    
    midrule
    Block{4-1}{lll} 
    & Block{4-1}{mmm}        
    & nnn
    & 
    &  
    
    & & bbb & $ccc$ 
     
    & & ddd & fff 
    
    & & fff & &
    
    bottomrule
    end{NiceTabular}
end{table}
end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

Answered by F. Pantigny on May 27, 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