LaTeX forum ⇒ Graphics, Figures & TablesCentering Tables with Width Greater than \textwidth

Information and discussion about graphics, figures & tables in LaTeX documents.
Goldenrod000
Posts: 2
Joined: Fri Jan 08, 2021 4:24 am

Centering Tables with Width Greater than \textwidth

Postby Goldenrod000 » Fri Jan 08, 2021 5:25 am

I've been using the tabularx package in hopes of representing a rather lengthy table as part of a LaTeX document. However, no matter what strategies I have tried, the table not only runs past the right side of the table, but isn't centered on the page as I would have hoped. This image demonstrates what the table looks like:
TableError.png
Demonstration of table running past the right side of the document
TableError.png (43.84 KiB) Viewed 168 times

Currently, I've tried using tabularx and specifying every column as X, but all this has proven to do is squish the cells that the contents of the table are in, to the point where the table looks visually unappealing. I've tried setting the width of the table to \pagewidth instead of \textwidth, but this has not alleviated the issue. I believe the issue stems from the fact that the left limit of the text is too far for the entire table to be displayed, but I'm unsure on how to fix this issue without changing the text width entirely, which I don't want to do.

Any thoughts on this matter? For reference, here is the excerpt of code I have used thus far to produce this table:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage{multirow}
  4. \usepackage{tabularx}
  5.  
  6. \begin{document}
  7. \begin{table}
  8. \begin{center}
  9. \begin{tabularx}{\paperwidth}{|X|X|X|X|X|X|X|X|X|}
  10. \hline
  11. \multirow{2}{*}{Sample} & \multicolumn{2}{l|}{Month 1} & \multicolumn{2}{l|}{Month 2} & \multicolumn{2}{l|}{Month 3} & \multicolumn{2}{l|}{Month 4} \\
  12. \cline{2 - 9}
  13. & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
  14. \hline
  15. 1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
  16. \hline
  17. 2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
  18. \hline
  19. 3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
  20. \hline
  21. 4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
  22. \hline
  23. 5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
  24. \hline
  25. 6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
  26. \hline
  27. 7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
  28. \hline
  29. 8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
  30. \hline
  31. 9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
  32. \hline
  33. 10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
  34. \hline
  35. \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
  36. \hline
  37. \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
  38. \hline
  39. \end{tabularx}
  40. \caption{Height of Sample Plants}
  41. \label{PlantHeight}
  42. \end{center}
  43. \end{table}
  44. \end{document}


Thank you all in advance for your help! :D

User avatar
Ijon Tichy
Posts: 392
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Fri Jan 08, 2021 10:26 am

First of all: You should never use center to center floats like table or figure, because center is a trivlist environment and so adds extra vertical space. Usually use \centering switch to center inside floats. However in this case this wouldn't be enough, because the tabularx would be still to wide and a centering will be done inside the text area. The only LaTeX centering command's, that allow to exceed the text area are \centerline or \makebox. However, also using \centerline the tabularx would be to wide, because the sum of the widths of all X[latex]-columns would be wider than [latex]\paperwidth. Here we have the case: You cannot take an elephant on the bus.

A suggestion would be to use the natural width of every column (here given by the colum heads). But I would not use l-, c-, or r-columns. For numbers the (configurable) S-columns of siunitx are recommended. See the manual for more information.
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage{multirow}
  4. \usepackage{siunitx}
  5.  
  6. \begin{document}
  7. \begin{table}
  8. \centerline{%
  9. \begin{tabular}{|l|*8{S|}}
  10. \hline
  11. \multirow{2}{*}{Sample} & \multicolumn{2}{l|}{Month 1} & \multicolumn{2}{l|}{Month 2} & \multicolumn{2}{l|}{Month 3} & \multicolumn{2}{l|}{Month 4} \\
  12. \cline{2 - 9}
  13. & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
  14. \hline
  15. 1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
  16. \hline
  17. 2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
  18. \hline
  19. 3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
  20. \hline
  21. 4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
  22. \hline
  23. 5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
  24. \hline
  25. 6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
  26. \hline
  27. 7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
  28. \hline
  29. 8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
  30. \hline
  31. 9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
  32. \hline
  33. 10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
  34. \hline
  35. \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
  36. \hline
  37. \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
  38. \hline
  39. \end{tabular}%
  40. }
  41. \caption{Height of Sample Plants}
  42. \label{PlantHeight}
  43. \end{table}
  44. \end{document}

BTW: Vertikal rules in tabulars are evil. So I would also suggest to remove them:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage{booktabs}
  4. \usepackage{multirow}
  5. \usepackage{siunitx}
  6.  
  7. \begin{document}
  8. \begin{table}
  9. \centerline{%
  10. \begin{tabular}{l*8{S}}
  11. \toprule
  12. \multirow{2}{*}{Sample} & \multicolumn{2}{l}{Month 1} & \multicolumn{2}{l}{Month 2} & \multicolumn{2}{l}{Month 3} & \multicolumn{2}{l}{Month 4} \\
  13. \cmidrule(r){2-3}
  14. \cmidrule(r){4-5}
  15. \cmidrule(r){6-7}
  16. \cmidrule{8-9}
  17. & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
  18. \midrule
  19. 1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
  20. 2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
  21. 3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
  22. 4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
  23. 5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
  24. 6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
  25. 7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
  26. 8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
  27. 9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
  28. 10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
  29. \midrule
  30. \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
  31. \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
  32. \bottomrule
  33. \end{tabular}%
  34. }
  35. \caption{Height of Sample Plants}
  36. \label{PlantHeight}
  37. \end{table}
  38. \end{document}

An alternative suggestion would be to rotate the table:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage[anticlockwise]{rotating}
  4. \usepackage{booktabs}
  5. \usepackage{multirow}
  6. \usepackage{siunitx}
  7.  
  8. \begin{document}
  9. \begin{sidewaystable}
  10. \centering
  11. \begin{tabular}{l*8{S}}
  12. \toprule
  13. \multirow{2}{*}{Sample} & \multicolumn{2}{l}{Month 1} & \multicolumn{2}{l}{Month 2} & \multicolumn{2}{l}{Month 3} & \multicolumn{2}{l}{Month 4} \\
  14. \cmidrule(r){2-3}
  15. \cmidrule(r){4-5}
  16. \cmidrule(r){6-7}
  17. \cmidrule{8-9}
  18. & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
  19. \midrule
  20. 1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
  21. 2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
  22. 3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
  23. 4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
  24. 5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
  25. 6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
  26. 7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
  27. 8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
  28. 9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
  29. 10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
  30. \midrule
  31. \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
  32. \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
  33. \bottomrule
  34. \end{tabular}%
  35. \caption{Height of Sample Plants}
  36. \label{PlantHeight}
  37. \end{sidewaystable}
  38. \end{document}


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 10 guests