2

I am using the minted package's listing environment to typeset MATLAB source code.

As explained in the minted manual, I have used the \SetupFloatingEnvironment{listing}{name=Program code} option to change the caption title from the default listing to Program code. This succesfully updates the caption. However, when cross-referencing the source code in text, cleveref still uses the word listing.

Here is a MWE.

\documentclass{article}
\usepackage[newfloat=true]{minted}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}
\SetupFloatingEnvironment{listing}{name=Program code}

\begin{document}

The code for computing mass-energy relationship is given in~\cref{code:massenergy}.

\begin{listing}
    \begin{minted}[autogobble,frame=single]{matlab}
    clear; close all; clc;
    e = m*c^2;
    \end{minted}
    \caption{Mass-Energy Relationship}
    \label{code:massenergy}
\end{listing}

\end{document}

The following is the output produced

Wrong cross-refence string by cleveref for minted source code

How can I get cleveref to produce the expected cross-reference string in text?

  • 4
    Add \crefname{listing}{Program code}{Program codes} – egreg May 27 '18 at 14:55
3

The newfloat package doesn't pass the new name to cleveref; you also need the plural form anyway.

Add

\crefname{listing}{Program code}{Program codes}

after the \SetupFloatingEnvironment line.

You can avoid code duplication by doing

\crefname{listing}{\listingname}{\listingname s}
  • Is there a way to define the desired caption name only once in the document (perhaps as a variable?) and have both minted and cleveref pick it up automatically? – Krishna May 28 '18 at 10:48
  • 1
    @Krishna Somewhat possible. See edit. – egreg May 28 '18 at 11:12
  • Upvoted. Almost there. But this results in capitalisation of the reference, i.e. we get a sentence like "the source code listing is given in Program code 1.1". Is this acceptable typographic convention? the lower-case \cref usage should result in lower-cased referencing right? The \MakeLowercase construct is not working for me and results in an undefined reference ?. Any thoughts on how to fix this? – Krishna May 28 '18 at 11:31
  • I figured it out. The trick is to use MakeLowercase right where crefname gets defined and not in the body of the document. So, the solution is \crefname{listing}{\MakeLowercase{\listingname}}{\MakeLowercase{\listingname s}} – Krishna May 28 '18 at 11:41

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.