table caption throws errors

I'm new to Latex and need to create some tables with captions. I looked at the examples available here: https://www.sharelatex.com/project/5aa15ef4ecc7bf1bc277d160 but when I try to use captions in my own code, the compiler throws the error LaTeX Error: \caption outside float. This is my code:

\usepackage{tabu}

\begin{center}
\begin{tabular}{|p{0.4cm}||p{4.3cm}|p{4.3cm}|p{4.3cm}|}
\hline
Id & Inclusion/Exclusion & Description & Motivation \\ [0.5ex]
\hline\hline
X & X. & XX \\ [1ex]
\hline
\end{tabular}
\caption{Table 1: The criteria.} % Error here!
\label{table:1}
\end{center}


Also if I try to reference the table, like this:

The table \ref{table:1} is an example of referenced


The table referenced is displayed as number 3 even though I only have one single table.

• \caption should be in a table enviornment. also avoid using numbers in the \label (or the text of the caption) latex will assign a number automatically. – David Carlisle Mar 8 '18 at 17:36

\caption and \label, as pointed out by @David Carlisle, should always be inside a tableenvironment. It is also a very good practise to write \caption and \label on the beginning at the top of the \table environment. A good discussion on this is provided here: Why should a table caption be placed above the table?

\documentclass[a4paper]{article}

\usepackage{tabu}

\begin{document}
\begin{table}
\caption{ The criteria.} % This should always come on top!
\label{table:1}
\begin{tabular}{|p{0.4cm}||p{4.3cm}|p{4.3cm}|p{4.3cm}|}
\hline
Id & Inclusion/Exclusion & Description & Motivation \\ [0.5ex]
\hline\hline
X  & X.                  & XX          &  Meh!      \\ [1ex]
\hline
\end{tabular}

\end{table}

This table is Table \ref{table:1}. It will remain Table \ref{table:1}. Why should it not be Table \ref{table:1}?

\end{document}


Please note your table was missing a cell, and that now it is properly formatted and working properly with \ref. Notice also that the \caption switched position (is now on the top of the table), since it was introduced earlier in the \table environment.

Also, be so kind as to provide a fully functional Minimum Workable Example so that we can help you - I've been there, so I understand that it might feel haunting at first!

Best,

Strelok

I recommend reading some latex tutorials on basic table and cross referencing, but:

\documentclass{article}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|p{0.4cm}||p{3.3cm}|p{3.3cm}|p{3.3cm}|}
\hline
Id & Inclusion/Exclusion & Description & Motivation \\ [0.5ex]
\hline\hline
X & X. & XX &\\ [1ex]
\hline
\end{tabular}
\caption{The criteria.}
\label{table:criteria}
\end{table}

zzzzz see Table~\ref{table:criteria}
\end{document}

• Aha, I see. Weird that the examples were wrong then? Is it possible to add a "top" caption at the same time? I tried it but then it woud reference the wrong table. – Fruppe Mar 8 '18 at 17:43
• @Fruppe most likely you misread the examples, the \caption just goes where you place it, top or bottom, the reference works in either case so long as you put the label in the correct place (after or better in the \caption argument) – David Carlisle Mar 8 '18 at 18:34
• So it is not possible to have 2 captions, one above and one below? – Fruppe Mar 8 '18 at 18:43
• @Fruppe, you can have two captions - however, you'll notice that this next caption will be numbered with what would be the next table in the document. The main idea is - one table, one caption. You can add footnotes to a table, however! \threeparttable packages is neatly made to ease up your effort to make a proper table with Caption / Body / Footnotes. Take a look at the CTAN documentation on \threeparttable In other words, to summarize: having two captions defeats the purpose of the notion of caption. – Strelok Mar 8 '18 at 18:48
• Alright, that's what I noticed! Thanks for the help, I will look into those packages! – Fruppe Mar 8 '18 at 18:57