1

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
  • 1
    Welcome to TeX.SX! Please prepare a complete minimal working example (MWE) that also includes the documentclass as well as the relevant packages. How to make a table fit into the textwidth entirely depends on the available space which itself is controlled by the documentclass and parameters such as font and margin sized that you probably have changed yourself.
    – leandriis
    Commented Aug 15, 2020 at 11:52
  • 1
    Apart from my above comment about missing documentclass and packages, you code is not compilable since the tabular environment is missing the column declarations. If you try to compile your code as is, you should reveive a bunch of error messages. Please do not ignore them.
    – leandriis
    Commented Aug 15, 2020 at 11:54
  • Try with \begin{table*}...\end{table*}, note that this was my assumption, able to provide correct suggestion after seeing your complete MWE...
    – MadyYuvi
    Commented Aug 15, 2020 at 13:50

3 Answers 3

3

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]{>{\Centering\hspace{0pt}\hsize=#1\hsize}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}
1

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}
1
  • Quite comprehensive!
    – hesham
    Commented Aug 15, 2020 at 16:47
0

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}
2
  • The OP mentioned that the document uses a two column format.
    – Mico
    Commented Aug 15, 2020 at 18:59
  • Yea I noticed later, thanks. The answer however wouldn't be hard to adapt to two columns format, particulary, the OP didn't show issues in knowing how to produce that format.
    – hesham
    Commented Aug 15, 2020 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .