0

How do I correctly multicolumn a tabularx while preserving the correct width?

When relatively short content is inserted into the \multicolumn, it can be done with l, c, or r. However, if the content does not fit into the width of the multi-column, one needs either X or p and the correct width must be computed. This question asks how to do this.

MWE

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\pagestyle{empty}

\newcommand{\sbl}{\tiny Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  Etiam lobortis facilisis sem.  Nullam nec mi et neque pharetra sollicitudin.  Praesent imperdiet mi nec ante.}

\begin{tabularx}{0.95\textwidth}{X|X|X|X|X}
  \hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & c\hfill 5\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & \sbl\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & \multicolumn{1}{X}{\sbl}\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & \multicolumn{2}{X}{\sbl}\\\hline
  c\hfill 1 & c\hfill 2 & \multicolumn{3}{X}{\sbl}\\\hline
  c\hfill 1 & \multicolumn{4}{X}{\sbl}\\\hline
  \multicolumn{5}{X}{\sbl}\\\hline
\end{tabularx}

\end{document}

rendering of mwe

Using 2\hsize does not do the trick as suggested many a place.

c\hfill 1 & c\hfill 2 & c\hfill 3 &
\multicolumn{2}{>{\hsize=2\hsize}X}{\sbl}\\\hline % <--- does not work

What is the correct way to do this?

2

The best I could come up with from various information I found was to introduce a new macro (\Xwdth) to pre-compute the size, taking into account additional width variables. Then I created a multicolumn wrapper \multiX for convenience.

\documentclass{article}
\usepackage{tabularx}
\begin{document}

\pagestyle{empty}

\newcommand{\Xwdth}[1]{\dimexpr#1\hsize+#1\tabcolsep+#1\tabcolsep+#1\arrayrulewidth-2\tabcolsep-\arrayrulewidth\relax}

\newcommand{\multiX}[2]{\multicolumn{#1}{>{\hsize=\Xwdth{#1}}X}{#2}}
\newcommand{\multiXr}[2][1]{\multicolumn{#1}{>{\hsize=\Xwdth{#1}\raggedright\arraybackslash}X}{#2}}

\newcommand{\sbl}{\tiny Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  Etiam lobortis facilisis sem.  Nullam nec mi et neque pharetra sollicitudin.  Praesent imperdiet mi nec ante.}

\begin{tabularx}{0.95\textwidth}{X|X|X|X|X}
  \hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & c\hfill 5\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & \sbl\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & c\hfill 4 & \multiX{1}{\sbl}\\\hline
  c\hfill 1 & c\hfill 2 & c\hfill 3 & \multiX{2}{\sbl}\\\hline
  c\hfill 1 & c\hfill 2 & \multiX{3}{\sbl}\\\hline
  c\hfill 1 & \multiX{4}{\sbl}\\\hline
  \multiX{5}{\sbl}\\\hline
\end{tabularx}

\end{document}

solution rendering

|improve this answer|||||
  • that is the answer (it is harder, basically not supported if you need to span ove l columns of unknown width) (it is better to make the table full width and then suppress the paragraph indent with \noindent before the table) – David Carlisle Sep 29 '19 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.