# Linebreak in table cell at document margin

In order to allow line breaks within a table cell, I use \parbox. But when the cells I want to have line breaks in are in the last column of the table, the best thing would be to break them at the margin of the document. How can I specify the width of the \parbox that it does so automatically, without needing to specify it manually each time depending on the table I'm in?

\documentclass{article}
\newcommand{\breakcell}[1]{\parbox[t]{22em}{#1}}

\begin{document}
\hrule
\begin{tabular}{ll}
A short 1st column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A short 1st column & Blah blah\\
\end{tabular}

\begin{tabular}{ll}
A considerably longer first column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A considerably longer first column & Blah blah\\
\end{tabular}
\hrule
\end{document}


-
I see you mention tabularx in your answer (tabulary is another possibility) but why use \breakcell at all? Normally you would use a table preamble of lp{22em} for the table in the question. –  David Carlisle Mar 19 '13 at 18:53

With the tabularx package, it's possible to specify the width of the table as such, and then use X to tell it which column should be breakable across several lines. If the width of the table is set to \linewidth (or \textwidth), the last column will automatically break at the margin of the document. For this to work properly, however, it's necessary to either precede the \begin{tabularx} environment with a \noindent command, or to subtract the width of the indentation from the width of the table:

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\hrule
\noindent \begin{tabularx}{\linewidth}{lX}
A short 1st column & A considerably longer second column where I write way too much stuff\\
A short 1st column & Blah blah\\
\end{tabularx}

\newlength{\mylength}
\setlength{\mylength}{\linewidth}

\begin{tabularx}{\mylength}{lX}
A considerably longer first column & A considerably longer second column where I write way too much stuff\\
A considerably longer first column & Blah blah\\
\end{tabularx}
\hrule
\end{document}


-

Can you afford to use linegoal?

\documentclass{article}
\usepackage{linegoal}

\newcommand{\breakcell}[1]{\parbox[t]{\linegoal}{#1}}

\begin{document}
\hrule
\begin{tabular}{ll}
A short 1st column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A short 1st column & Blah blah\\
\end{tabular}

\begin{tabular}{ll}
A considerably longer first column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A considerably longer first column & Blah blah\\
\end{tabular}
\hrule
\end{document}


-
It's interesting to see that the text with your solution stretches somewhat further than it does with tabularx ... –  Sverre Mar 20 '13 at 15:36