3

I used to use listings a lot but minted+pygmentize has outstanding output. However, there are a few features from listings that are hard to find in minted, like having the line numbers on the right.

Is there a way to cross reference lines in a minted environment as in listings?

3
  • minted uses fancyvrb, so the documentation of fancyvrb will help you.+ Mar 4, 2013 at 19:24
  • 3
    You can use texcl=true to allow LaTeX in comments, and then put a \label in a comment and \ref that. Unfortunately, Pygments doesn't allow LaTeX anywhere except for comments (though that feature has been requested). So Pygments is a bit more limited than listings in that respect. (You could always take the direct output of pygmentize and add \label's by hand, but that's probably not what you were looking for.)
    – G. Poore
    Mar 4, 2013 at 20:05
  • Ok, I added a bogus comment by hand and used the texcl feature.
    – green diod
    Mar 4, 2013 at 20:40

1 Answer 1

7

I know this is an "ancient" question but I would like to answer it to help those who get here like me. :)

minted nows provides a macro called escapeinside (also texcl and texcomments for LaTeX in comments, please check the document for more):

escapeinside (string) (default: ⟨none ⟩)

Escape to LaTeX between the two characters specified in (string). All code between the two characters will be interpreted as LaTeX and typeset accordingly. This allows for additional formatting. The escape characters need not be identical. Special LaTeX characters must be escaped when they are used as the escape characters (for example, escapeinside=\#\%). Requires Pygments 2.0+.

Escaping does not work inside strings and comments (for comments, there is texcomments)....

An example given in the document is:

\begin{minted}[escapeinside=||]{py}
def f(x):
    y = x|\colorbox{green}{**}|2
return y
\end{minted}

I've tried that. We can also use \label{refname} to create a line reference using esacpeinside like this:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{minted}

\begin{document}
\begin{minted}[escapeinside=||,linenos,gobble=0]{c}
void hello() |\label{line:hello_start}|
{
    printf("Hello minted!\n");
} |\label{line:hello_end}|
int main()
{
    hello(); |\label{line:invoke}|
    return 0;
}
\end{minted}
The code at Line~\ref{line:invoke} invokes a function defined
between Line~\ref{line:hello_start} and Line~\ref{line:hello_end}.
\end{document}

The example on Overleaf.

Hope this helps. :)

5
  • Thank you @mkdong for updating this. I now have a lot of stuff with listings but for new projects, I'll think about it.
    – green diod
    Jan 14, 2017 at 17:48
  • I think this only works when the document is not compiled in draft mode (so do not worry about undefined labels in draft mode).
    – asante
    Apr 30, 2019 at 20:47
  • 2
    I spent way too long trying to figure this out. Dashes are NOT allowed in these labels. For some reason, this causes the label to be undefiend. Sucks, because the rest of my document standardized on kebab-casing our refs.
    – ollien
    Mar 18, 2021 at 1:28
  • This is helpful, thank you! Seems to be incompatible with cleveref, though?
    – Sam Estep
    Nov 16, 2023 at 19:46
  • Also, even after switching from \cref to just \ref, when I click the reference in the PDF, it jumps to the beginning of the section and does not actually jump to the line number; is there any way to fix this?
    – Sam Estep
    Nov 16, 2023 at 19:53

You must log in to answer this question.

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