Graphics, Figures & Tablestabularx, multicolumn, and textwidth

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

tabularx, multicolumn, and textwidth

Post by rspringuel »

I'm trying to create a fairly simply table using tabularx that spans the page. While this seems to be working, for the most part, there's a slight issue when I combine this with a multicolumn which spans the whole table. In particular, the multicolumn stretches off to the right further than the text in the regular columns.

MWE:

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{L}{>{\raggedright}X}
\newcolumntype{R}{>{\raggedleft}X}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{LR}
\hline
Some left aligned text&Some right aligned text\tabularnewline
Some more left aligned text&Some more right aligned text\tabularnewline
\multicolumn{2}{p{\textwidth}}{Some text which is supposed to span the two columns and have it's extents align with the extents of the above column.  Nothing really important to read here, just filling it out so that the text is long enough to make the point.  You can stop reading now.}\tabularnewline
\hline
\end{tabularx}
\end{document}
Anyone have any idea what might be happening and how to fix it?
Last edited by rspringuel on Thu Feb 10, 2011 11:55 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

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

tabularx, multicolumn, and textwidth

Post by frabjous »

\textwidth would not be the same as the width of those two columns put together. Keep in mind that there is padding of \tabcolsep on each side of each cell, so the width is roughly \textwidth minus 2 * \tabcolsep. If you set it to just \textwidth it'll be too wide.

But when spanning columns with tabularx, I actually recommend using the X specifier with a modified \hsize, as discussed in section 4.3 of the tabularx documentation. \hsize would normally be width of one X-column. So for two X-columns, you want 2\hsize, but you also want to add back in the inner and outer padding for where those two columns meet, so we'll change \hsize to 2\hsize and then add 2\tabcolsep:

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{LR}
\hline
Some left aligned text&Some right aligned text\\
Some more left aligned text&Some more right aligned text\\
\multicolumn{2}{>{\setlength{\hsize}{2\hsize}\addtolength{\hsize}{2\tabcolsep}}X}{Some text which is supposed to span the two columns and have its extents align with the extents of the above column.  Nothing really important to read here, just filling it out so that the text is long enough to make the point.}\\
\hline
\end{tabularx}
\end{document}
span.png
span.png (7.8 KiB) Viewed 11331 times
P.S. I also recommend adding \arraybackslash into your columntypes to avoid the need for \tabularnewline rather than \\, but that's your call. I also fixed your "it's"/"its" typo for good measure. Don't hate me. ;)
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Re: tabularx, multicolumn, and textwidth

Post by rspringuel »

That does fix it. Thanks for the help.

I've purposely avoided the \arraybackslash in the new column types because this table is actually defined in a command and then used over and over again, potentially with multi-line arguments going into some of the cells which use '\\' to mark the line breaks.
Post Reply