5

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.

0

2 Answers 2

5

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}

enter image description here

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

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} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .