# How can I only make table headers center and bold?

by SparkAndShine   Last Updated July 11, 2019 19:23 PM - source

I would like to center and bold the table header. Here are codes.

\begin{center}
\begin{table}
\caption{table decription}
\label{t_sim}
\begin{tabular}{|l|l|}
\hline
Key & Value \\
\hline
$x$ & description of x \\
$y$ & description of y \\
$y$ & description of z \\
\hline
\end{tabular}
\end{table}
\end{center}


The results is:

Problems need to be solved:

• center the whole table

• center the table header only (the column titles, the first row, key | value)

• bold the table header

• why is the gap between TABLE II and TABLE DISCRIPTION so huge?

Thanks @Christian Hupfer, here is my result. The gap between TABLE II and TABLE DISCRIPTION is still huge? BTW, I use IEEEtran.

\documentclass[conference]{IEEEtran}


Tags :

1. Use \centering inside the \begin{table}...\end{table} pair, this is safe, since it's inside a group and won't bleed into the following text...
2. Use \multicolumn{1}{|c|}{\textbf{Key}} for the first column and \multicolumn{1}{c|}{\textbf{Value}} for the 2nd column. Note that the 2nd is c| and not |c|, otherwise the | would be doubled.
3. \textbf{Key} etc. for bold font text

\documentclass{article}

\begin{document}

\begin{table}
\centering
\caption{table description}
\label{t_sim}
\begin{tabular}{|l|l|}
\hline
\multicolumn{1}{|c|}{\textbf{Key}} & \multicolumn{1}{c|}{\textbf{Value}} \\
\hline
$x$ & description of x \\
$y$ & description of y \\
$y$ & description of z \\
\hline
\end{tabular}
\end{table}

\end{document}


user31729
July 06, 2015 20:27 PM

The simplest way to do that is to use the makecell package, which is done for formatting column heads. In addition, for free, you have that, by default, the contents of column heads are centred, both vertically and horizontally, and they can have line breaks. Here are two ways, with and without vertical rules:

\documentclass{article}
\usepackage{array, booktabs, caption}

\usepackage{makecell}

\begin{document}

\begin{table}[!htbp]
\centering
\caption{table description}
\label{t_sim}
\begin{tabular}{|l|l|}
\hline
\hline
$x$ & description of x \\
$y$ & description of y \\
$y$ & description of z \\
\hline
\end{tabular}
\end{table}
\vskip1cm

\begin{table}[!htbp]
\centering
\caption{table description}
\label{t_sim}
\begin{tabular}{ll}
\toprule
\midrule
$x$ & description of x \\
$y$ & description of y \\
$y$ & description of z \\
\bottomrule
\end{tabular}
\end{table}

\end{document}


Bernard
July 06, 2015 20:41 PM

Here is a version using booktabs and makecell. The results are different with IEEEtran.cls, however. (See below: this is why a Minimum Working Example is so important.)

\centering is a better option than the center environment within floats (e.g. table, figure etc.) as it avoid adding excessive vertical spacing.

\documentclass{article}
\usepackage{array,booktabs,makecell}
\begin{document}
\begin{table}
\centering% will not add additional vertical space as center will - since table already adds spacing, you do not want extra here
\caption{table decription}
\label{t_sim}
\begin{tabular}{*{2}{l}}
\toprule% professional horizontal rules of variable weight from booktabs
\midrule% professional horizontal rules of variable weight from booktabs
$x$ & description of x \\
$y$ & description of y \\
$y$ & description of z \\
\bottomrule% professional horizontal rules of variable weight from booktabs
\end{tabular}
\end{table}
\end{document}


Switching the class to IEEEtran.cls produces this:

Here is the result shown with some dummy text to illustrate placement:

Here is the result with the conference option for the class:

If you are preparing a manuscript for submission, don't bother formatting things as you want as you will need to stick to the required style provided by the class etc. anyway. If you are not preparing for submission, you will find life easier if you use a more general-purpose class designed for greater flexibility.

cfr
July 06, 2015 21:02 PM