5

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

enter image description here

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

  • 4
    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
2

Although, one cannot get it directly to work, there is a workaround:

\newcommand{\Vlabel}[1]{\label[line]{#1}\hypertarget{#1}{}}
\newcommand{\lref}[1]{\hyperlink{#1}{\FancyVerbLineautorefname~\ref*{#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.

The solution is based on the discussion at http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2008-12/msg00075.html.

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.