7

How can one make LaTeX insert Figure into \ref calls automatically, i.e. instead of writing:

See data in Figure ~\ref{fig:JobInformationDialog}

simply write:

See data in \ref{fig:jobInformationDialog}

and have LaTeX insert Figure [fignum] when it gets compiled?

migrated from stackoverflow.com May 13 '14 at 17:31

This question came from our site for professional and enthusiast programmers.

8
+200

A very easy way is provided by the prettyref package:

\documentclass{article}
\usepackage{caption}
\usepackage{prettyref}
\newrefformat{fig}{Figure~[\ref{#1}]}

\begin{document}

\begin{center}
  \rule{10cm}{2cm}
  \captionof{figure}{This is my caption}
  \label{fig:first}
\end{center}

Please see \prettyref{fig:first}.
\end{document}

enter image description here

3

hyperref provides \autoref which automatically sets a title with every reference. For figures, this is Figure:

enter image description here

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\begin{figure}[ht]
  \centering\rule{10cm}{2cm}
  \caption{This is my caption}
  \label{fig:first}
\end{figure}

Please see \autoref{fig:first}.
\end{document}

The starred version \autoref* removes the internal document hyperlinking for that specific link only.

2

The cleveref package provides macros \cref and \Cref that do exactly this, in a very configurable way (see documentation):

A captioned figure with a reference using cleveref

\documentclass{article}
\usepackage{hyperref,cleveref}

\begin{document}

\begin{figure}[ht]
  \centering\rule{10cm}{2cm}
  \caption{This is my caption}
  \label{fig:first}
\end{figure}

Please see \cref{fig:first}.
\end{document}

Using hyperref is optional, but it should be loaded before cleveref.

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.