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

1 Answers1

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 &amp; 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.

Zarko
  • 296,517