Graphics, Figures & TablesCaption in ltablex make table narrow

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Caption in ltablex make table narrow

Post by stinkinrich88 »

Hi.

I need a tabularx table to span across pages, so I'm using the ltablex package. It works fine! Except when I include a caption... When the caption is included, it makes the table narrower. Here is an example:

Code: Select all

\begin{tabularx}{\linewidth}{lX}
		\toprule
		col1 & col2 \\
		\midrule
		Name & %
		Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition\\
		Name 2 & %
		Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
		Name 3 & %
		Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
		\bottomrule
\caption{Quite a long caption}\\
	\end{tabularx}
Remove the line with \caption on it and it looks normal. Any ideas how I can get it to look normal with a caption?

Thanks!
Last edited by stinkinrich88 on Fri Oct 01, 2010 5:58 pm, edited 1 time 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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Caption in ltablex make table narrow

Post by gmedina »

Please post complete, minimal code and not just snippets.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Caption in ltablex make table narrow

Post by stinkinrich88 »

Sorry, here's a complete version:

Code: Select all

\documentclass{article}
\usepackage{ltablex}
\usepackage{caption}

\begin{document}
    \begin{tabularx}{\linewidth}{lX}
        col1 & col2 \\
        Name & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition\\
        Name 2 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
        Name 3 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
    \caption{Quite a long caption quite a long caption quite a long caption}\\
    \end{tabularx}
\end{document}
Notice that the table is very narrow. Now remove the caption and the table is the correct size.
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Caption in ltablex make table narrow

Post by stinkinrich88 »

I've just realised that if I fix the width of the first column, the problem is solved.

i.e. change the following line in my example:

Code: Select all

\begin{tabularx}{\linewidth}{lX}
to this:

Code: Select all

\begin{tabularx}{\linewidth}{p{2cm}X}
That's all the experimenting I've done, but that should be enough to help anyone else with this problem.

My guess is that the package has a bug. I won't make this post as solved in case anyone else has a better solution that doesn't require a fixed width column.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Caption in ltablex make table narrow

Post by localghost »

stinkinrich88 wrote:I've just realised that if I fix the width of the first column, the problem is solved. […]
Then please be so kind and mark the topic accordingly.
stinkinrich88 wrote:[…] My guess is that the package has a bug. I won't make this post as solved in case anyone else has a better solution that doesn't require a fixed width column.
If you think so, send a note to the package maintainer and report back with new insights.


Thorsten
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Re: Caption in ltablex make table narrow

Post by stinkinrich88 »

Very well.... but as I mentioned in my previous post, I didn't want to mark it as solved as a better solution would be much appreciated. I would prefer not to use fixed width columns in my table, so if anyone knows another way then please let me know!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Caption in ltablex make table narrow

Post by localghost »

Well, either I misunderstood something or you talked a little bit light-minded about the problem as solved. Nevermind. You are free to change the status of the topic if you consider that necessary.

I had the opportunity for some tests and I would consider this a bug in the package. I found out some interesting things. The misbehaviour seems to depend on the line width. If I increase this length, the package allows a longer caption without problems, but not as long as you want in your example. However, the appearance of the resulting table is not as I would expect it from tabularx and the width of the »X« column is wrong. The width of the table itself doesn't match the given width in the table head. And the example from the package manual is not very expressive so that from this side there will be no help. I recommend to contact the package maintainer. Feel free to post the results of the discussion here. It would be very interesting to hear his opinion and we might see a suggestion for a solution.
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Re: Caption in ltablex make table narrow

Post by stinkinrich88 »

Thanks! I'll get in touch and give him/her a link to this thread.
fpz
Posts: 1
Joined: Tue Sep 03, 2013 3:27 pm

Caption in ltablex make table narrow

Post by fpz »

Hi,

I'm having this exact same issue: using a caption in a tabularx environment (with ltablex) makes the table narrower.

It seems like the \caption{} width is used to compute the width of the first column which distorts the computation of the whole table.

Any news from the package maintainer ? Or any hints to fix this issue ?

Thanks a lot.
Last edited by cgnieder on Tue Sep 03, 2013 8:48 pm, edited 1 time in total.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Caption in ltablex make table narrow

Post by sommerfee »

The main problem is the redefinition of \caption inside \TX@endtabularx which is

Code: Select all

\def\caption{\\}
but should be something like

Code: Select all

\def\caption#1{\\}%
instead, or even better

Code: Select all

\def\caption{\caption@withoptargs\TX@caption}
\def\TX@caption#1#2{\\}
so the starred variant and the optional argument of \caption will still work:

Code: Select all

\documentclass{article}
\usepackage{ltablex}
\usepackage{caption}

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\TX@endtabularx}% <cmd>
  {\def\caption}% <search>
  {\def\caption{\caption@withoptargs\TX@caption}%
   \def\TX@caption##1##2}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}
    \begin{tabularx}{\linewidth}{lX}
        col1 & col2 \\
        Name & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition\\
        Name 2 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
        Name 3 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
    \caption{Quite a long caption quite a long caption quite a long caption}\\
    \end{tabularx}

    \begin{tabularx}{\linewidth}{lX}
        col1 & col2 \\
        Name & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition\\
        Name 2 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
        Name 3 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
    \caption[Text for LOT]{Quite a long caption quite a long caption quite a long caption}\\
    \end{tabularx}

    \begin{tabularx}{\linewidth}{lX}
        col1 & col2 \\
        Name & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition\\
        Name 2 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
        Name 3 & %
        Really loonggg descrition Really loonggg descrition Really loonggg descrition Really loonggg descrition \\
    \caption*{Quite a long caption quite a long caption quite a long caption}\\
    \end{tabularx}
\end{document}
(\caption@withoptargs is offered by caption3.sty which is part of the caption package bundle.)

We already had this issue (solved) in 2010, but I'm not able to find the corresponding thread so far...

(An e-mail to the package author remained unanswered.)
Post Reply