Graphics, Figures & Tablescentering table headers

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tchari
Posts: 1
Joined: Wed Feb 25, 2009 9:42 pm

centering table headers

Post by tchari »

Hello everyone,

I'm pretty new to LaTeX and I've been trying to figure out a way to create a table where my table headers are centered but the rest of the table is not. I tried using the {\centering Blarg} commands, but they didn't work. Here is a minimal working example:

Code: Select all

\documentclass [12pt,letterpaper]{report}

\begin {document}

\begin{table}
	\begin{center}
		\begin{tabular}{|p{45mm} p{45mm} p{45mm}|}
			\hline
				\begingroup\centering Header 1\endgroup & \begingroup\centering Header 2\endgroup & \begingroup\centering Header 3\endgroup \\
			Some random text here to fill the box & Some random text here to fill the box & Some random text here to fill the box \\
			\hline
		\end{tabular}
	\end{center}
\end{table}

\end{document}
As you can see from my code above, I then tried the \begingroup...\endgroup but that didn't work either. In fact the only thing I found that did work was to embed centering environments around each header. That is:

Code: Select all

\begin{center}
Blarg
\end{center}
However, as many other forum posts have informed me, this leaves some vertical white space around the headers. Is there a way to center only the headers without using the center environment? Or is there a way to limit the white space from the center environment?

Thanks!
-Tarun

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
daleif
Posts: 199
Joined: Wed Nov 19, 2008 12:46 am

centering table headers

Post by daleif »

use \multicolumn{1}{c}{...} around your headers
BTW: each cell is a group of its own so the \begin/endgroup are not needed. You may also want to check the array class, which can exmplan how one might center the contents of a p{...}

Code: Select all

\documentclass [12pt,letterpaper]{report}
\begin {document}

\begin{table}
   \begin{center}
      \begin{tabular}{|p{45mm} p{45mm} p{45mm}|}
         \hline
            \multicolumn{1}{|c}{Header 1} & \multicolumn{1}{c}{Header 2} & \multicolumn{1}{c|}{Header 3} \\
         Some random text here to fill the box & Some random text here
         to fill the box 
         & Some random text here to fill the box \\
         \hline
      \end{tabular}
   \end{center}
\end{table}

\end{document}
Mabolaya
Posts: 4
Joined: Fri Apr 20, 2012 9:44 am

Re: centering table headers

Post by Mabolaya »

Thanks, much appreciated!
Post Reply