Graphics, Figures & Tablestabularx | Tables not filling Text Width

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
guitarmanjon
Posts: 8
Joined: Tue Jul 12, 2011 12:39 pm

tabularx | Tables not filling Text Width

Post by guitarmanjon »

Hi

I've a slight problem when using the tabularx package.

I would like all of the tables in my thesis to be the width of the page. I am trying to achieve this by using the tabularx package. It mostly works. However, if I have a caption that is less than 58 characters long the table does not spread across the page.

I include a MWE below. The first table has 59 characters and produces the desired output, the second has 58 characters and is not how I would like it.

Code: Select all

% ************ Preamble ****************
\documentclass[a4paper,12pt]{report}
\usepackage[top=2cm, bottom=2cm, left=3.5cm, right=2cm]{geometry} % sets margins
\usepackage{natbib} % for harvard style referencing
\usepackage[margin=10pt, labelfont=bf, format=hang, textfont={small,it}]{caption} % for more interesting captions
\captionsetup[subfigure]{style=default, margin=0pt, labelfont=bf, textfont={small,it}, singlelinecheck=true} % makes subfigure captions a bit more interesting.
\usepackage{multirow} % allows merging of rows in tables
\usepackage{ltablex} % Combines tabularx and longtable functionality
\usepackage{booktabs} % For professional looking tables

% *********** BEGIN! ******************
\begin{document}

\begin{tabularx}{\textwidth}{X X X X X X X}
	\caption{An example table that does not spread across the whole widt}\\
		\toprule
		\textbf{Number} & \multicolumn{6}{l}{\textbf{Number}} \\
		\textbf{(samples)} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} \\ \hline
		1 & \textit{1} & -- & -- & -- & -- & --\\
		2 & \textit{2} & -- & -- & -- & -- & --\\
		3--5 & \textit{3} & -- & -- & -- & -- & --\\
		6--10 & \textit{4} & \textit{5} & -- & -- & -- & --\\
		11--18 & \textit{6} & \textit{7} & \textit{8} & -- & -- & --\\
		\bottomrule
		\label{tab:this_one_does}
\end{tabularx}

\begin{tabularx}{\textwidth}{X X X X X X X}
	\caption{An example table that does not spread across the whole wid}\\
		\toprule
		\textbf{Number} & \multicolumn{6}{l}{\textbf{Number}} \\
		\textbf{(samples)} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} \\ \hline
		1 & \textit{1} & -- & -- & -- & -- & --\\
		2 & \textit{2} & -- & -- & -- & -- & --\\
		3--5 & \textit{3} & -- & -- & -- & -- & --\\
		6--10 & \textit{4} & \textit{5} & -- & -- & -- & --\\
		11--18 & \textit{6} & \textit{7} & \textit{8} & -- & -- & --\\
		\bottomrule
		\label{tab:this_one_does_not}
\end{tabularx}

\end{document}
Any advice on how to solve this issue (other than making my captions longer than they need to be!) would be welcome.

Thank you.
Jon
Last edited by guitarmanjon on Sun Oct 09, 2011 9:11 pm, edited 3 times in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx | Tables not filling Text Width

Post by localghost »

The \caption command within the tabularx environment is the culprit. It has nothing to do there. I'm astonished that it works at all outside a float environment with causing any error.

If you don't want the tables to float, use the \captionof command from the caption package that you already load and put it outside right behind the tabularx environments. To avoid page breaks between the tables and their corresponding captions, put the whole stuff into a minipage environment.


Thorsten
guitarmanjon
Posts: 8
Joined: Tue Jul 12, 2011 12:39 pm

Re: tabularx | Tables not filling Text Width

Post by guitarmanjon »

Thank you Thorsten. Problem solved. I had had issues with captions and tabularx before, finding that I had to put \\ after the caption to avoid errors.

Your suggestion for minipage has resolved the other problem that I would no doubt have asked about in a few days time!

Edit: I avoided putting tabularx tables in a table environment as I found this increased the table number counter by 2 everytime. I will use minipage around my tables from now on.

Thank you!
Jon
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx | Tables not filling Text Width

Post by localghost »

guitarmanjon wrote:[…] I avoided putting tabularx tables in a table environment as I found this increased the table number counter by 2 everytime. I will use minipage around my tables from now on. […]
This behaviour is caused by the longtable package (loaded by ltablex) when using the longtable environment not correctly with the corresponding caption (see the package manual for details). I couldn't reproduce it with your example.
guitarmanjon
Posts: 8
Joined: Tue Jul 12, 2011 12:39 pm

tabularx | Tables not filling Text Width

Post by guitarmanjon »

Thorsten (or anyone else in fact),

