How can I put a new line (line break) within a table cell? I'm at a loss how to do this. I'm not using lyx or anything special, just MacTex and texmaker.
[Tex/LaTex] How to add a newline in a table cell
line-breakingtables
Related Solutions
Strangely, no answer (unless I've misread them) mentions a package that is dedicated to this precise question: makecell
, which allows for common formatting of certain cells, thanks to its \thead
and \makecell
commands, and for line breaks inside these cells. The horizontal and vertical alignments can chosen independently from those of the table they're included in. The default is cc
, but you can change it globally in the preamble with
\renewcommand{\cellalign}{vh}
\renewcommand{\theadalign}{vh}
where v is one of t,c,b
and h one of l,c,r
. Alternatively, for a single cell, you can use the \makecell
or \thead
commands with the optional argument [vh]
.
So here is a demo:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier}
\usepackage{array}
\usepackage{makecell}
\renewcommand\theadalign{bc}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{\Gape[4pt]}
\renewcommand\cellgape{\Gape[4pt]}
\begin{document}
\begin{center}
\begin{tabular}{ | c | c | c |}
\hline
\thead{A Head} & \thead{A Second \\ Head} & \thead{A Third \\ Head} \\
\hline
Some text & \makecell{Some really \\ longer text} & Text text text \\
\hline
\end{tabular}
\end{center}
\end{document}
You can use the m
option provided by the array package to vertically centre the material in table columns. However, you have another problem. Using \\
without a preceding ampersand creates a row with only one column. This is why part of the right edge of the table is missing. Instead, you could use \par
to insert a line break without moving to the next line of the table.
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|m{5.5cm}|m{12cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb+<< EOL+\par
\verb+foo+\par
\verb+bar+\par
\verb+baz+\par
\verb+EOL+
& Redirect a bunch of lines to the stdin. \\
\hline
\end{tabular}
\end{document}
Best Answer
The cell needs to be of a paragraph style: either via a
p{<len>}
column specification, or using a\parbox
.Using
\\
alone would not help, since this is redefined inside atabular
to define a new line. Of course, depending on the context, it may also be possible to usewhich resembles a newline in one of the cells.