25

I like it when tables have centered columns (e.g. |c|c|c|).

However, I have width restrictions. The most obvious solution to force a table to be within a certain width is to do |p{2.5cm}|p{2.5cm}|, etc. However, this puts the text off to one side instead of centered.

Ideally, I'd like to say "my table will be a total of 10cm wide" and "the columns will be centered." Is there a way to do this?

Next best thing, can I make a table where I specify each column's width, and the columns are centered?

4 Answers 4

13

There are two ways you can achieve what you want. The first is as in Maarten's answer, which requires you to specify the width of each column. The use of \arraybackslash is needed in case the centred column is the last column in the table. See the array package documentation for more information on this or Difference between \\ and \tabularnewline.

A second way is to use the tabulary package, which allows you to define columns that will fill up the maximum width of the table. Here's an example of both. I've used K as the column type to distinguish it from the C column type defined within the tabulary environment.

Please note that I've used \hline in the example code just to show the width of the tables. For most tables I would highly recommend the booktabs package, and its various rule commands which have much better spacing and width parameters.

\documentclass{article}
\usepackage{array}
\usepackage{tabulary}
\newcolumntype{K}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
table with centred columns of specified width
\bigskip

\begin{tabular}{K{2cm}K{2cm}}
\hline
foo & bar \\
foobar & barfoo \\
\hline
\end{tabular}
\bigskip

table of 5cm width, centred columns
\bigskip

\begin{tabulary}{5cm}{CC}
\hline
foo & bar \\
foobar & barfoo\\
A much wider column  & c \\
\hline
\end{tabulary}
\bigskip

as we make the total width wider, the columns adjust
\bigskip

\begin{tabulary}{8cm}{CC}
\hline
foo & bar \\
foobar & barfoo\\
A much wider column  & c \\
\hline
\end{tabulary}
\end{document}

output of code

1
  • I ended up using tabulary -- it works great. The other responses look good, too. Apr 13, 2015 at 19:54
9

When I posted this, I did not realise that Mico had posted an answer using tabularx 3 minutes earlier than I hit the submit button. So credit for the use of tabularx should go to Mico.

I am leaving this answer, with Mico's approval, because it also demonstrates the advantages of using booktabs to create nicer horizontal rules and dispensing with vertical rules. This enables you to produce professional quality tables. For more information, take a look at the booktabs manual which provides lots of advice about presenting information effectively in tabulars. (The manual is also available in French and German.)

For completeness, here is a solution which combines tabularx with booktabs.

As Mico explains, tabularx allows you to specify the total width of the table. At least one of your columns must then be X which expands to fill the width. In this case, I use array to define a centred version of this column type, C and specify 3 such columns for the table.

booktabs provides enhanced spacing and additional commands for drawing horizontal rules. In a professional quality table, the top and bottom rules should be heavier than rules within the table. Consequently, booktabs provides \toprule, \midrule and \bottomrule as demonstrated below. For additional possibilities, including \cmidrule and rule trimming, see the excellent package documentation.

\documentclass{article}
\usepackage{array,booktabs,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
  \begin{tabularx}{\linewidth}{*{3}{C}}
    \toprule
    Header 1 & Header 2 & Header 3\\\midrule
    This is some text which will be longer than the width of the column. & This is some more text which will be longer than the width of this second column. & And here is some more text for the third column.\\\bottomrule
  \end{tabularx}
\end{document}

set width tabular

1
  • @Mico Missed that. Should I delete this? I'm only tempted to leave it because I used booktabs. If you don't want me to delete, I'll add a note at the top pointing to your answer as the first one. Otherwise, I'd be happy to delete if you wouldn't mind mentioning booktabs?
    – cfr
    Apr 11, 2015 at 14:47
6

You wrote,

Ideally, I'd like to say "my table will be a total of 10cm wide" and "the columns will be centered." Is there a way to do this?

Answer: Yes -- with the help of the tabularx package, which provides the tabularx environment that lets you specify the overall width of the tabular-like structure, and the array package, which lets you modify tabularx's X column-type to typeset the contents centered rather than fully justified. (I'm assuming here that you want all columns to be equally wide.)

enter image description here

\documentclass{article}
\usepackage{tabularx} % also loads 'array' package
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of 'X' columns
\begin{document}

\begin{tabularx}{10cm}{|C|C|C|}
\hline
some text & more text & a huge amount of additional text\\
\hline
\end{tabularx}
\end{document}

Note that if (i) the total width of the tabularx is given by totalwidth, (ii) there are n columns, and (iii) each column is delimited by vertical bars (hence, n+1 vertical bars in total), the usable width of each column is equal to

(totalwidth-2*n*\tabcolsep-(n+1)*\arrayrulewidth)/n
2

With the array package you can specify new column types:

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

MWE:

\documentclass{article}
\usepackage{array}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{tabular}{|C{1cm}|C{3cm}|C{1cm}|}
    \dots & \dots & \dots
\end{tabular}

\end{document}
1
  • Or simply change |p{2.5cm}| by |>{\centering}p{2.5cm}|
    – Fran
    Apr 11, 2015 at 2:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .