# Table reference not compiling correctly

I'm hoping for some guidance on why LaTeX isn't properly referencing this table.

\begin{table}[ht]
\caption*{Table 2 \\ Number and Percentage}\label{tab:table2}
\centering
\begin{tabular}{rrrrrr}
\hline
& \# & \# & \# & \% & \% \\
\hline
A  & 446 & 105 & 42 & 23.54 & 9.42 \\
B  & 389 & 6 & 69 & 1.54 & 17.74 \\
C  & 355 & 8 & 79 & 2.25 & 22.25 \\
D  & 343 & 21 & 107 & 6.12 & 31.20 \\
\hline
\end{tabular}
\end{table}

In Table~\ref{tab:table2} ...


I've tried compiling multiple times and moving the label inside the caption, but neither of those has worked; I still get "In Table ??" as output. Why might this be happening? Thanks for any help!

-
This works fine for me. Why are you manually captioning the table as "Table 2" inside of the \caption? This is what the command \caption is for; it automatically does these sorts of things. It might be helpful if you turn this into a complete minimal (non-)working example (MWE), in case any packages that you might be loading are the culprit. Also, welcome to TeX.SX! – Adam Liter Mar 22 '14 at 20:27
Are you loading the caption package (i.e., \usepackage{caption})? – Adam Liter Mar 22 '14 at 20:34
Yes to the caption package. It was the first (and so far only) way I found to put "Table 2:" and the name on two separate lines and to remove the colon. I just tried taking out and that solved the problem; is there a way I can keep that formatting, though? Thank you for the tips! – user3447259 Mar 22 '14 at 20:39
\caption* does not update a counter so does not set a referencable value. You need to use \caption. But please always post complete documents that reproduce the problem, not just fragments as here. – David Carlisle Mar 22 '14 at 20:40

As @DavidCarlisle notes in the comments, \caption* does not update a counter, so there is nothing for \label to label.

The caption documentation says (p. 17):

The longtable package defines the command \caption* which typesets the caption without label and without entry in the list of tables. [. . .] The caption package offers this feature, too, so you can use this command now within every floating environment [emphasis added]

To achieve the format that you want (as you noted in the comments), you can just make use of caption's \captionsetup.

Also, I've taken the liberty of using booktabs in the following MWE. You might want to use this package in order to typeset nicer tables. See the documentation for some tips on typesetting tables. In brief, the package provides \toprule, \bottomrule, and \midrule, which are nicer than \hline.

\documentclass{article}

\usepackage{caption}
\captionsetup[table]{
labelsep=newline,
justification=centering
}

\usepackage{booktabs} % for nicer looking tables

\begin{document}

\begin{table}[ht]
\caption{Number and Percentage}\label{tab:table2}
\centering
\begin{tabular}{rrrrrr}
\toprule
& \# & \# & \# & \% & \% \\
\midrule
A  & 446 & 105 & 42 & 23.54 & 9.42 \\
B  & 389 & 6 & 69 & 1.54 & 17.74 \\
C  & 355 & 8 & 79 & 2.25 & 22.25 \\
D  & 343 & 21 & 107 & 6.12 & 31.20 \\
\bottomrule
\end{tabular}
\end{table}

In Table~\ref{tab:table2} ...

\end{document}
`

-