Graphics, Figures & TablesCentering Cell content in fixed Width Columns

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Toty
Posts: 10
Joined: Fri Apr 13, 2012 1:40 am

Centering Cell content in fixed Width Columns

Post by Toty »

Hi, perhaps this is a basic question, but looking around in forums I can't find the solution for it .

In a table I defined the width of some of its columns. I'd like to align the content of these columns in the horizontal center. How can I do it ?

Please find an example of the code below. The columns 4 to 7 are aligned to the left, but I want them at the center.

Code: Select all

\documentclass[a4paper]{article} 
\usepackage{amsmath} 

\begin{document}

\begin{table}[H]
\centering
\begin{tabular}{lcccp{2.0cm}p{2.0cm}p{2.0cm}p{2.0cm}}
\hline\\
\centering
Col 1&Col 2&Col 3&Col 3&Col 4 XXX XX XXXX&Col 5 XXX XX XXXX&Col 6 XXX XX XXXXX&Col 7 XXX XX XXXX\tabularnewline
\hline
\centering
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
XX&XX&XX&XX&XX&XX&XX&XX\tabularnewline
\hline
\end{tabular}
\end{table}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Centering Cell content in fixed Width Columns

Post by Stefan Kottwitz »

Hi Toty,

you could use the array package for defining a centered column type:

Code: Select all

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
In your document just use that instead of p:

Code: Select all

\begin{tabular}{lcccC{2.0cm}C{2.0cm}C{2.0cm}C{2.0cm}}
Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Centering Cell content in fixed Width Columns

Post by localghost »

Thanks for the clear example which makes working out a solution very easy.

You can define a new column type by the array package which has a fixed width and centers the content.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{array,booktabs}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
  \begin{table}[!ht]
    \centering
    \begin{tabular}{lcccP{2.0cm}P{2.0cm}P{2.0cm}P{2.0cm}}\toprule
      Col 1 & Col 2 & Col 3 & Col 3 & Col 4 XXX XX XXXX & Col 5 XXX XX XXXX & Col 6 XXX XX XXXXX & Col 7 XXX XX XXXX \\\midrule
      XX & XX & XX & XX & XX & XX & XX & XX \\
      XX & XX & XX & XX & XX & XX & XX & XX \\
      XX & XX & XX & XX & XX & XX & XX & XX \\
      XX & XX & XX & XX & XX & XX & XX & XX \\
      XX & XX & XX & XX & XX & XX & XX & XX \\
      XX & XX & XX & XX & XX & XX & XX & XX \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}
The look of tables can be enhanced very nicely by the booktabs package. For details please have a look at the respective manuals of the involved packages.


Thorsten
Toty
Posts: 10
Joined: Fri Apr 13, 2012 1:40 am

Re: Centering Cell content in fixed Width Columns

Post by Toty »

Problem solved ! thanks a lot guys !
Post Reply