Tell me more ×
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 have table that has long headers and I would like to specify width of the column, but also be able to remain it's centred position.

Something like |c{0.30\textwidth}|.

It is possible? How to achieve this effect?

share|improve this question

2 Answers

up vote 15 down vote accepted

You could use the array package. It provides a way add a \centering command to a p-colum. Here's a tiny example:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{>{\centering\arraybackslash}p{1cm}}
first row \\
second row
\end{tabular}
\end{document}

The general syntax is >{command} before the actual column specifier. \centering changes the meaning of \\, thus \arraybackslash has been used to restore that. Have a look at the array documentation to learn more.

share|improve this answer
I would add \hspace{0pt} to the column definition in order to allow the first word in a cell to be hyphenated. – lockstep Nov 6 '10 at 20:34
@lockstep: Good addition, because it's a general issue with narrow columns like chosen in the demo example. In that example code, >{\centering\arraybackslash\hspace{0pt}} would indeed result in a hyphenation. – Stefan Kottwitz Nov 6 '10 at 20:43

In addition to Stefans answer: If you use centered columns with a fixed width quite often, you can define a new column type:

\usepackage{array}
\newcolumntype{x}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}

The "x" column takes its width as argument. In your document, you would write e.g.

\begin{tabular}{cx{3cm}c}

EDIT: Thanks to an answer from Ulrike Fischer on the German-speaking forum mrunix.de, I learned that there is still room for improvement: One might want to manually break lines within a cell of a "centered" p-column, but \newline doesn't work correctly in this case. Solution: assign the meaning of \\ (as defined by \centering!) to \newline within the cell:

\usepackage{array}
\newcolumntype{x}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
share|improve this answer

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.