TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Jan 17 '14 at 17:36
Keep in mind that minted now is the recommended package for using pygments. – koppor Oct 7 '15 at 5:57
up vote 13 down vote accepted

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}.
share|improve this answer
cool :-)! works! – Gabriel Aug 8 '11 at 10:07
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 7 '15 at 10:06

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...
share|improve this answer
With listings you can refer to a single line of the code. I'd be amazed if minted couldn't do the same. – Seamus Aug 5 '11 at 12:53
@Seamus: as a matter of fact, it's not mentioned in the documentation at least. – ℝaphink Aug 5 '11 at 12:56
yes I just looked. I am amazed. As I said I would be... – Seamus Aug 5 '11 at 13:03
@Seamus How do you refer to a single line in listings? Thanks! – Alenanno Nov 2 '13 at 22:57
@Alenanno look at my answer to this question. – Seamus Nov 4 '13 at 12:01

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

share|improve this answer
So it's a choice between nice colors and line references... or patching the package for everybody's benefit! – ℝaphink Aug 5 '11 at 13:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.