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?

closed as too broad by Werner, Maarten Dhondt, Jesse, user31729, user13907 Jul 8 '15 at 17:23

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 8 '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 8 '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 8 '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 | – user31729 Jul 8 '15 at 16:40
  • @ChristianHupfer Did you mean \begin{longtable}{|l|p{4cm}|}? Yes, it works. – SparkAndShine Jul 8 '15 at 16:47