Incorrect label type using threeparttable and cleveref

cleverefcross-referencingthreeparttable

I'm having an issue with the threeparttable package label type, causing cleverref to not be able to refer correctly to the table. Compiling on Overleaf produces the following warning: "cref reference format for label type `' undefined on input line 8."

Is there a way to correct the label type for this table? Or another workaround that will allow cleveref to reference correctly?

MWE below:

\documentclass{article}

\usepackage{threeparttable}
\usepackage{cleveref}

\begin{document}
    
    Cleveref reference to threeparttable \cref{tab:test}.
    
    \begin{table}
        \centering
        \begin{threeparttable}
            \caption{Caption.}
            \begin{tabular}{l l}
                Header 1 & Header 2 \\
                \hline
                Entry 3 & Entry 4 \\
                \hline
            \end{tabular}
        \end{threeparttable}
        \label{tab:test}
    \end{table}
    
\end{document}

Best Answer

This is not due to either cleveref or threeparttable in particular. It happens because you put \caption within the threeparttable environment and \label cannot "see" its effects outside it. You should move \label into the environment, after \caption.

\documentclass{article}

\usepackage{threeparttable}
\usepackage{cleveref}

\begin{document}

Cleveref reference to threeparttable \cref{tab:test}.

\begin{table}
  \centering
  \begin{threeparttable}
  \caption{Caption.}
  \label{tab:test}
    \begin{tabular}{l l}
      Header 1 & Header 2 \\
      \hline
      Entry 3 & Entry 4 \\
      \hline
    \end{tabular}
  \end{threeparttable}
\end{table}

\end{document}

enter image description here