TransWikia.com

Why is my 7-column tabular(x) wider than specified?

TeX - LaTeX Asked by homocomputeris on December 17, 2020

I want a very simple table (tabular/x/*, whatever) with 7 columns, each 14% percent of the linewidth, 98% total.

But for some reason the rightmost column is a) on the margin, b) outside the table rules.
enter image description here

How do I fix it?

documentclass[version=last,12pt]{scrreport}
usepackage{tabularx,booktabs}

begin{document}
begin{table}[t]
tiny
begin{tabularx}{linewidth}{@{}
p{0.14linewidth}
p{0.14linewidth}
p{0.14linewidth}
p{0.14linewidth}
p{0.14linewidth}
p{0.14linewidth}
p{0.14linewidth}
@{}
}
toprule
Excavator
& Alphanumeric data 
& Location 
& Warranty details 
& Expiry date of the support period 
& Required business properties
& Maximum needed diesel

bottomrule
end{tabularx}
end{table}

end{document}     

One Answer

First version: Using tabularx's X type columns results in a table that is exactly as wide as the textwidth and consists of 7 equally wide columns. In order to left-align the columns, I used >{raggedrightarraybackslash} and defined a new custom column type L.

Second version: Here, I manually caclulated the column widths taking into account the value of tabcolsep which is added to the left and right of each column's contents. As you can see, the resulting table is, as expected, slightly narrower than the textwidth.

Third version: In this version, I used tabular* in combination with extracolsep{fill}} to make sure the table is as wide as the textwidth.

The vertical lines in the following screenshot are cause by the showframe package and show the textwidth/beginning of the margins.

enter image description here

Side note: I hope, the tiny font size was just used in this example. As this font size is quite hard to read as it is that tiny, I recommend not using it. You could easily fit the table into the textwidth, even with a regular font size, resulting in an output similar to

enter image description here

Depending on the table's contents, it might also be beneficial to allow different column widths.

documentclass[version=last,12pt]{scrreport}
usepackage{tabularx,booktabs}
newcolumntype{L}{>{raggedrightarraybackslash}X}

newcolumntype{Z}[2]{>{raggedrightarraybackslash}p{dimexpr #1textwidth- #2tabcolsep} }

newcolumntype{Y}[1]{>{raggedrightarraybackslash}p{dimexpr #1textwidth} }

usepackage{showframe}
begin{document}
begin{table}[t]
tiny
begin{tabularx}{linewidth}{@{}*{7}{L}@{}}
toprule
Excavator
& Alphanumeric data 
& Location 
& Warranty details 
& Expiry date of the support period 
& Required business properties
& Maximum needed diesel

bottomrule
end{tabularx}
end{table}


begin{table}[t]
tiny
begin{tabular}{@{}Z{0.14}{1} 
                   *{5}{Z{0.14}{2}}
                   Z{0.14}{1} @{}}
toprule
Excavator
& Alphanumeric data 
& Location 
& Warranty details 
& Expiry date of the support period 
& Required business properties
& Maximum needed diesel

bottomrule
end{tabular}
end{table}


begin{table}[t]
tiny setlength{tabcolsep}{0pt}
begin{tabular*}{textwidth}{@{extracolsep{fill}}*{7}{Y{0.14}}}
toprule
Excavator
& Alphanumeric data 
& Location 
& Warranty details 
& Expiry date of the support period 
& Required business properties
& Maximum needed diesel

bottomrule
end{tabular*}
end{table}

end{document}

 

Correct answer by leandriis on December 17, 2020

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