3

If I combine cleverref with subcaption and minted, then \cref{lst:subfigure} does resolve to "?? 1a", when it should be "listing 1a". How can I fix this?

Minimal non-working example

\documentclass{book}

\usepackage[newfloat=true]{minted}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{cleveref}

\begin{document}

\begin{listing}
  \begin{subfigure}{.49\textwidth}
    \begin{minted}{cpp}
void foo() {};
    \end{minted}
    \caption{Left figure caption}
    \label{lst:foo}
  \end{subfigure}
  \begin{subfigure}{.49\textwidth}
    \begin{minted}{cpp}
void bar() {};
    \end{minted}
    \caption{Right figure caption}
    \label{lst:bar}
  \end{subfigure}
  \caption{Overall Caption}
  \label{lst:foo-bar}
\end{listing}

\begin{itemize}
\item Overall: \cref{lst:foo-bar}
\item Foo: \cref{lst:foo}
\item Bar: \cref{lst:bar}
\end{itemize}

\end{document}

Rendered version with "?? 1a"

1 Answer 1

4

You get

LaTeX Warning: cref reference format for label type `sublisting' undefined on input line 35.

LaTeX Warning: cref reference format for label type `sublisting' undefined on input line 36.

Teach cleveref what you want:

\documentclass{book}

\usepackage[newfloat=true]{minted}
\usepackage{subcaption}
%\usepackage{caption}
\usepackage{cleveref}

\crefname{sublisting}{sublisting}{sublistings}
\Crefname{sublisting}{Sublisting}{Sublistings}

\begin{document}

\begin{listing}
  \begin{subfigure}{.49\textwidth}
    \begin{minted}{cpp}
void foo() {};
    \end{minted}
    \caption{Left figure caption}
    \label{lst:foo}
  \end{subfigure}
  \begin{subfigure}{.49\textwidth}
    \begin{minted}{cpp}
void bar() {};
    \end{minted}
    \caption{Right figure caption}
    \label{lst:bar}
  \end{subfigure}
  \caption{Overall Caption}
  \label{lst:foo-bar}
\end{listing}

\begin{itemize}
\item Overall: \cref{lst:foo-bar}
\item Foo: \cref{lst:foo}
\item Bar: \cref{lst:bar}
\end{itemize}

\end{document}

enter image description here

You may want

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

or

\crefname{sublisting}{Listing}{Listings}

if you always want uppercase. Take your pick.

1
  • That does what I want. Thanks for your answer, much appreciated. :)
    – Flow
    Sep 23 at 12:39

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.