0

I'm trying to get a multicolumn cell to wrap the contents inside a tabulary table. The table is being dynamically generated, so I can't manually set the width of the multicolumn cell for wrapping. Instead I'd like it to wrap at the width of the combined columns it is spanning.

Here's a MWE. I'm coloring the cells so you can see how the multicolumns are overflowing the cells they span. My real table is much more complex and renders best in a tabulary environment, which is why I'm using it instead of tabular or tabularx.

\documentclass{article}
\usepackage{lipsum, multirow, tabulary, color, colortbl}
\newcommand\cc[1]{\cellcolor{yellow}#1}
\begin{document}

\begin{tabulary}{\linewidth}{|C|C|C|C|}
  \multicolumn{2}{c|}{\cc{\lipsum[1][1]}} &  \multicolumn{2}{c|}{\cc{\lipsum[1][1]}} \\
  \cc{One} & \cc{Two} & \cc{Three} & \cc{Four}
\end{tabulary}

\end{document}

enter image description here

A few things I've tried:

  • Wrapping the contents in a minpage or parbox. This works if I specify the width, but I need to set the width dynamically as mentioned above.
  • I've tried the suggestions here, but they don't seem to work with the tabulary environment.

1 Answer 1

1

enter image description here

\documentclass{article}
\usepackage{lipsum, multirow, tabulary, color, colortbl}
\newcommand\cc[1]{\cellcolor{yellow}#1}
\begin{document}


\begin{tabulary}{\linewidth}{|C|C|C|C|}
  \multicolumn{2}{p{
\dimexpr
\csname TY@F1\endcsname +
\csname TY@F2\endcsname +
2\tabcolsep +
\arrayrulewidth
\relax
}|}{\cc{\lipsum[1][1]}} &  
\multicolumn{2}{p{
\dimexpr
\csname TY@F3\endcsname +
\csname TY@F4\endcsname +
2\tabcolsep +
\arrayrulewidth
\relax
}|}{\cc{\lipsum[1][1]}} \\
  \cc{One} & \cc{Two} & \cc{Three} & \cc{Four}
\end{tabulary}

\end{document}
3
  • This seems to work for two column spans, but not three? As I span more columns, the white space on the right of the multcolumn cells gets bigger. (see edit above)
    – Joel
    Oct 2, 2019 at 18:20
  • @Joel you need 6\tabcolsep+3\arrayrulewidth in that case (as you are spanning over 3 vertical rules and 6 lots of \tabcolsep padding ) Oct 2, 2019 at 18:33
  • Ah! That worked like a charm. I'll remove my edit. Thanks!
    – Joel
    Oct 2, 2019 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .