Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
Please provide a complete MWE that shows your problem. Do you compile twice? –  Marco Daniel Apr 29 '12 at 16:49
2  
If you don't know what a MWE is you can read it here: minimal working example (MWE) –  Marco Daniel Apr 29 '12 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 ;-) –  Axel Sommerfeldt Apr 29 '12 at 19:28
add comment

1 Answer

up vote 16 down vote accepted

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.