4

I am trying to produce a caption to describe a table in LaTeX, however am getting an error: ! LaTeX Error: \caption outside float. The formulation of the table is:

\begin{center}
 \begin{tabular}{c| c c c} 
 Campus & Main & Jubilee & Kings Meadow \\ [0.5ex] 
 \hline
 Main         & 0 & 21.8 & 24.6 \\
 Jubilee      & 21.8 & 0 & 9.3 \\
 Kings Meadow & 24.6 & 9.3 & 0 \\
\end{tabular}
\caption{Table Description}
\end{center}

Any help would be greatly appreciated. Many thanks in advance

sam
  • 85

1 Answers1

3

Principially you have two possibilities to add captions to a table (same works for figures too with figure instead table):

  1. You can use a non floating table (that means you do not use environment table, which makes the table floating) as shown in the first example of the following MWE, nearly the same you used in your code. Then you need to use command \captionof{table}{} to get a table caption for your table outside a float (you see the relevance to the error message you got?).
  2. You can use a floating table with environment table. That means LaTeX is allowed to place the table inside the document at a place at its own. Usually that means the table is placed at the top of the page (as done in my MWE) or at the top of the next page ... Then you need to use command \caption{} to get a table caption ...

Please see the following MWE

\documentclass{article}

\usepackage{caption} % <================================================
\usepackage{booktabs} % <================================= better tables
\usepackage{blindtext} % <======================= to generate dummy text 


\begin{document}

\begin{minipage}[c]{\linewidth}
\centering % <====================================== to center the table
\begin{tabular}{c| c c c} 
Campus & Main & Jubilee & Kings Meadow \\ [0.5ex] 
\hline
Main         & 0 & 21.8 & 24.6 \\
Jubilee      & 21.8 & 0 & 9.3 \\
Kings Meadow & 24.6 & 9.3 & 0 \\
\end{tabular}
\captionof{table}{Table Description} % <================== from package `caption`
\label{tab:campus} % <============================== to be referenacable
\end{minipage}

\blindtext

\begin{table}%[ht] % <====================================== let table float!
\centering
\begin{tabular}{c c c c} 
\toprule % <====================================== from package booktabs
Campus       & Main & Jubilee & Kings Meadow \\ [0.5ex] 
\midrule % <====================================== from package booktabs
Main         & 0    & 21.8    & 24.6 \\
Jubilee      & 21.8 & 0       & 9.3 \\
Kings Meadow & 24.6 & 9.3     & 0 \\
\bottomrule % <=================================== from package booktabs
\end{tabular}
\caption{Table Description a}
\label{tab:campusa}
\end{table}

Now we can reference table~\ref{tab:campus} and table~\ref{tab:campusa}.

\end{document}

and its result:

resulting pdf

As you can see table two is floating at the top of the page, has a caption and is referencable as you can see in the last sentence. You can also see that I used package booktabs for better horizontal lines and I deleted the vertical line (try to do not use them!) in the second table.

If you do not want the table to float at the top of the same page you can try to use option [ht] for table environment:

 \begin{table}[ht] % <====================================== let table float!

If you remove the % before [ht] in the MWE above you get the changed result:

resulting pdf 2

BTW: Have you considered to write the numberes aligned to the dezimal point? Numbers in tables looks better then IMHO. See package dcolumn or siunitx ...

Mensch
  • 65,388