# displaying ?? instead of the name of the lstlisting when using \cref

I change the name of the label of the lstlisting to Algorithm and it works fine for the caption. But when I use \cref, the instead of displaying algorithms 1 it shows ?? 1.

\documentclass{scrreprt}
\usepackage{etex}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{bm}
\usepackage[utf8]{inputenc}
\usepackage[pdftex,final]{graphicx}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{hyperref}
\usepackage[noabbrev]{cleveref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{matlab-prettifier}

\renewcommand\lstlistingname{Algorithm}
\begin{document}

\cref{example}

\begin{lstlisting}[style=Matlab-editor, caption=A caption, label={example}]
%My listing
end
\end{lstlisting}

\end{document}


I already tried \crefname{listing}{algorithm}{algorithms} but it still doesn't work for me.

The package cleveref is one of the packages that should be loaded last, even after hyperref, since it does a lot of redefinitions on counters and labelling/referring (of course, since this is the topic cleveref is about).

\documentclass{scrreprt}
\usepackage{etex}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{bm}
\usepackage[utf8]{inputenc}
\usepackage[pdftex,final]{graphicx}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{matlab-prettifier}
\usepackage[noabbrev]{cleveref}

\renewcommand\lstlistingname{Algorithm}
\crefname{listing}{algorithm}{algorithms}

\begin{document}

\cref{example}

\begin{lstlisting}[style=Matlab-editor, caption=A caption, label={example}]
%My listing
end
\end{lstlisting}

\end{document}


• You just change the place where you put \usepackage[noabbrev]{cleveref} ? That's it ? – Pierrick May 27 '15 at 21:12
• @PierrickBersier: Nope, It was my fault. I forgot the \crefname in my first edit. – user31729 May 27 '15 at 21:23
• @PierrickBersier - I just up-voted your posting. You should be able to up-vote answers from now on. :-) – Mico May 27 '15 at 21:34
• I upvoted your answer too... – Mico May 27 '15 at 21:37
• @Mico: I suspected your evil-deeds ;-) – user31729 May 27 '15 at 21:38

Cleveref has to be informed of the name you want to give to the environment. Here is a solution which uses the name of the environment dynamically (partially!). The code can be written in a cleveref.cfg file:

\documentclass{scrreprt}
\usepackage{etex}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{bm}
\usepackage[utf8]{inputenc}
\usepackage[pdftex,final]{graphicx}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{hyperref}
\usepackage[noabbrev]{cleveref}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{matlab-prettifier}
\crefname{lstlisting}{\MakeLowercase\lstlistingname}{\MakeLowercase\lstlistingname s}
\Crefname{lstlisting}{\lstlistingname}{\lstlistingname s}
\renewcommand\lstlistingname{Algorithm}
\begin{document}

\cref{example}
\begin{lstlisting}[ caption=A caption, label={example}]%style=Matlab-editor,
%My listing
end
\end{lstlisting}

\renewcommand\lstlistingname{Pseudo-code}
\cref{example2}
\begin{lstlisting}[ caption=A caption, label={example2}]%style=Matlab-editor,
%My listing
end
\end{lstlisting}

\end{document}