17

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

19

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}.
  • cool :-)! works! – Gabriel Aug 8 '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 7 '15 at 10:06
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...

  • 3
    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
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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