Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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 Answers 2

up vote 2 down vote accepted

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

share|improve this answer

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.