10

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

3
  • Center the table header? You mean the column titles should be centered?
    – user31729
    Jul 6, 2015 at 20:25
  • yes, the column titles (the first row, key value). Jul 6, 2015 at 20:27
  • 2
    I don't get that gap with \documentclass[conference]{IEEEtran}. Output is shown in my answer below for article, IEEEtran and IEEEtran with option conference.
    – cfr
    Jul 6, 2015 at 21:15

3 Answers 3

23

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

4
  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

8
  • Awesome. One more question, why is the gap between TABLE II and TABLE DISCRIPTION so huge? Jul 6, 2015 at 20:31
  • No comments on the vertical lines!
    – user31729
    Jul 6, 2015 at 20:31
  • @SparkandShine: In my output it's not really large. Do you have other spacings?
    – user31729
    Jul 6, 2015 at 20:34
  • it might be my problem. All the caption has the same problem. Thanks again. Jul 6, 2015 at 20:40
  • 2
    Don't use \begin{center}...\end{center} in a float; just \centering.
    – egreg
    Jul 6, 2015 at 20:56
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.