Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 103 down vote accepted

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

share|improve this answer
    
Thanks, it works. In this way I don't have to add anything, just use "\\" and "\hline" as normally. –  Covi Mar 5 '11 at 12:24
4  
Nice answer! @Covi: You should probably accept lockstep's answer as the correct one, since it's more complete than mine –  Jake Mar 5 '11 at 12:29
    
@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 5 '11 at 12:30
    
@Jake: +1 for chivalry. –  lockstep Mar 5 '11 at 12:31
    
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 at 19:54

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} |}
share|improve this answer
3  
perhaps add the way using a new column type to your answer, if you like. Such as \newcolumntype{C}[1]{>{\centering}m{#1}} for using C{5cm} instead of m{5cm}. –  Stefan Kottwitz Mar 5 '11 at 10:46
    
@Stefan: Done! Thanks for the suggestion. –  Jake Mar 5 '11 at 10:51
    
& @Stefan: Thank you both! –  Covi Mar 5 '11 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.