8

I'm formatting the listings captions as follows:

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

However, I realized that just writting \usepackage{caption} breaks references to tables, causing warnings like:

LaTeX Warning: Reference `table:swtp' on page 13 undefined on input line 44.

and LaTeX writes "Table. ??" in the generated document.

This an example that gives me the problem:

\documentclass[10pt,letterpaper]{report}
\usepackage{caption}
\begin{document}

\chapter{Chapter}

See the Table ~\ref{table:example}

\begin{table}[htdp] 
\begin{center}
    \begin{tabular}{ | c | c |}
    \hline
    A & B  \\ \hline
    1 & 2 \\ 
    \hline
    \end{tabular}
    \caption{my cap}
\end{center}
  \label{table:example}
\end{table}

\end{document}
3
  • 1
    Please provide a complete MWE that shows your problem. Do you compile twice? Apr 29, 2012 at 16:49
  • 2
    If you don't know what a MWE is you can read it here: minimal working example (MWE) Apr 29, 2012 at 17:20
  • 1
    You forgot to mention the warning Package caption Warning: \label without proper \caption on input line 19. See the caption package documentation for explanation. your example causes. When taking a look at the caption package doc you'll see that's not a bug, it's a feature ;-)
    – user2574
    Apr 29, 2012 at 19:28

2 Answers 2

20

With

\begin{table}[htdp] 
\begin{center}
    \begin{tabular}{ | c | c |}
    \hline
    A & B  \\ \hline
    1 & 2 \\ 
    \hline
    \end{tabular}
    \caption{my cap}
\end{center}
  \label{table:example}
\end{table}

the label is defined when the center environment has already been closed and the object which the reference should be attached to has been forgotten.

Never use the center environment inside figure or table:

\begin{table}[htbp] 
\centering
    \begin{tabular}{ | c | c |}
    \hline
    A & B  \\ \hline
    1 & 2 \\ 
    \hline
    \end{tabular}
    \caption{my cap}\label{table:example}
\end{table}

Notice also that d is not among the possible float specifiers and probably you were meaning b (bottom).

12

Had kind of the same Error, but for me the solution was, putting the \label after the \caption command (which makes kind of sense...)

3
  • The \label is after the \caption command in the question so this is likely to be the answer. Please check before posting...
    – user30471
    Oct 5, 2016 at 7:01
  • 1
    I know, but I came here with the same error, but for me it was the wrongly placed '\label', I just want other people coming here lead into the right direction!
    – Marcel
    Oct 6, 2016 at 11:24
  • 3
    Thanks @Marcel. I think your answer is relevant. I ended up here looking for an answer to the question and this worked. Apr 16, 2017 at 18:01

You must log in to answer this question.

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