[Tex/LaTex] Using \autoref on a table inside the figure environment

captionscross-referencingfloatshyperref

I want a plot and a table to show up together in my document, therefore I used the figure environment, made the plot with tikzpicture, linked to this file, then added the \captionof command and then added the table in tabular environment. In my document refering to the table gives me Abb. instead of Tab.. When using the exact same code in a new document refering to the table by \autoref gives me Tab. as it should. In my document though it says Abb. instead of Tab. but the enumeration follows the one of the Tables, not the figures.

\documentclass[12pt]{article}
\usepackage{hyperref}
\usepackage{capt-of}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\addto\captionsngerman{\def\figurename{Abb.}}
\addto\extrasngerman{\def\figureautorefname{Abb.}}
\addto\extrasngerman{\def\tableautorefname{Tab.}}
\begin{document}
\begin{figure}
\centering
    %Figure
    \begin{tikzpicture}
        \begin{axis}
            \addplot coordinates{(1,1)};
        \end{axis}
    \end{tikzpicture}
    \caption{CAPTION}
    \label{figure}
    %Table
    \captionof{table}{CAPTION\label{tab:LABEL}}
        \begin{tabular}{ccc}
            akbvk & laknflfk& kanfkf  \\
        \end{tabular}
\end{figure}

Refering to the figure (\autoref{figure}) and refering to the table (\autoref*{tab:LABEL})
\end{document}

The code shows a minimal example that works as intended in a new document but inserted into my existing document it suddenly doesn't work. Packages and the \addto commands are the same in both documents. My thesis is 50 pages already strong and neither do I want to publish it nor do you (probably) want to dig in it to find what's wrong, but maybe you have an idea what could be the problem…

Compiled in my document
Compiled in a new document

The first picture shows the code, given above, in my document (thesis) the second in the new document.

Edit:
I copied all the packages I use in my thesis into the new document and then deleted one package after the other, this way I found out the subfig package causes the error:

\documentclass[12pt]{article}
\usepackage{hyperref}
\usepackage{capt-of}
\usepackage{subfig}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\addto\captionsngerman{\def\figurename{Abb.}}
\addto\extrasngerman{\def\figureautorefname{Abb.}}
\addto\extrasngerman{\def\tableautorefname{Tab.}}
\begin{document}
\begin{figure}
\centering
%Figure
\begin{tikzpicture}
    \begin{axis}
        \addplot coordinates{(1,1)};
    \end{axis}
\end{tikzpicture}
\caption{CAPTION}
\label{figure}
%Table
\captionof{table}{CAPTION\label{tab:LABEL}}
    \begin{tabular}{ccc}
        akbvk & laknflfk& kanfkf  \\
    \end{tabular}
\end{figure}

Refering to the figure (\autoref{figure}) and refering to the table (\autoref*{tab:LABEL})
\end{document}

Since I wouldn't want to miss on subfig, do you know how to bypass the problem?

Best Answer

The easiest solution is to put an explicit \captionsetup[type=table] before \captionof{table}{...}.

Apparently, \captionof should not be used in floating environment at all, it seems to grab the wrong \@captype content. In final, this will write a wrong anchor name to the .aux file from which \autoref grabs its information to determine the name.

Perhaps, the grouping together is not the best way at all.

The package cleveref provides correct names, however!

\documentclass[12pt]{article}

\usepackage{capt-of}
\usepackage{subfig}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\usepackage{hyperref}
\addto\captionsngerman{\def\figurename{Abb.}}
\addto\extrasngerman{\def\figureautorefname{Abb.}}
\addto\extrasngerman{\def\tableautorefname{Tab.}}
\begin{document}
\begin{figure}
\centering
%Figure
\begin{tikzpicture}
    \begin{axis}
        \addplot coordinates{(1,1)};
    \end{axis}
\end{tikzpicture}
\caption{CAPTION}
\label{figure}
%Table
\captionsetup{type=table}
\captionof{table}{CAPTION\label{tab:LABEL}}
    \begin{tabular}{ccc}
        akbvk & laknflfk& kanfkf  \\
    \end{tabular}
\end{figure}

Refering to the figure (\autoref{figure}) and refering to the table (\autoref*{tab:LABEL})
\end{document}

enter image description here