[Tex/LaTex] Tabular rows that adjust height to wrap text

celltablestextwidth

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?

Best Answer

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