1

I have a table like this:

\begin{tabular}{|l|l|}
  \hline
  \multicolumn{2}{|l|}{\textbf{Item Name}} \\
  \hline
  Item Type & Item Subtype \\
  \hline
  \multicolumn{2}{|l|}{Item effect (long text).} \\
  \hline
  \multicolumn{2}{|l|}{Item description (long text).} \\
  \hline
\end{tabular}

That produces this:

|-------------------------------|
| Item Name                     |
|-------------------------------|
| Item Type | Item Subtype      |
|-------------------------------|
| Item effect (long text).      |
|-------------------------------|
| Item description (long text). |
|-------------------------------|

The table will always be as wide as the longest row. In my case, the bottom two rows will often be very long, sometimes multiple sentences. But because cells do not wrap text and a row cannot change its height to accommodate that, the table will grow so wide that it leads off the right side of the page. How can I make the bottom two rows automatically wrap their text and expand the row height to match, without altering the row height of the top two rows?

  • Use tabularx and set at least one column type to x. – Bernard May 1 '19 at 23:11
  • you should use colums type which cell enable to break long text into multi line text. for example p{<column width>} or use tabularx table environment and its X column type. – Zarko May 1 '19 at 23:12
2

Here is how to do it with tabularx:

\documentclass{article}

\usepackage{tabularx, lipsum}
\usepackage[showframe]{geometry} 
\setlength{\extrarowheight}{2pt}

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{|l|X|}
  \hline
  \multicolumn{2}{|l|}{\textbf{Item Name}} \\
  \hline
  Item Type & Item Subtype \\
  \hline
  \multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep -2\arrayrulewidth}|}{\lipsum[11]} \\
  \hline
  \multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Item description (long text).} \\
  \hline
\end{tabularx}

\end{document} 

enter image description here

|improve this answer|||||
  • This works great, thank you! Would you mind explaining why you used {|p{\dimexpr\linewidth-2\tabcolsep -2\arrayrulewidth}|} in the third column but {|p{\dimexpr\hsize-2\tabcolsep-2\arrayrulewidth}|} in the fourth? – klasdfjasdf May 2 '19 at 3:31
  • I certainly don't mind. Initially; I wrote \hsize, which is plain TeX, then I decided that the LaTeX length \linewidth would be less ambiguous, but forgot to change in the second \multicolumn. It's fixed now. Thanks for pointing the inconsistency! – Bernard May 2 '19 at 8:45

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.