1

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}

enter image description here

  • 1
    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
2

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}
\addtolength{\mylength}{-\parindent}

    \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}

enter image description here

2

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}

enter image description here

  • 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

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.