How can I only make table headers center and bold?

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

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:

enter image description here

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}

enter image description here



Answers 3


  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}

enter image description here

user31729
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}
\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{table}[!htbp]
  \centering
  \caption{table description}
  \label{t_sim}
  \begin{tabular}{|l|l|}
    \hline
    \thead{Key} & \thead{Value} \\
    \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
    \thead{Key} & \thead{Value} \\
    \midrule
    $x$ & description of x \\
    $y$ & description of y \\
    $y$ & description of z \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document} 

enter image description here

Bernard
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}
\renewcommand\theadfont{\bfseries}% bold tabular headers
\renewcommand\theadalign{cc}% centred tabular headers
\renewcommand\theadgape{}% booktabs rules already add vertical spacing
\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
    \thead{Key} & \thead{Value}\\
    \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}

fancier tabular

Switching the class to IEEEtran.cls produces this:

IEEEtran version

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

IEEEtran placement

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

conference variation

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
cfr
July 06, 2015 21:02 PM

Related Questions


Updated February 23, 2019 10:23 AM

Updated February 23, 2019 11:23 AM

Updated August 27, 2019 19:23 PM

Updated May 27, 2017 08:23 AM

Updated August 05, 2015 13:10 PM