1

I declared a macro like this:

\newcommand{\itemcell}[1]{%
    \begin{tabular}[t]{@{}l@{}}#1\end{tabular}
} 

Yet, I am now struggling with this, because it does not allow an automatic line wrapping anymore. See the example below :

\begin{longtable}{p{4cm}p{1.5cm}p{1.2cm}p{1.2cm}p{1.2cm}p{\dimexpr\columnwidth-9.1cm-12\tabcolsep\relax}}
\toprule
Title 1 & Title 2 & Title 3 & Title 4 & Title 5 & Title 6 \\
\midrule
a       & b       & c       & d       & e       & \itemcell{This is a very long description which won't fit in 1 line, it should break, but it won't\\And this is a second line} \\
\bottomrule
\end{longtable}

The content of the \itemcell{} won't be wrapped at the end of the column, overfull hbox incoming.

How can I arrange this macro to get that default behaviour back ?

  • your columns are p columns so just remove \itemcell it does nothing other than prevent line breaking. – David Carlisle Dec 12 '14 at 10:10
  • to force a newline in a p column use a paragraph break (if it is a new paragraph) or \linebreak or \newline if you just want to manually break a line. – David Carlisle Dec 12 '14 at 10:11
  • And I would really like to know where on this website \itemcell is recommended. A search return nothing. – Ulrike Fischer Dec 12 '14 at 10:11
  • You could use \itencell in a l column to allow line breaking, but it is missing a % at the end of the line in its definition. – David Carlisle Dec 12 '14 at 10:13
  • @UlrikeFischer : I renamed it, but here, among others : http://tex.stackexchange.com/questions/2441/how-to-add-a-forced-line-break-inside-a-table-cell –  Dec 12 '14 at 10:14
  • As I say above, use \newline or \linebreak (depending on whether you want the line to be cut short or be justified) just as in normal text outside of a table. – David Carlisle Dec 12 '14 at 10:14
  • The example you refer to is in a c column which does not allow multiple lines, so it is designed to locally over-ride that. – David Carlisle Dec 12 '14 at 10:15
  • Well you misunderstood the context, the purpose and the use of the command. In p-columns you don't need it, simply use \newline or \par(or an empty line) to get a paragraph break. And in other column types it can only be used for short bits. – Ulrike Fischer Dec 12 '14 at 10:18

1 Answers1

1

p columns allow line breaking so the macro is not needed at all, just use

\begin{longtable}{p{4cm}p{1.5cm}p{1.2cm}p{1.2cm}p{1.2cm}p{\dimexpr\columnwidth-9.1cm-12\tabcolsep\relax}}
\toprule
Title 1 & Title 2 & Title 3 & Title 4 & Title 5 & Title 6 \\
\midrule
a       & b       & c       & d       & e       &
This is a very long description which won't fit in 1 line, it should break, but it won't

And this is a second line\\
\bottomrule
\end{longtable}

in a c column you can locally use the nested tabular to allow multiple lines with manual line breaking, but your definition adds spurious white space as it is missing a %

\newcommand{\itemcell}[1]{%
    \begin{tabular}[t]{@{}l@{}}#1\end{tabular}%%%%
}
David Carlisle
  • 757,742