Graphics, Figures & TablesCentering the Cell Content in the Table Header

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
marie2011
Posts: 138
Joined: Mon Feb 06, 2012 4:58 pm

Centering the Cell Content in the Table Header

Post by marie2011 »

Dear forum members,

I was wondering how I can center just specific contents of a heading in a table. I would like to center the word "University" and "Bern".

I have a minimal example:

Code: Select all

\documentclass[
12pt,%
%  draft,%
  twoside,%
  BCOR10mm,%
  bib=totoc,        
toc=listof,
toc=bibliography,
numbers=noenddot
]{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,ngerman,catalan,english]{babel}
\usepackage{natbib}
\usepackage{tabularx}

\begin{document}
\begin{table}[!ht]
\caption{Example}
\centering
\begin{tabular}{lcclcl}
\toprule
\textbf{Name} &  \textbf{Surname} &     \textbf{Age} &
\textbf{University}& \textbf{Level of Proficiency} \\
\midrule 
Helen & Dooley& 20 & Bern & C2\\ 
\hline
\bottomrule
\end{tabular}
\label{tab:infogeneralemden}
\end{table}
\end{document}
Many thanks in advance.

Regards,

Marie

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Centering the Cell Content in the Table Header

Post by localghost »

Please check your examples for functionality and minimal content before you add them to a question. In your example here at least the booktabs package is missing to get the table. And the packages natbib and tabularx are not needed here to reproduce the problem.

You want nothing else then centering the content of a whole column. So you did a small mistake in the table preamble. You declare six columns but you use only five. Furthermore there is an l column type where you do not want it. Your table preamble should read like this.

Code: Select all

\begin{tabular}{l*{4}{c}}
% table content
\end{tabular}
This will center all columns except for the first one. I don't know if that is what you want but you should be able to correct this if necessary.

Since you want your table header to be in bold type face, it is suggestive to incorporate some code from the UK TeX FAQ (see code below). It requires to load the array package for the declaration of some new column types.

Code: Select all

\documentclass[
  fontsize=12pt,%
  twoside,%
  BCOR=10mm,%
  bibliography=totoc,
  captions=tableabove,
  listof=totoc,
  numbers=noenddot
]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish,ngerman,catalan,english]{babel}
\usepackage{array,booktabs}

%% Some code from the UK TeX FAQ to influence the style of a complete table row
%% see: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=wholerow
\newcolumntype{_}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{%
  \gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\begin{document}
  \begin{table}[!ht]
    \caption{Example}
    \label{tab:infogeneralemden}
    \centering
    \begin{tabular}{_l*{4}{^c}} \toprule
      \rowstyle{\bfseries}
      Name & Surname & Age & University & Level of Proficiency \\ \midrule
      Helen & Dooley& 20 & Bern & C2 \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}

Thorsten
marie2011
Posts: 138
Joined: Mon Feb 06, 2012 4:58 pm

Re: Centering the Cell Content in the Table Header

Post by marie2011 »

Hallo Thorsten,

Many thanks for your prompt answer. I am going to try what you suggested.

Regards,

Marie
Post Reply