2

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.

David Carlisle
  • 757,742
Fruppe
  • 23
  • 2
    \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 08 '18 at 17:36

2 Answers2

2

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

enter image description here

\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}
David Carlisle
  • 757,742
  • 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 08 '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 08 '18 at 18:34
  • 1
    So it is not possible to have 2 captions, one above and one below? – Fruppe Mar 08 '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 08 '18 at 18:48
  • Alright, that's what I noticed! Thanks for the help, I will look into those packages! – Fruppe Mar 08 '18 at 18:57
  • @Fruppe you can have any number of captions but by default the captions are all numbered, the caption package offers \caption* unnumbered caption but really it isn't often needed: You can just put a paragraph of text in the table, it is not clear what \caption offers if you do not need a number. – David Carlisle Mar 08 '18 at 19:08
  • Now I've ecountered another problem, table appear to be a floating element so it is now messed up in where they are placed. I looked at it here and have tried different positining arguments unable to get them to align properly. https://tex.stackexchange.com/questions/79639/why-is-my-table-appearing-before-my-text – Fruppe Mar 08 '18 at 19:09
  • 1
    @Fruppe the only function of a table environment is to allow floating (and traditionally the only reason to have a caption is so you can refer to the repositioned table) so this isn't messed up it is by design, But really this is one of the most frequently asked questions, there are multiple answers on site showing how to control table positions. – David Carlisle Mar 08 '18 at 19:11
1

\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

Strelok
  • 487