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.

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?

share|improve this question
minted uses fancyvrb, so the documentation of fancyvrb will help you.+ –  Marco Daniel Mar 4 '13 at 19:24
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 '13 at 20:05
Ok, I added a bogus comment by hand and used the texcl feature. –  green diod Mar 4 '13 at 20:40
add comment

1 Answer

The package minted offers the option texcl which enables LaTeX code inside your listing. However this is really annoying. Another approach is the hacking of \theFancyVerbLine which prints the line numbers. In the example below every line which is numbered get a label. Of course the label and especially the label name can be improved.



#include <class.h>

Class::Class() { 
    // construct stuff



share|improve this answer
Could you explain your solution? I don´t get what it actually achieves? It just prints the number line 4 for me and does no real referencing... –  escalator Dec 11 '13 at 13:05
@escalator: \ref{minted:line:1:4} is the reference to line 4 of the previous environment. So 4 is correct. What would you expect? –  Marco Daniel Dec 11 '13 at 13:13
I just hoped to find a solution for the possibility to create a \label{anyLine} inside of the listing, do not care about the actual line number anymore, and just reference it with \ref. However, there is no way, so I am switching back to lstlisting package. –  escalator Dec 11 '13 at 21:26
add comment

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.