0

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}
1
  • 1
    Why don't you place the \label directive immediately after the \caption directive?
    – Mico
    Apr 12, 2022 at 1:45

2 Answers 2

3

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

6
  • Ah such an easy fix, thanks! As it seems to work with the label after all of the table as well, why is it preferable for the label and caption next to each other Apr 12, 2022 at 0:14
  • 1
    Not for any hard rule, it is just easier not to make this kind of mistake if you maintain the habit of placing the label right after the caption.
    – gusbrs
    Apr 12, 2022 at 0:16
  • You wrote, "You can either move \caption out of the threeparttable environment or the \label into it." The former suggestion would make using the threeparttable environment pretty much pointless. The 3 formal parts of a threeparttable environment are the caption, a tabular-like environment, and (optionally) a tablenotes environment. The machinery of the tablenotes package then assures that the widths of the caption and of the tablenotes material don't exceed the width of the tabular env. Hence, it's not good advice to place the caption outside the threeparttable environment.
    – Mico
    Apr 12, 2022 at 1:28
  • @user7794764 - Your assertion that "As it seems to work with the label after all of the table as well" is incorrect. To verify this counter-claim, take the answer above, insert two copies of \refstepcounter{figure} before the start of the table environment, emove the \caption directive from its current location, and place it after the end of the table environment instead (to comply with your assertion). Recompile the document twice and take note that \cref{tab:test} outputs "fig. 2", not "table 1".
    – Mico
    Apr 12, 2022 at 1:35
  • 1
    @Mico You're right, of course. I have edited to correct the answer. Thank you!
    – gusbrs
    Apr 12, 2022 at 1:58
1

The same is valid if you use talltblr table environment defined in tabularray package. It is equivalent of threeparttable:

\documentclass{article}
\usepackage{tabularray}
\usepackage{cleveref}

\begin{document}

Cleveref reference to \verb+talltblt+ table is \cref{tab:test}.

\begin{table}
    \centering
    \begin{talltblr}[
caption = {Caption.},
  label = {tab:test} % <---
                    ]{cells=l}
Header 1 & Header 2 \\
      \hline
Entry 3 & Entry 4\TblrNote{a}   \\
      \hline
    \end{talltblr}
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

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