0

I am trying to add a title above a table, without success. Here the code:

    \begin{table}[b]
      \centering
      \begin{tabular}{|c|c|c|c|}
      \cline{2-4}
      \multicolumn{1}{c|}{}& &{CPU time}&\\
      \cline{2-4}
      \multicolumn{1}{c|}{} & Algorithm 1  & Algorithm 2  & Algorithm 3\\
      \hline
      $n=29$                & $0.001137$   & $0.000962$   & $0.000679$\\
      \hline
      $n=97$                & $0.020917$   & $0.008182$   & $0.005571$\\
      \hline
      $n=353$               & $0.699906$   & $0.067613$   & $0.054980$\\
      \hline
      $n=1345$              & $32.03852$   & $1.063334$   & $0.823744$\\
      \hline
    \end{tabular}
    \caption{Caption}
    \label{time}
    \end{table}

enter image description here

How to have a better setting of title? Thanks!

What I would like to obtain is this:

enter image description here

5
  • 2
    By default, the \caption just appears where you put it, so if you want it before the tabular rather than after, put it there. Jan 11, 2018 at 13:43
  • Thanks for your answer. However, by title I mean 'CPU time' and not the caption.
    – Mik
    Jan 11, 2018 at 13:45
  • @Mik What do you mean exactly? Now you put CPU time in a cell of the table.
    – Karlo
    Jan 11, 2018 at 13:50
  • 1
    What does it mean for you to "successfully" add the title? With your current code you have added the CPU time title, but presumably you don't like the way that it appears. It would help if you said how you wanted to add the title.
    – user30471
    Jan 11, 2018 at 13:50
  • Thanks for your suggest. Actually, I did explain what I would obtain. I am going to modify my question.
    – Mik
    Jan 11, 2018 at 13:59

2 Answers 2

2

An alternative layout without vertical lines and with aligned decimal separators could be

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage{booktabs,siunitx}
\begin{document}
\begin{table}[b]
\centering
\begin{tabular}{l S S S S }
\toprule
\multicolumn{1}{c}{} &     \multicolumn{3}{c}{\large CPU Time}          \\
                            & \multicolumn{1}{c}{Algorithm 1}  & \multicolumn{1}{c}{Algorithm 2}  & \multicolumn{1}{c}{Algorithm 3}\\
      \midrule
      $n=29$                & 0.001137   & 0.000962   & 0.000679 \\
      \midrule
      $n=97$                & 0.020917   & 0.008182   & 0.005571  \\
      \midrule
      $n=353$               & 0.699906   & 0.067613   & 0.054980  \\
      \midrule
      $n=1345$              & 32.03852   & 1.063334  & 0.823744  \\
\bottomrule
\end{tabular}
\caption{Caption\label{time}}
\end{table}
\end{document} 

using the siunitx and the booktabs packages

enter image description here

3

OK, you could do this

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage{booktabs}
\begin{document}
    \begin{table}[b]
      \centering
      \begin{tabular}{|c|c|c|c|}
      \cline{2-4}
      \multicolumn{1}{c}{}  & \multicolumn{3}{|c|}{CPU time}\\
      \cline{2-4}
      \multicolumn{1}{c|}{} & Algorithm 1  & Algorithm 2  & Algorithm 3\\
      \hline
      $n=29$                & $0.001137$   & $0.000962$   & $0.000679$\\
      \hline
      $n=97$                & $0.020917$   & $0.008182$   & $0.005571$\\
      \hline
      $n=353$               & $0.699906$   & $0.067613$   & $0.054980$\\
      \hline
      $n=1345$              & $32.03852$   & $1.063334$   & $0.823744$\\
      \hline
    \end{tabular}
    \caption{Caption}
    \label{time}
    \end{table}
\end{document} 

but the layout seems ugly. Avoid vertical lines in tables.

enter image description here

2
  • Also, you might consider aligning the decimal separators.
    – Denis
    Jan 11, 2018 at 14:20
  • Thanks for your suggest. How to do that? And how to have a more beautiful layout?
    – Mik
    Jan 11, 2018 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .