Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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 at 17:36
add comment

3 Answers

up vote 10 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 ;
\end{minted}
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
add comment

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...
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
1  
@Seamus: as a matter of fact, it's not mentioned in the documentation at least. –  ℝaphink Aug 5 '11 at 12:56
1  
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
add comment

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

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
add comment

Your Answer

 
discard

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.