Detect float type from label
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}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink,noabbrev]{cleveref}
\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.