# Detect float type from label

The command \autoref{some-label-goes-here} from the hyperref package would print "Figure X", or "Table Y", etc. depending on the type of the referenced float.

How can I print "Figure" / "Table" (without the number) given the label? Something like \floattype{some-label-goes-here}.

This is useful if one wants to say, for example,

The \floattype{some-figure-label} shows the expansion rules


which would expand to "The figure shows the expansion rules."

• The cleveref package has \namecref that does this. Dec 16 '17 at 21:40

It's straightforward to achieve your objective by employing the cleveref package and its \namecref macro.

If you need to upppercase the first letters in figure and table, load the cleveref package with the option capitalise. (capitalize works too.)

\documentclass{article}
\begin{document}
\begin{figure}[h]\caption{Hello}\label{fig:h}\end{figure}
\begin{table}[h]\caption{Goodbye}\label{tab:g}\end{table}

\Cref{fig:h} shows that \dots

The \namecref{fig:h} shows that \dots

\Cref{tab:g} show that \dots

The \namecref{tab:g} demonstrates that \dots
\end{document}


The following inserts the reference type as the fifth, unused element in the components written with every \label:

\documentclass{article}

\usepackage{hyperref,etoolbox}

\makeatletter
\def\strip@@period#1.#2\@nil{#1}
\def\@@currentHref{\expandafter\strip@@period\@currentHref\@nil}
\newcommand{\@@fifthoffive}[5]{%
\@ifundefined{type@name@#5}
{#5}
{\csname type@name@#5\endcsname}%
}
\newcommand{\settyperef}[2]{\@namedef{type@name@#1}{#2}}
\newcommand{\typeref}[1]{%
\HyRef@StarSetRef{#1}\@@fifthoffive
}%
\AtBeginDocument{
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\label}{{\@currentHref}{}}{{\@currentHref}{\@@currentHref}}{}{}
}
\makeatother

\begin{document}

\begin{figure}
\caption{A figure caption}
\label{fig:figure}
\end{figure}

See \autoref{fig:figure}. It is a \typeref{fig:figure}.

\settyperef{figure}{FiGuRe}

See \autoref{fig:figure}. It is a \typeref{fig:figure}.

\end{document}


\settyperef{<type>}{<output>} allows you to format what the output should look like.

• What is the advantage of this solution vs using the cleveref package? EDIT: Seems like a good source of information for some of the inner workings of hyperref.
– dow
Dec 17 '17 at 2:18
• @dow: Using an existing package is always a good idea, since it's probably undergone some scrutiny by the community and may be updated to support different features. However, using such an encompassing package would seem to suggest changing your user interface (\cref instead of \ref). Mine allows you to stick to your format of using \autoref, but it really depends on the end-user. Dec 17 '17 at 7:44