0

There is a problem while I use newline in the tabular environment. Namely, from the following exemple in the column 3 the \newline adds a new line and it works as intended, however in the column 4 no new line is created for \newline command (please check the attached image below). Could you please tell why does it appear? By the way I am using array package.

\begin{center}
\begin{tabular}{|l|l|>{\raggedright\arraybackslash}p{5cm}|l|}
        \hline
        \rule[-1ex]{0pt}{2.5ex} ID & text & text & text \\
        \hline
        \rule[-1ex]{0pt}{2.5ex} 1 & text & text &  text1 \newline text2\\
        \hline
        \rule[-1ex]{0pt}{2.5ex} 2 & text & text &  \\
        \hline
        \rule[-1ex]{0pt}{2.5ex} 3 & text & text &  \\
        \hline
        \rule[-1ex]{0pt}{2.5ex} 4 & text& text &  \\
        \hline
        \rule[-1ex]{0pt}{2.5ex} 5 & text  & text1 \newline text2 & text1 \newline text 2\\
        \hline
    \end{tabular}
\end{center}

enter image description here

8
  • 1
    You define the fourth column as l type, so no line break is allowed.
    – egreg
    Commented Jun 11, 2022 at 9:32
  • Need p-type. Similar to tex.stackexchange.com/questions/254388/…
    – user202729
    Commented Jun 11, 2022 at 9:45
  • you could also use another tabular in the affecting cell: \begin{tabular}[t]{@{} l @{}} text 1\\text 22 \end{tabular} if you want to stick to the l column type.
    – Celdor
    Commented Jun 11, 2022 at 9:57
  • @user202729, what does l and p mean? It seems that I have to add |>{\raggedright\arraybackslash}p{5cm}| as in the column 3, otherwise just |p| gives an error. Commented Jun 11, 2022 at 9:59
  • 1
    You can load the makecell package and use \makecell[l]{text1 \\ text2} for standard column types.
    – Bernard
    Commented Jun 11, 2022 at 10:01

1 Answer 1

1

As already mentioned in comments below question:

  • Text in a cell can only be broken in so-called "paragraph" cells, i.e. in cells in columns of type p{<width>}, m{<width>} or b{<width>}, which are defines an array package or in the X type defined by the tabularx package. -In these cells text longer then cells width is broken automatically, however is possible force to break it by \newline or \par or with inserting empty line between paragraphs in cells.
  • Cells in columns of type c, l and r text cannot be broken. In those cells their width is adopted to width of contained text.

Some remarks about how to write your table:

  • use of \rule for horizontal lines is wrong. For those lines are defined \hline and cline.
  • beside them are exist plenty of other lines defined packages as booktabs, hhline, etc.
  • new packages for writing tables -- nicematrix and tabularray enables to define all table lines in theirs preambles.

An example of table written by use of tabularray package:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{center}
\begin{tblr}{hlines, vlines,
             colspec = {c l Q[l, wd=50mm] Q[l, wd=3em]},
             row{1} = {font=\bfseries, c}
             }
ID  & text  & text  &   text                \\
1   & text  & text  &   text1 text2 \\
2   & text  & text  &                       \\
3   & text  & text\newline text2   
                    &                       \\
4   & text  & text\par text2   
                    &                       \\
5   & text  & text1

              text2 & text1 \newline text 2 \\
    \end{tblr}
\end{center}
\end{document}

which produce:

enter image description here

For tabularray syntax please consult package documentation, where it is thoroughly and concise explained.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .