# How can I use \cref to reference a minted environment?

I've tried

\begin{listing}
\begin{minted}{SQL}
SELECT COUNT(*) FROM lineitem
WHERE l_shipdate <= :shipdate
\end{minted}
\label{test}
\end{listing}


but it doesn't work. \cref{test} reference to the parent section

• Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. Jun 30, 2017 at 14:57

You need something that has a number, i.e. a \caption:

\documentclass{article}
\usepackage{minted}
\usepackage{cleveref}

\begin{document}
\begin{listing}
\begin{minted}{SQL}
SELECT COUNT(*) FROM lineitem
WHERE l_shipdate <= :shipdate
\end{minted}
\caption{Testing}
\label{test}
\end{listing}
See \cref{test}.
\end{document}

• Thanks! but this looks a bit ugly. Can I have something like a surrounding box with title?
– Amos
Jun 30, 2017 at 17:42
• @Amos You can add a simple frame (or rather, lines above and below) with \begin{minted}[frame=lines]{SQL}, but the manual mentions that both the tcolorbox and mdframed packages have interfaces for minted, so in that sense there is a lot of flexibility with regard to frames. To move the caption to the top, see tex.stackexchange.com/a/256796/586 Jun 30, 2017 at 18:19
• @Amos If you have any more requirements I suggest you ask a new question about it. Jun 30, 2017 at 18:20