23

Does anybody know a working example, where a source code line is reference in minted? I have not found any help till now how to do this?

\begin{minted}[linenos=true]{c++}
    bla bla bla bla ;   \label{myline}
\end{minted}

The above does not work, unfortunately, there is not commandchars or excape for latex commands only for math?

Thanks for any help!!

lockstep
  • 250,273
Gabriel
  • 1,574

4 Answers4

23

Use mathescape, then put your \label in math mode in a comment:

\begin{minted}[linenos=true, mathescape]{c++}
    i = i + 1 ;  
    j = j + 1 ; // The important line $\label{myline}$
    k = k + 1 ;
\end{minted}
The important line is line \ref{myline}.
Ant
  • 7,568
  • cool :-)! works! – Gabriel Aug 08 '11 at 10:07
  • 1
    Yes, it works for me too but not as well as desired. It inserts right line number as link but this link points to whole listing, not a mentioned line. Anyway you should accept this answer. – Kirill May 10 '12 at 11:18
  • Is it correct that there's no way (with mathescape) to specify a label without requiring a comment symbol? – Mark Apr 22 '15 at 19:11
  • @Mark Unfortunately I think this will require editing the relevant Pygments lexer – everything gets taken over to Pygments before LaTeX has a chance to edit bits out. Incidentally you can use the texcomments option instead of mathescape to avoid having to put the \label in math mode. – Ant Apr 23 '15 at 21:09
  • Works at c++, doesn't work for xml language. – koppor Oct 07 '15 at 10:06
  • Works for python with the comment symbol #. Also, we can refer to a label in the code with $\ref{}$ in math mode! – Sigur Feb 19 '19 at 18:28
  • My code is plenty of $ symbols and they are not comments, they are used in the R language to specify variables inside dataframes. How can I keep them and still use the mathscape trick? – skan Oct 01 '20 at 17:40
16

@Ant's answer is great, but requires that your \label go inside a comment that will appear in the rendered code. You can get around this by using escapeinside instead of mathescape:

\begin{minted}[linenos=true, escapeinside=!!]{c++}
    i = i + 1 ;  
    j = j + 1 ; !\label{myline}!
    k = k + 1 ;
\end{minted}
The important line is line \ref{myline}.
jme
  • 333
  • Can I use any delimiter? It's difficult to find a symbol not already used in the code for other things. Is it possible to use longer delimiters instead of a single character? – skan Oct 01 '20 at 17:42
  • 1
    I have my minted code in an appendix. Your solution points to the appendix but not to the line of code nor even the page where the label is located. How can I get it? – skan Oct 02 '20 at 00:17
3

If you are happy to change from minted to listings then it is possible as this example shows:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
  escapeinside={(*}{*)},
  numbers=left
}
\begin{document}
\begin{lstlisting}
  def foo
    while do bar(*\label{line}*)
  end
\end{lstlisting}
As we can see in line \ref{line}
\end{document}

Unfortunately, this means a lot more work to get nice colourful syntax highlighting. escapeinside defines a way to escape the verbatim environment and have what's inside the (* and *) actually read by TeX...

Seamus
  • 73,242
  • 4
    So it's a choice between nice colors and line references... or patching the package for everybody's benefit! – raphink Aug 05 '11 at 13:11
2

I do not think you can refer to a single line of code. You can however embed the minted call in a listing environment and refer to that listing:

\begin{listing}
  \begin{minted}[linenos=true]{c++}
    bla bla bla bla ;   \label{myline}
  \end{minted}
  \caption{My nice listing}
  \label{lst:nice_listing}
\end{listing}

In line~2 of listing \ref{lst:nice_listing}, we can see that...
raphink
  • 31,894