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:

    \caption{Some 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" ?


1 Answer 1


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{}.



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

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

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


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.

  • 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, 2013 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.