TransWikia.com

cells not centering after multicell

TeX - LaTeX Asked by BobLee on July 2, 2021

I’m trying to create a tabular with all the cells centered. I have found a way to make this possible but all the cells have to contain:

multicolumn{1}{|c|}{"Text"}

Is there a way to center the text without the need to add the multicolumn command everywhere?

Tabular

documentclass[10pt]{article}
usepackage[english]{babel}
usepackage{a4wide}      
usepackage[utf8]{inputenc}     
usepackage{mathtools}
usepackage{float}
usepackage{makecell}
usepackage{slashbox}
usepackage{multirow}
usepackage{array}

begin{document}
begin{table}[H]
centering
begin{tabular}    {|c|p{.7textwidth/5}|p{.7textwidth/5}|p{.7textwidth/5}|p{.7textwidth/5}|}
hline
multirow{2}{*}{backslashbox{Distance [m]}{Module}} & multicolumn{2}{c|}{Range Test [dBm]} & multicolumn{2}{c|}{Throughput Test [Kbps]}cline{2-5}
   & multicolumn{1}{c|}{A-B} & multicolumn{1}{c|}{A-C} & multicolumn{1}{c|}{A-B} & multicolumn{1}{c|}{A-C} cline{1-5}
multicolumn{1}{|c|}{10} & multicolumn{1}{c|}{•} & • & • & •  
hline 
multicolumn{1}{|c|}{15} & • & • & • & •  
hline 
multicolumn{1}{|c|}{20} & • & • & • & •  
hline 
multicolumn{1}{|c|}{25} & • & • & • & •  
hline 
multicolumn{1}{|c|}{30} & • & • & • & •  
hline 
multicolumn{1}{|c|}{35} & • & • & • & •  
hline 
multicolumn{1}{|c|}{40} & • & • & • & •  
hline 
multicolumn{1}{|c|}{45} & • & • & • & •  
hline 
multicolumn{1}{|c|}{50} & • & • & • & •  
hline 
end{tabular} 
end{table}

end{document}

when I try and use only centered cells I get the issue that the cells are not equally sized.
table centered

3 Answers

text in your columns is not centered since you prescribe p{...} column type. as mentioned in comments to your question you should modify column type to:

>{centerngarraybackslash}p{<column width>}

However, I would not use slashbox for first cell in table and for table I would use tabularx table environment and equal width in all columns:

enter image description here

Also I would remove all unnecessary multicolumns:

documentclass[10pt]{article}
usepackage[english]{babel}
usepackage{a4wide}
%usepackage[utf8]{inputenc}
usepackage{mathtools}
%usepackage{float}
usepackage{array, makecell, multirow, tabularx}
%usepackage{slashbox}

begin{document}
    begin{table}[htb]
    centering
    renewcommandarraystretch{1.2}
begin{tabularx}{0.7linewidth}{| c | *{4}{>{centeringarraybackslash}X|}}
    hline
    & multicolumn{4}{c|}{Module}            
    cline{2-5}
multirow{3}{=}{centering  Distance [m]}
    & multicolumn{2}{c|}{Range Test [dBm]}
                                & multicolumn{2}{c|}{Throughput Test [Kbps]}   
    cline{2-5}
   &  A-B   &   A-C &   A-B & A-C        
   cline{1-5}
10  &   •   &   •   &   •   & •         
    hline
15  &   •   &   •   &   •   & •         
    hline
20  &   •   &   •   &   •   & •         
    hline
25  &   •   &   •   &   •   & •         
    hline
30  &   •   &   •   &   •   & •         
    hline
35  &   •   &   •   &   •   & •         
    hline
40  &   •   &   •   &   •   & •         
    hline
45  &   •   &   •   &   •   & •         
    hline
50  &   •   &   •   &   •   & •         
    hline
end{tabularx}
    end{table}
end{document}

Answered by Zarko on July 2, 2021

Here a much shorter code:

documentclass[10pt]{article}
usepackage[english]{babel}
%usepackage{a4wide}
usepackage{geometry}
usepackage[utf8]{inputenc}
usepackage{mathtools}
usepackage{float}
usepackage{makecell}
%usepackage{slashbox}
usepackage{diagbox}
usepackage{multirow}
usepackage{array}

begin{document}
begin{table}[H]
centeringsetlength{extrarowheight}{2pt}
begin{tabular} {|c|*{4}{>{centeringarraybackslash}p{.7textwidth/5}|}}
hline
multirow{2}{*}{diagbox[height=dimexpr2line +6pt]{Distance rlap{{[m]}}}{Module}} & multicolumn{2}{c|}{Range Test [dBm]} & multicolumn{2}{c|}{Throughput Test [Kbps]}[2pt]cline{2-5}
   & A-B & A-C & A-B & A-C cline{1-5}
10 & • & • & • & • 
hline
15
 & • & • & • & • 
hline
20 & • & • & • & • 
hline
25 & • & • & • & • 
hline
30 & • & • & • & • 
hline
35 & • & • & • & • 
hline
40 & • & • & • & • 
hline
 45 & • & • & • & • 
hline
50 & • & • & • & • 
hline
end{tabular}
end{table}

end{document} 

enter image description here

Answered by Bernard on July 2, 2021

Here is how you can construct that table with {NiceTabular} of nicematrix.

 documentclass{article}
 usepackage{nicematrix}
 usepackage{float}

 begin{document}
 begin{table}[H]
 centeringsetlength{extrarowheight}{2pt}
 begin{NiceTabular}{w{c}{3cm}*{4}{w{c}{0.14textwidth}}}[hvlines]
 Block{2-1}{diagbox{Distance [m]}{Module}} & Block{1-2}{Range Test [dBm]} && Block{1-2}{Throughput Test [Kbps]}
    & A-B & A-C & A-B & A-C 
 10 & • & • & • & • 
 15 & • & • & • & • 
 20 & • & • & • & • 
 25 & • & • & • & • 
 30 & • & • & • & • 
 35 & • & • & • & • 
 40 & • & • & • & • 
 45 & • & • & • & • 
 50 & • & • & • & • 
 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 July 2, 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