0

Here is my code:

\documentclass{article}
\usepackage{minted}
\usepackage{cleveref}
\begin{document}
\begin{minted}[linenos,escapeinside=||]{cpp}
Hello,
world! |\label{foo}|
\end{minted}
See line no.\ref{foo}.
See \cref{foo}.
\end{document}

It renders:

enter image description here

How to fix?

yegor256
  • 12,021

1 Answers1

3

I don't know if that's a possibility for you, but zref-clever can handle the case:

\documentclass{article}
\usepackage{minted}
\usepackage{zref-clever}
\AddToHook{env/minted/begin}{\zcsetup{currentcounter=FancyVerbLine}}
\zcsetup{countertype={FancyVerbLine=line}}
\begin{document}
\begin{minted}[linenos,escapeinside=||]{cpp}
Hello,
world! |\label{foo}|
\end{minted}
See line no.~\ref{foo}.
See \zcref{foo}.
\end{document}

enter image description here

Note that I don't know minted very well and just gave it a quick look and, as far as I can tell, it uses the FancyVerbLine counter for line numbering. Also, minted seems to resort to a hard to track resetting behavior when langlinenos is enabled. That might well confuse the above setup, and would probably require further investigation and fine-tuning. (I don't think the consequences would be serious though. Things like if labels to two "lines 3" of different languages were to be referenced in the same command, zref-clever would not be able to distinguish between them and would compress them into one. But both of them are of type line anyway, so it would be somewhat strange to typeset "lines 3 and 3" instead of just "line 3").

gusbrs
  • 13,740