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

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

Centering Tables with Width Greater than \textwidth

Post by Goldenrod000 »

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:
Demonstration of table running past the right side of the document
Demonstration of table running past the right side of the document
TableError.png (43.84 KiB) Viewed 18109 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

\documentclass{article}

\usepackage{multirow}
\usepackage{tabularx}

\begin{document}
	\begin{table}
		\begin{center}
			\begin{tabularx}{\paperwidth}{|X|X|X|X|X|X|X|X|X|}
				\hline
				\multirow{2}{*}{Sample} & \multicolumn{2}{l|}{Month 1} & \multicolumn{2}{l|}{Month 2} & \multicolumn{2}{l|}{Month 3} & \multicolumn{2}{l|}{Month 4} \\
				\cline{2 - 9}
				& {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
				\hline
				1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
				\hline
				2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
				\hline
				3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
				\hline
				4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
				\hline
				5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
				\hline
				6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
				\hline
				7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
				\hline
				8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
				\hline
				9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
				\hline
				10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
				\hline
				\textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
				\hline
				\textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
				\hline
			\end{tabularx}
			\caption{Height of Sample Plants}
			\label{PlantHeight}
		\end{center}
	\end{table}
\end{document}
Thank you all in advance for your help! :D

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

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

Centering Tables with Width Greater than \textwidth

Post by Ijon Tichy »

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

\documentclass{article}

\usepackage{multirow}
\usepackage{siunitx}

\begin{document}
\begin{table}
  \centerline{%
    \begin{tabular}{|l|*8{S|}}
      \hline
      \multirow{2}{*}{Sample} & \multicolumn{2}{l|}{Month 1} & \multicolumn{2}{l|}{Month 2} & \multicolumn{2}{l|}{Month 3} & \multicolumn{2}{l|}{Month 4} \\
      \cline{2 - 9}
      & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
      \hline
      1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
      \hline
      2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
      \hline
      3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
      \hline
      4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
      \hline
      5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
      \hline
      6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
      \hline
      7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
      \hline
      8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
      \hline
      9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
      \hline
      10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
      \hline
      \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
      \hline
      \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
      \hline
    \end{tabular}%
  }
  \caption{Height of Sample Plants}
  \label{PlantHeight}
\end{table}
\end{document}
BTW: Vertikal rules in tabulars are evil. So I would also suggest to remove them:

Code: Select all

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}

\begin{document}
\begin{table}
  \centerline{%
    \begin{tabular}{l*8{S}}
      \toprule
      \multirow{2}{*}{Sample} & \multicolumn{2}{l}{Month 1} & \multicolumn{2}{l}{Month 2} & \multicolumn{2}{l}{Month 3} & \multicolumn{2}{l}{Month 4} \\
      \cmidrule(r){2-3}
      \cmidrule(r){4-5}
      \cmidrule(r){6-7}
      \cmidrule{8-9}
      & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
      \midrule
      1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
      2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
      3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
      4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
      5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
      6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
      7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
      8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
      9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
      10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
      \midrule
      \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
      \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
      \bottomrule
    \end{tabular}%
  }
  \caption{Height of Sample Plants}
  \label{PlantHeight}
\end{table}
\end{document}
An alternative suggestion would be to rotate the table:

Code: Select all

\documentclass{article}

\usepackage[anticlockwise]{rotating}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}

\begin{document}
\begin{sidewaystable}
  \centering
    \begin{tabular}{l*8{S}}
      \toprule
      \multirow{2}{*}{Sample} & \multicolumn{2}{l}{Month 1} & \multicolumn{2}{l}{Month 2} & \multicolumn{2}{l}{Month 3} & \multicolumn{2}{l}{Month 4} \\
      \cmidrule(r){2-3}
      \cmidrule(r){4-5}
      \cmidrule(r){6-7}
      \cmidrule{8-9}
      & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} & {Control} & {(Vermiaqua)} \\
      \midrule
      1 & 0.9 & 1.0 & 2.4 & 2.5 & 2.4 & 4.2 & 4.1 & 5.5 \\
      2 & 0.8 & 1.5 & 2.3 & 2.4 & 2.5 & 4.0 & 4.3 & 5.2 \\
      3 & 1.0 & 1.5 & 2.5 & 2.5 & 2.4 & 3.9 & 4.2 & 5.0 \\
      4 & 1.2 & 1.0 & 2.4 & 2.3 & 2.6 & 3.9 & 4.0 & 5.3 \\
      5 & 1.1 & 1.5 & 2.4 & 2.6 & 2.5 & 3.8 & 4.0 & 5.1 \\
      6 & 0.7 & 1.4 & 2.4 & 2.7 & 2.6 & 3.8 & 4.0 & 5.1 \\
      7 & 0.9 & 1.3 & 2.2 & 2.8 & 2.4 & 4.1 & 4.1 & 5.4 \\
      8 & 1.0 & 1.2 & 2.3 & 2.5 & 2.6 & 4.0 & 4.2 & 5.3 \\
      9 & 0.8 & 1.3 & 2.5 & 2.5 & 2.6 & 4.3 & 4.1 & 5.0 \\
      10 & 0.9 & 1.0 & 2.4 & 2.4 & 2.5 & 4.0 & 4.1 & 5.0 \\
      \midrule
      \textit{Total} & 9.3 & 12.7 & 23.8 & 25.2 & 25.1 & 40.0 & 41.1 & 51.9 \\
      \textit{Average} & 0.93 & 1.27 & 2.38 & 2.52 & 2.51 & 4.00 & 4.11 & 5.19 \\
      \bottomrule
    \end{tabular}%
  \caption{Height of Sample Plants}
  \label{PlantHeight}
\end{sidewaystable}
\end{document}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply