14
\documentclass{scrbook}

\usepackage{listings}
\usepackage{cleveref}

\begin{document}

Ref: \ref{test}.
Cref: \cref{test}.

\begin{lstlisting}[caption={Test},label={test}]
test
\end{lstlisting}

\end{document}

This produces Ref: 1. Cref: ?? 1.. I expected something like Ref: 1. Cref: Listing 1.

I get a LaTeX Warning: \Cref reference format for label type 'lstlisting' undefined on input line 9.

lockstep
  • 250,273
Dennis
  • 819
  • 9
  • 17
  • 2
    Works for me. What versions are you using? – yannisl Mar 10 '12 at 05:44
  • ("C:\Program Files\MiKTeX 2.9\tex\latex\cleveref\cleveref.sty" Package: cleveref 2011/03/22 v0.17.9 Intelligent cross-referencing Package cleveref Info:listings' support loaded on input line 2312.)` Please check you log file. – yannisl Mar 10 '12 at 05:48
  • I am using the last version from here: http://www.ctan.org/tex-archive/macros/latex/contrib/cleveref – Dennis Mar 10 '12 at 05:54
  • @YiannisLazarides - I added the same log meesage to my question. – Dennis Mar 10 '12 at 05:55
  • 1
    Can you pleae read you .log file and post the version number? – yannisl Mar 10 '12 at 05:56
  • For what it's worth, when I run your program (I have MacTeX2011), with Package: cleveref 2012/02/15{} v0.18.4{} showing in the log file`, I don't experience any problems either. – Mico Mar 10 '12 at 11:53
  • Hmmm.... Seems like a classic problem when multiple runs (with the .aux files left intact), are required to resolve references. Are you processing this twice? First run produces your output, the subsequent run produces the desired output. There should also have been a LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right. in your log file indicating that multiple runs are required. – Peter Grill Mar 10 '12 at 17:16
  • The version number is not shown. It just shows (/usr/share/texmf-texlive/tex/latex/cleveref/cleveref.sty)... just writing this I realized, that I did not run latex cleveref.ins. Now I did it and it's working now. Thanks for your pointers. – Dennis Mar 12 '12 at 03:51

1 Answers1

25

Solution 1

Add the following to your header:

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

Solution 2

Download the newest version from here (cleveref.dtx and cleveref.ins) and run latex cleveref.ins.

Dennis
  • 819
  • 9
  • 17
  • As I mentioned in the comments, your original MWE works fine if you just run it twice. – Peter Grill Mar 12 '12 at 04:09
  • It didn't, even after running it 10 times, because of the LaTeX Warning: \Cref reference format for label type 'lstlisting' undefined on input line 9.. Seriously, my cleveref version was to old :). – Dennis Mar 12 '12 at 04:27
  • The link shows version 0.19 2013-12-28. tlmgr info cleveref tells I run 0.19 2014-02-26 which I assume is even newer. Still, I ran latex several times, and it would not detect the listing as a listing. Had to go with solution 1 :( – mcnesium Nov 26 '15 at 10:20
  • Regarding solutions 1, it didn't work for me. The problem and the solution is explaind here: http://tex.stackexchange.com/questions/86157/cleveref-doesnt-print-correct-caption-for-listings Basically, lstlisting should be replaced with listing due to some alias problems. – Talouv Dec 30 '15 at 09:46
  • 2
    @Dennis Just a small hint: The call to \Crefname is superfluous in the above case. As mentioned in section 8.1.2 of the cleveref documentation, "If the corresponding \Crefname is undefined when \crefname is called, it will automatically define \Crefname to be a capitalised version of \crefname, using \MakeUppercase." – Janosh Aug 11 '16 at 22:03
  • 5
    For those who run into the same issue having installed the current version as @mcnesium: Make sure that listings is included BEFORE cleveref! – tbolender May 28 '17 at 16:06
  • If you came here based on question search terms, but use \lstinputlisting, then your solution is given here: https://tex.stackexchange.com/questions/338141/cleveref-with-hyperref-uses-wrong-label-when-referring-to-line-in-code-listing – a.t. Nov 24 '19 at 12:59