0

I'm trying to make a large table where each of the boxes has several lines of text underneath each other. I have tried \newline and \linebreak to get onto a new line, but neither work. The command \\ does give a new line, but it ends the entire line in the whole table.

Can anyone suggest how I might get a new line, or perhaps a better package to use?

  • 1
    There are some suggestions in http://tex.stackexchange.com/questions/2441/how-to-add-a-forced-line-break-inside-a-table-cell and http://tex.stackexchange.com/questions/485/how-to-break-a-line-in-a-table – Torbjørn T. Mar 15 '14 at 13:53
  • Please always post a complete, Minimal (non-)Working Example of code which people can use to reproduce the issue. This makes your question a lot clearer, as well as making it easier for people to help you. – cfr Mar 15 '14 at 23:22

3 Answers3

1

Not mentioned in the previous links, the makecell package defines \makecell, thead, diaghead and \multirowcell commands that accept the use of \\and can be further customised (choice of font, rotation, vertical spaces, &c.). One can change the hlines and clines thickness.

See this thread to have an example of its use.

Bernard
  • 271,350
1

Use parbox{}{} as in:

\documentclass{standalone}

\begin{document}

    \begin{tabular}{ll}
        \hline
        row 1, column 1, line 1 & \parbox{5cm}{row 1, column 2, line 1 \\ row 1, column 2, line 2 \\ row 1, column 2, line 3} \\ \hline
        \parbox{5cm}{row 2, column 1, line 1 \\ row 2, column 1, line 2} & row 2, column 2, line 1 \\ 
        \hline
    \end{tabular}

\end{document}

enter image description here

1

There are several possible solutions. The following demonstrates 2 approaches. The first relies on only standard commands. Although I've used booktabs for aesthetic reasons, this is not essential to the solution. The second uses tabularx.

2 ways to get <code>\newline</code> in tabulars

\documentclass{article}
\usepackage{tabularx,booktabs}
\begin{document}

One method involves using the standard \verb|p{<width>}| column specifier. This requires knowing how wide you want the columns but it allows you to use \verb|\newline| and does not require additional packages. Table \ref{tab:standard} does use commands from \verb|booktabs| to improve the tabular's appearance but you could replace with \verb|\hline| etc.\ if preferred.
\begin{table}
\centering
\begin{tabular}{*{2}{p{.285\linewidth}}}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabular}
\caption{Tabular with standard commands}\label{tab:standard}
\end{table}
\bigskip

If you don't know how wide the columns should be and don't wish to figure it out, but you can specify the overall width of the tabular, \verb|tabularx| can be used. This supports the \verb|X| column specifier which figures out the width based on the overall tabular width. It also allows \verb|\newline|. Table \ref{tab:tabularx} again uses \verb|booktabs| but that is for merely aesthetic reasons.
\begin{table}
\centering
\begin{tabularx}{.65\linewidth}{XX}
    \toprule
    row 1, column 1, line 1 & row 1, column 2, line 1\newline row 1, column 2, line 2\newline row 1, column 2, line 3\\\midrule
    row 2, column 1, line 1\newline row 2, column 1, line 2 & row 2, column 2, line 1 \\
    \bottomrule
\end{tabularx}
\caption{Tabular with tabularx}\label{tab:tabularx}
\end{table}

\end{document}
cfr
  • 198,882