9

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."

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

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

enter image description here

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}
6

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

enter image description here

\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.

2
  • 2
    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
  • 1
    @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.
    – Werner
    Dec 17 '17 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.