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?

    bla bla bla bla ;   \label{myline}

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

Thanks for any help!!


4 Answers 4


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 ;
The important line is line \ref{myline}.
  • cool :-)! works!
    – Gabriel
    Commented Aug 8, 2011 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
    Commented May 10, 2012 at 11:18
  • Is it correct that there's no way (with mathescape) to specify a label without requiring a comment symbol?
    – Mark
    Commented Apr 22, 2015 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
    Commented Apr 23, 2015 at 21:09
  • Works at c++, doesn't work for xml language.
    – koppor
    Commented Oct 7, 2015 at 10:06

@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 ;
The important line is line \ref{myline}.
  • 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
    Commented Oct 1, 2020 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
    Commented Oct 2, 2020 at 0:17

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

  def foo
    while do bar(*\label{line}*)
As we can see in line \ref{line}

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...

  • 4
    So it's a choice between nice colors and line references... or patching the package for everybody's benefit!
    – raphink
    Commented Aug 5, 2011 at 13:11

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:

    bla bla bla bla ;   \label{myline}
  \caption{My nice listing}

In line~2 of listing \ref{lst:nice_listing}, we can see that...
  • With listings you can refer to a single line of the code. I'd be amazed if minted couldn't do the same.
    – Seamus
    Commented Aug 5, 2011 at 12:53
  • 1
    @Seamus: as a matter of fact, it's not mentioned in the documentation at least.
    – raphink
    Commented Aug 5, 2011 at 12:56
  • 1
    yes I just looked. I am amazed. As I said I would be...
    – Seamus
    Commented Aug 5, 2011 at 13:03
  • @Seamus How do you refer to a single line in listings? Thanks!
    – Alenanno
    Commented Nov 2, 2013 at 22:57
  • @Alenanno look at my answer to this question.
    – Seamus
    Commented Nov 4, 2013 at 12:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .