2

If I define a figure, table, code listing or anything similar with a caption and a label, how can I get the item name together with the reference label?

For example, if I define a figure:

\begin{figure}
    \includegraphics{figures/figure.png}
    \caption{Some figure.}
    \label{figure:some-figure}
\end{figure}

what do I need to do to avoid writing: Figure \ref{figure:some-figure} shows and write \ref{figure:some-figure} shows to produce the following output:

"Figure 1 shows" ?

2

You can use the package cleveref which provides the command \cref{} which automatically adds "figure" or "table" depending on the nature of the float you are referring to. You also have the capitalized version \Cref{}.

\documentclass{article}
\usepackage{cleveref}

\begin{document}

\begin{figure}
\caption{This is a figure.}\label{fig}
\end{figure}

\begin{table}
\caption{This is a table.}\label{tab}
\end{table}

See \cref{fig} or \cref{tab}. \Cref{fig} shows that\dots. \Cref{tab}, on the contrary,\dots

\end{document}

Edit: if you are using listings with a custom name, you should give it explicitly to cleveref:

\crefname{lstlisting}{code listing}{code listings}
\Crefname{lstlisting}{Code Listing}{Code Listings}

See also: cleveref + listings. By the way, I think you need to load cleveref before listings.

| improve this answer | |
  • Thanks, I just tried it, however the cleveref package seems to ignore the alternative name for the listings package: \renewcommand{\lstlistingname}{Code Listing}, it just writes "listing 1", instead of "Code Listing 1". I just found the answer here: tex.stackexchange.com/questions/47495/cleveref-listings – tmaric Mar 21 '13 at 16:34

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.