3

I expect to break a long text automatically in tabular. After searching this site, there are several ways:

(i). Use p{dimension} in tabular, answered here

\begin{table}[h]
\begin{tabular}{|l|l|} 
\hline
key & \multicolumn{1}{p{4cm}|}{A very long text. A very long text. A very long text. A very long text}\\
\hline
\end{tabular}
\end{table}

As you can see, we need to assign dimension, not automatic.

(ii). Use the package tabularx, answered here

\begin{table}[h]
\begin{tabularx}{\textwidth}{|l|X|}
\hline
key & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
\hline
\end{tabularx}
\end{table}

(iii). Use the package longtable, answered here

\begin{longtable}{|l|p{4cm}|}
\hline
key & A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. A very long text. \\
\hline
\end{longtable}

Here is the result.

enter image description here

(iv). Other solutions?

  • 1
    longtable is irrelevant here, it's the p{4cm} column specification that enables line breaking (for all cells in that column), and that works with tabular and tabularx as well. – Torbjørn T. Jul 08 '15 at 16:24
  • VTC: "What is best..."-style questions typically tend to request opinion-based answers. Moreover, this question is too broad as it stands. – Werner Jul 08 '15 at 16:35
  • The reason there are no line breaks in your longtable example is that you have specified three columns, and the third one is a p column, but in the table there are only two columns, so the very long text ends up in an l column where line breaking does not happen. Remove one l| in the column specification. – Torbjørn T. Jul 08 '15 at 16:39
  • p{...} should work in any tabular - environment which is derived from array, as far as I know. In addition to Torbjørn T.'s comment use {|*{2}p{4cm}|} if you really need the | –  Jul 08 '15 at 16:40
  • @ChristianHupfer Did you mean \begin{longtable}{|l|p{4cm}|}? Yes, it works. – SparkAndShine Jul 08 '15 at 16:47
  • 2
    So there is no way of getting automatic line breaks in a column with dynamic width? I was kind of hoping that, with the hundreds of table-related packages around, I would not have to fiddle around with cm values manually and keep adjusting them every time the table content changes. What these comments suggest is that you can either have a flexibly sized column, or automatic line breaks, but not both; am I understanding this correctly? – Daniel Saner Jul 04 '18 at 08:39

0 Answers0