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?
Best Answer
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 thearray
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 usedK
as the column type to distinguish it from the C column type defined within thetabulary
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 thebooktabs
package, and its various rule commands which have much better spacing and width parameters.