# Reference source code line in minted environment with cleveref

I'd like to reference lines in the minted environment (solved here for regular \ref) using cleveref.

MWE:

% !TEX TS-program = arara
% arara: pdflatex: { shell: yes }
% arara: pdflatex: { shell: yes }

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

\begin{document}
\section{First section}

\begin{minted}[linenos=true, mathescape]{c}
int i = 0;
int j = 1; // label $\label{ln:unreachable1}$
\end{minted}

Test with ref: \ref{ln:unreachable1}

Test with cref: \cref{ln:unreachable1}

\end{document}


Gives

i.e. the containing section instead of "line \$number".

• I'm afraid you're out of luck. This almost certainly requires explicit support in cleveref, which doesn't exist yet. And I'm unlikely to have time to add it myself anytime soon. It might be easy to code the necessary support yourself, or it might be hard, depending on how the minted environment is implemented. The algorithmicx support already in cleveref might give you some clues. If you send me a patch to cleveref to add minted support, I'd certainly consider applying it. – Toby Cubitt Nov 18 '13 at 22:13

\newcommand{\Vlabel}[1]{\label[line]{#1}\hypertarget{#1}{}}

Make the label with \Vlabel{label} and then use \lref{label}. I know, this is not much better than manually writing the link. At least, hyperlinks are working.
With [line], I could get cleveref print out "Line" instead of the section, but the reference is still wrong.