51

This question may look like a duplicate but, none of the answers given to the possible duplicates contain the answer to this specific case.

I have a tabular, say \begin{tabular}{ccc}. While adding a new line to a cell, I want both resulting lines to be horizontally centered. Is there an elegant solution to this? If not is there a solution to this?

nimcap
  • 5,135
  • Actually, (at the moment) answers in https://tex.stackexchange.com/q/2441/250119 and https://tex.stackexchange.com/q/12703/250119 works well. – user202729 Apr 11 '23 at 17:55

7 Answers7

98

How about using \shortstack inside a cell?

\documentclass{article}

\begin{document} 
\begin{tabular}{ccc}
    one & two & three \\
    one & two & \shortstack{a \\ bb \\ c}\\
\end{tabular}
\end{document}
Moriambar
  • 11,466
19

This is an old thread, but there is a new solution.

\usepackage{makecell}
%% tablestart
...
\makecell{line 1 \\ line2}
%% tableend
Timo
  • 191
10

If you want the cells to be centered horizontally as well as vertically I suggest the following solution:

\documentclass{article}
\usepackage{booktabs}
\newcommand{\bigcell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document} 

\begin{tabular}{ccc}
\toprule
a & \bigcell{c}{this schould be a longer line \\ this is a shorter one} & c \\ 
\midrule
 0,9892 & 0,9892 & 0,9892  \\
\end{tabular}

\end{document}
Moriambar
  • 11,466
Philipp
  • 3,815
  • All answers given work, but I prefer this one as it gives adequate spacing at the top of a table cell (instead of touching the cells top line). – Christopher Bull Jul 13 '14 at 11:11
  • Is there a way to decrease the vertical space between the lines in the cell? Using \renewcommand{\arraystretch}{0.8} inside the bigcell definition leads to a bad looking vertical alignment. – Yannic Mar 02 '21 at 18:32
4

Another way I discovered is to use matrix. Not as simple as Christian's answer but provides vertical alignment along columns.

\documentclass{article}
\usepackage{amsmath}

\begin{document} 
\begin{tabular}{ccc}
    one & two & three \\
    one & two & $\begin{matrix} \text{a} \\ \text{bb} \\ text{c} \end{matrix}$ \\
\end{tabular}
\end{document}
Moriambar
  • 11,466
nimcap
  • 5,135
2

An easy solution with tblr environment of the new LaTeX3 package tabularray:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{ccc} one & two & three \ one & two & {a \ bb \ c}\ \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932
1

You can also use \parbox like this:

\parbox[c][][c]{width}{\centering very long \\ line}

This has an advantage over \shortstack when also using \rowcolors to color table rows with alternating colors. \parbox results in a cell that has a single consistent color, and the next table row will have the alternative color. However, using \shortstack leads to two consecutive rows with the same color.

Dana
  • 111
  • A similar solution was proposed here: http://tex.stackexchange.com/questions/55860/any-way-to-center-text-within-a-parbox/55861#55861 – landroni Jun 13 '16 at 12:28
1

With {NiceTabular} of nicematrix.

\documentclass{article}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccc} one & two & three \ one & two & \Block[t]{}{a \ bb \ c}\ \end{NiceTabular}

\end{document}

Output of the above code

F. Pantigny
  • 40,250