TransWikia.com

How can I change the table width to fit in a two column layout?

TeX - LaTeX Asked by Nasser_Omar on January 19, 2021

I’m using two columns ACM format to write a research. I used the below code to create a table. The issue that the code has been created is too large in width and it’s extended to the whole page. I need help to shrunk its width.

 begin{table}[h!]
 begin{tabular}{lcccc}
 toprule
 Benchmark & Number of Nodes & Residual Energy of Original & Residual Energy of Modified & Percentage of Residual Energy %  [0.5ex]
 hline
 1  & 10 & 1.171 J & 1.214 J & 4.3%   
hline
 2 & 30 & 1.145 J & 1.197 J & 5.2% 
 hline
 3 & 50 & 1.116 J & 1.163 J & 4.7%  
hline
 4 & 70 & 1.152 J & 1.186 J & 3.4% 
 hline
 5 & 100  & 1.193 J & 1.235 J & 4.15%  
hline
 Avg &   & 1.1554 J & 1.1989 J & 4.35% 
 bottomrule
end{tabular}
end{table}

3 Answers

You can use tabularx environment instead of tabular. In that case you can, not only set the width of the table to the textwidth (can also be achieved by tabular*), but also you avoid the overflow of the columns into the margins by using X column type which force equal column widths.

Using your posted example, you may get something like this

enter image description here

With the full minimal code,

documentclass{article}
usepackage{booktabs}
usepackage{tabularx}
%
begin{document}
begin{table}[h!]
    begin{tabularx}{textwidth}{XXXXX}
        toprule
        Benchmark & Number of Nodes & Residual Energy of Original & Residual Energy of Modified & Percentage of Residual Energy %  [0.5ex]
        hline
        1  & 10 & 1.171 J & 1.214 J & 4.3% 
        hline
        2 & 30 & 1.145 J & 1.197 J & 5.2% 
        hline
        3 & 50 & 1.116 J & 1.163 J & 4.7%  
        hline
        4 & 70 & 1.152 J & 1.186 J & 3.4% 
        hline
        5 & 100  & 1.193 J & 1.235 J & 4.15%  
        hline
        Avg &   & 1.1554 J & 1.1989 J & 4.35% 
        bottomrule
    end{tabularx}
end{table}
end{document}

Answered by hesham on January 19, 2021

Here is how you could make your table fit into one column of a twocolumn article. I have also removed the hline commands and used siunitx in order to improve tha alignment of the numbers in the table. In order to avoid repetition in table, I have placed the units in the corresponding column headers and in order to sapce space, I added linebreaks using the thead command from the makecell package. Lastly, I have alsu used tabular* in combination with extracolsep to make sure, the table is exactly as wide as the column of text.

Depending on the document calss you use, the output might differ but you should nevertheless get the general idea of how I ensured your table fits into one column.

enter image description here

documentclass[twocolumn]{article}
usepackage{lipsum}
usepackage{booktabs}
usepackage{siunitx}
usepackage{makecell}
renewcommand{theadfont}{normalsize}
begin{document}
begin{table}
setlength{tabcolsep}{0pt}
  begin{tabular*}{linewidth}{@{extracolsep{fill}}lS[table-format=3] *{2}{S[table-format=1.4]} S[table-format=1.2]}
    toprule
  & & multicolumn{3}{c}{thead{Residual Energy}}
    cmidrule{3-5}
{thead[l]{Bench-mark}} 
  & {thead{No. of Nodes}} 
    & {thead{Original in si{joule}}} 
      & {thead{Modified in si{joule}}}
        & {thead{Percentage}} 
 midrule
 1   & 10  & 1.171   & 1.214  & 4.3   
 2   & 30  & 1.145   & 1.197  & 5.2 
 3   & 50  & 1.116   & 1.163  & 4.7  
 4   & 70  & 1.152   & 1.186  & 3.4 
 5   & 100 & 1.193   & 1.235  & 4.15  
 Avg &     & 1.1554  & 1.1989 & 4.35 
 bottomrule
end{tabular*}
end{table}
lipsum
end{document}

Answered by leandriis on January 19, 2021

By "ACM format", I assume you mean the acmart document class. Please advise if this assumption is incorrect.

I suggest you replace the tabular environment with a tabularx environment to allow automatic line breaking in the header cells. I would also like to suggest that you take information about the units (joule, %) out of the body of the table and into the header instead.

enter image description here

documentclass[twocolumn]{acmart}
usepackage{booktabs}

% new code:
raggedbottom
usepackage{tabularx,ragged2e,siunitx}
newcolumntype{Y}[1]{>{Centeringhspace{0pt}hsize=#1hsize}X}
usepackage{lipsum} % for filler text

begin{document}

begin{table}[h!]
begin{tabularx}{columnwidth}{@{} Y{0.73} Y{0.85} Y{1.09} Y{1.09} Y{1.24} @{}}
% Note: 0.73+0.85+1.09+1.09+1.24 = 5 = # of X-type columns
toprule
Benchmark & Number of Nodes & 
Residual Energy of Original & 
Residual Energy of Modified & 
Percentage of Residual Energy  
 & & [,J] & [,J] & [%] 
midrule
  1 &  10 & 1.171 & 1.214 & 4.3    
  2 &  30 & 1.145 & 1.197 & 5.2  
  3 &  50 & 1.116 & 1.163 & 4.7  
  4 &  70 & 1.152 & 1.186 & 3.4  
  5 & 100 & 1.193 & 1.235 & 4.15 
addlinespace
 Avg&     & 1.1554& 1.1989& 4.35 
bottomrule
end{tabularx}
end{table}

lipsum % filler text
end{document}

Answered by Mico on January 19, 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