TransWikia.com

Why is the left alignment leaving space in table?

TeX - LaTeX Asked by Book Of Flames on June 15, 2021

I have this:

documentclass{article}

usepackage{longtable, array}
newcolumntype{L}[1]{>{raggedrightletnewlinearraybackslashhspace{0pt}}m{#1}}

begin{document}

begin{longtable}{|L{0.2textwidth}|L{0.2textwidth}L{0.2textwidth}L{0.2textwidth}|}hline
multicolumn{1}{|c|}{large Physical Properties} & 
multicolumn{1}{c|}{large Solid} & 
multicolumn{1}{c|}{large Liquid} & multicolumn{1}{c|}{large Gas}  hline
Shape & Definite & Indefinite & Indefinite  hline
Volume & Definite & Definite & Indefinite  hline
Force of Attraction & Very Strong & Less than solid & Least  hline
Fluidity & Doesn't flow & Can flow & Can blow  hline
Compressibility & Can't be compressed & Can't be compressed & Highly compressed  hline
Diffusion & Very Slow & Fast & Fastest  hline
end{longtable}

end{document}

which produces (the unwanted spaces are marked in yellow):
enter image description here

The items in the leftmost column are aligned to the left (as wanted) but leave some space before beginning.

How to fix this? Thanks!

2 Answers

Prescribed width of the first column is smaller than width of the multicolumn cell in it. Simpler remedy is to use natural width for this column:

documentclass{article}

usepackage{array, longtable}
newcolumntype{L}[1]{>{raggedrightarraybackslash}m{#1}}
NewExpandableDocumentCommandmcc{O{c|}m}{multicolumn{1}{#1}{#2}}

begin{document}
setlengthextrarowheight{2pt}
setlengthtabcolsep{4pt}
begin{longtable}{|l | *{3}{L{0.2textwidth}|} }
    hline
large Physical Properties 
    &   mcc{large Solid} 
        & mcc{large Liquid} 
            & mcc{large Gas}                               hline
Shape       & Definite      & Indefinite    & Indefinite     hline
Volume      & Definite      & Definite      & Indefinite     hline
Force of Attraction 
            & Very Strong   & Less than solid 
                                            & Least          hline
Fluidity    & Doesn't flow  & Can flow      & Can blow       hline
Compressibility 
            & Can't be compressed 
                            & Can't be compressed 
                                            & Highly compressed  hline
Diffusion   & Very Slow     & Fast          & Fastest            hline
end{longtable}

end{document}

enter image description here

I wonder, if you really need longtable for this table? If it is shorter than one page, is sensible (at least to my opinion) to use tabularx table. In this case you not need to take a care that table is not wider to textwidth:

documentclass{article}

usepackage{tabularx}
newcolumntype{L}{>{raggedrightarraybackslash}X}
NewExpandableDocumentCommandmcc{O{c|}m}{multicolumn{1}{#1}{#2}}

begin{document}

setlengthextrarowheight{2pt}
noindent
begin{tabularx}{linewidth}{|l | *{3}{L|} }
    hline
large Physical Properties 
    &   mcc{large Solid} 
        & mcc{large Liquid} 
            & mcc{large Gas}                               hline
Shape       & Definite      & Indefinite    & Indefinite     hline
Volume      & Definite      & Definite      & Indefinite     hline
Force of Attraction 
            & Very Strong   & Less than solid 
                                            & Least          hline
Fluidity    & Doesn't flow  & Can flow      & Can blow       hline
Compressibility 
            & Can't be compressed 
                            & Can't be compressed 
                                            & Highly compressed  hline
Diffusion   & Very Slow     & Fast          & Fastest            hline
end{tabularx}

end{document}

Result is almost the same as before.

Addendum: If table is longer than one page, you may consider to use xltabular table. Beside this, considering your question in comment below, use makecell and booktabs packages, without vertical rules:

documentclass{article}

usepackage{booktabs, makecell, xltabular}
renewcommandtheadfont{bfseries}
renewcommandtheadgape{}
newcolumntype{L}{>{raggedrightarraybackslash}X}

begin{document}

begingroup
setcellgapes{2pt}
makegapedcells
normalsizelinespread{0.84}selectfont
begin{xltabular}{linewidth}{ L{1.15} *{3}{L{0.95}} }
    toprule
thead[l]{Physical Properties} 
    &   thead[l]{Solid} 
        & thead[l]{Liquid} 
            & thead[l]{Gas}                               
    midrule
Shape       & Definite      & Indefinite    & Indefinite     
Volume      & Definite      & Definite      & Indefinite     
    addlinespace
Force of Attraction 
            & Very Strong   & Less than solid 
                                            & Least          
Fluidity    & Doesn't flow  & Can flow      & Can blow       
    addlinespace
Compressibility 
            & Can't be compressed 
                            & Can't be compressed 
                                            & Highly compressed  
Diffusion   & Very Slow     & Fast          & Fastest            
    bottomrule
end{xltabular}
endgroup

end{document}

enter image description here

Correct answer by Zarko on June 15, 2021

You must increase width of first column.

documentclass{article}

usepackage{longtable, array}
newcolumntype{L}[1]{>{raggedrightletnewlinearraybackslashhspace{0pt}}m{#1}}

begin{document}

begin{longtable}{|L{0.3textwidth}|L{0.2textwidth}L{0.2textwidth}L{0.2textwidth}|}
hline
multicolumn{1}{|c|}{large Physical Properties} & 
multicolumn{1}{c|}{large Solid} & 
multicolumn{1}{c|}{large Liquid} & multicolumn{1}{c|}{large Gas}  hline
Shape & Definite & Indefinite & Indefinite  hline
Volume & Definite & Definite & Indefinite  hline
Force of Attraction & Very Strong & Less than solid & Least  hline
Fluidity & Doesn't flow & Can flow & Can blow  hline
Compressibility & Can't be compressed & Can't be compressed & Highly compressed  hline
Diffusion & Very Slow & Fast & Fastest  hline
end{longtable}

end{document}

Answered by Bahman Mirzakhani on June 15, 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