311

I would like to create a table with some columns' width specified, while the text in those columns should be centered both horizontally and vertically. I found out that

\usepackage{array}
\begin{tabular}{| c | c | m{5cm} |}

vertically centers the text in the last column, but justifies it horizontally (like text in normal paragraphs).

Update: With Jake's method, it seems both \\ and \hline cause an error. How to fix it?

lockstep
  • 250,273
Covi
  • 7,055

4 Answers4

477

A comprehensive solution (based on this answer) is to define new column types (say, L, C, and R) that take their width as argument and do the following:

  • Issue \raggedright, \centering, or \raggedleft to achieve the desired horizontal alignment,

  • Declare \let\newline\\ to allow to use \newline for manual line breaks within a cell (note that \centering & friends change the meaning of \\ -- this is the problem with Jake's solution),

  • Issue \arraybackslash (i.e., \let\\\tabularnewline) to allow (again) to use \\ for ending rows,

  • Issue \hspace{0pt} to allow the first word in a cell to be hyphenated.

In the example below, the new column types are based on (vertically centered) m-columns, but one may use (top- or bottom-aligned) p- or b-columns as well.

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{tabular}{| c | L{3cm} | C{3cm} | R{3cm} |}
foo &
A cell with text that wraps around, is raggedright and allows \newline
    manual line breaks &
A cell with text that wraps around, is centered and allows \newline
    manual line breaks &
A cell with text that wraps around, is raggedleft and allows \newline
    manual line breaks \\
\end{tabular}

\end{document}

enter image description here

David Carlisle
  • 757,742
lockstep
  • 250,273
  • Thanks, it works. In this way I don't have to add anything, just use "\" and "\hline" as normally. – Covi Mar 05 '11 at 12:24
  • @Covi: As @Jakes's answer seems to produce errors which are corrected in my answer, please consider to accept my answer instead. ("Accepted" answers are displayed at the top, and the next user facing your problem will perhaps only read the accepted answer.) – lockstep Mar 05 '11 at 12:30
  • @Jake: +1 for chivalry. – lockstep Mar 05 '11 at 12:31
  • 4
    For anybody's interest: There is an (error free) conflict with the pdfsync package, which corrupts the spacing. I took me a while to find this problem... – matheburg Mar 14 '14 at 19:54
  • 2
    One small problem with this solution is that columns using p get vertically centered when there are longer L columns. – Georg Schölly Mar 10 '16 at 17:57
  • See http://tex.stackexchange.com/a/257166/9075 for more information on the \newcolumtype command – koppor Jul 09 '16 at 23:09
  • 6
    Please note that LCRJ column types are defined by the tabulary package. Just in case someone uses this excellent hint together with tabulary and wonders why some other tables break. – koppor Jul 09 '16 at 23:26
  • Is there a solution when your journal template is incompatible with the array package? Yes, incompatible with the array package. – cangrejo Sep 25 '17 at 11:25
  • @broncoAbierto Fix the template? More seriously though, post a new question about that if you haven't figured it out. – Torbjørn T. Oct 24 '17 at 11:33
  • Any way to escape the column header from the alignment? Like if I have numerals in the columns that I need right aligned, but the header should be centered. Thanks – tallharish Apr 16 '18 at 18:50
  • 2
    for the other noobs reading this: you HAVE TO specify a width for each column of this new type. Otherwise, you get the usual 27 Latex errors that say absolutely nothing ;) – Jakob Apr 26 '18 at 09:42
  • Does hspace in the R column type do anything? (if it does, there must be something else interfering in my setup, as it does nothing for me) – PatrickT May 05 '18 at 10:34
  • How to add multiple rows to the table such that each cell is bordered by lines? Currently, only the columns are bordered by lines. – Hans Aug 15 '18 at 23:12
  • The solution appears incompatible with the colortbl package while in the beamer document class, producing an error. – tvk Apr 18 '19 at 14:25
  • If some cell consists of one word that is wider than the fixed column, then that word is shifted down by half a line. How to prevent this? https://tex.stackexchange.com/q/498203/ – root Jul 01 '19 at 10:02
  • Can we also reduce space in between columns ? – alper Aug 13 '22 at 13:13
  • In LuaLaTeX the line break will always work. The problem only happen with other engines (try a code without the patch \begin{tabular}{>{\centering\arraybackslash}p{1.5cm}} particular particular \end{tabular}) – user202729 Apr 11 '23 at 14:44
77

The 'm' column type only centers the text vertically; to get it also centered horizontally, you can use the >{cmd} syntax, which adds a command before each cell in the specified column:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{| c | c | >{\centering}m{5cm} |}
Abc & Bcd & A long cell with text that wraps around and is centered
\end{tabular}
\end{document}

tabular with column that is horizontally and vertically centered

As Stefan Kottwitz pointed out in his comment, this can also be be wrapped in a new column type that you can define using

\newcolumntype{C}[1]{>{\centering}m{#1}}

Then you can define the above table using

begin{tabular}{| c | c | C{5cm} |}
Jake
  • 232,450
13

It’s easy peasy to specify the horizontal and vertical alignment of table cells at the same time with tblr environment of the new LaTeX3 package tabularray:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{|Q[l,t,3cm]|Q[c,m,3cm]|Q[r,b,3cm]|}
\hline
 {Top Baseline \\ Left Left} & Middle Center & {Right Right \\ Bottom Baseline} \\
\hline
\end{tblr}
\end{document}

enter image description here

You can even remove the width settings of the columns to use the natural widths.

L.J.R.
  • 10,932
  • What is the package for Ubuntu 20.04 LTS to install tabularray? – problemofficer - n.f. Monica Dec 03 '21 at 09:10
  • Download tabularray.sty and ninecolors.sty from CTAN: https://ctan.org/tex-archive/macros/latex/contrib/tabularray and https://ctan.org/tex-archive/macros/latex/contrib/ninecolors – L.J.R. Dec 05 '21 at 02:44
  • 2
    I just came here to say this is BY FAR the best solution, and table package, I've ever seen. LaTeX tables have given me so much grief over the years, and this simple package is delightful. Easy peasy, indeed. Thanks!!! – shiri Aug 10 '23 at 21:26
12

I found a very different technique that also works:

With the package ragged2e you can issue the command \RaggedRight outside the tabular environment and get ragged right inside p- or m-columns of a tabular environment.

Here is an example document:

\documentclass{article}
\usepackage{array}
\usepackage{ragged2e}
\begin{document}
{\RaggedRight
    \begin{tabular}{| c | m{3cm}}
    foo &
A cell with text that wraps around, is raggedright and allows \newline
manual line breaks \\ 
\end{tabular}
}% end of \RaggedRight
\end{document}

Eample created with <code>ragged2e</code>