Thank you once again for your help with this matter. However, I have a question related to my previous problem.

After your last response I started putting all of my tabularx tables in minipage environments. Only after compiling did I notice that this doesn't solve the problem of tables not filling the page width (some do, some don't). Also, I've noticed that using a tabularx in a minipage with a \captionof command seems to result in the table counter increasing by two for every table. I've included a MWE for this behaviour.

Code: Select all

% ************ Preamble ****************
\documentclass[a4paper,12pt]{report}
\usepackage[top=2cm, bottom=2cm, left=3.5cm, right=2cm]{geometry} % sets margins
\usepackage{natbib} % for harvard style referencing
\usepackage[margin=10pt, labelfont=bf, format=hang, textfont={small,it}]{caption} % for more interesting captions
\captionsetup[subfigure]{style=default, margin=0pt, labelfont=bf, textfont={small,it}, singlelinecheck=true} % makes subfigure captions a bit more interesting.
\usepackage{multirow} % allows merging of rows in tables
\usepackage{ltablex} % Combines tabularx and longtable functionality
\usepackage{booktabs} % For professional looking tables

% *********** BEGIN! ******************
\begin{document}

Tables~\ref{tab:1}, \ref{tab:2} and \ref{tab:3} are not numbered correctly.

\noindent
\begin{minipage}[htb]{\textwidth}
	\captionof{table}{A table.}
	\begin{tabularx}{\textwidth}{X X X X X X X}
			\toprule
			\textbf{Number} & \multicolumn{6}{l}{\textbf{Number}} \\
			\textbf{(samples)} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} \\ \hline
			1 & \textit{1} & -- & -- & -- & -- & --\\
			2 & \textit{2} & -- & -- & -- & -- & --\\
			3--5 & \textit{3} & -- & -- & -- & -- & --\\
			6--10 & \textit{4} & \textit{5} & -- & -- & -- & --\\
			11--18 & \textit{6} & \textit{7} & \textit{8} & -- & -- & --\\
			\bottomrule
			\label{tab:1}
	\end{tabularx}
\end{minipage}

\noindent
\begin{minipage}[htb]{\textwidth}
\captionof{table}{Another table.}
\begin{tabularx}{\textwidth}{X X X X X X X}
		\toprule
		\textbf{Number} & \multicolumn{6}{l}{\textbf{Number}} \\
		\textbf{(samples)} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} \\ \hline
		1 & \textit{1} & -- & -- & -- & -- & --\\
		2 & \textit{2} & -- & -- & -- & -- & --\\
		3--5 & \textit{3} & -- & -- & -- & -- & --\\
		6--10 & \textit{4} & \textit{5} & -- & -- & -- & --\\
		11--18 & \textit{6} & \textit{7} & \textit{8} & -- & -- & --\\
		\bottomrule
		\label{tab:2}
\end{tabularx}
\end{minipage}

\noindent
\begin{minipage}[htb]{\textwidth}
\captionof{table}{And another table.}
\begin{tabularx}{\textwidth}{X X X X X X X}
		\toprule
		\textbf{Number} & \multicolumn{6}{l}{\textbf{Number}} \\
		\textbf{(samples)} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} \\ \hline
		1 & \textit{1} & -- & -- & -- & -- & --\\
		2 & \textit{2} & -- & -- & -- & -- & --\\
		3--5 & \textit{3} & -- & -- & -- & -- & --\\
		6--10 & \textit{4} & \textit{5} & -- & -- & -- & --\\
		11--18 & \textit{6} & \textit{7} & \textit{8} & -- & -- & --\\
		\bottomrule
		\label{tab:3}
\end{tabularx}
\end{minipage}


\end{document}
I've tried using a table environment instead of a minipage one as I would like the tables to float but the behaviour is the same.

Any suggestions to overcome this bizarre behaviour?

Thanks
Jon
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx | Tables not filling Text Width

Post by localghost »

The problem that I have with your example is that there is no reason for loading the ltablex package because you don't use it. Furthermore this package is very old (1995). Your code works fine for me if I replace it with tabularx. And this works also fine in float environments without side effects. If you need both features, take a look at the quite new tabu package. Probably your problems then disappear.

By the way, you are using the minipage environment not correctly. It has no placement parameters like the float environments but optional alignment parameters with respect to the current line.
guitarmanjon
Posts: 8
Joined: Tue Jul 12, 2011 12:39 pm

Re: tabularx | Tables not filling Text Width

Post by guitarmanjon »

Once again, you have solved my problems. Thank you so much.

The ltablex package was included as I had used it in previous MWEs which required it. I have now opted for the tabu package which seems to be working exactly as I would like it to.

Thank you again.
jon
Post Reply