I don't use minted, but here's how I would do it for fancyvrb, which minted uses internally. Here, I define new command \VerbLabel{linex}{a $ a} which assigns the label linex to the source code line a $ a.
It's written using expl3, so see the source3 documentation for explanation for any of the commands that don't make sense. In brief: prop in expl3 is a ‘property list’ datatype that holds pair-value data. \prop_gput is the allocator, \prop_if_in tests for the existence of a key, and \prop_get_gdel is an accessor function that also deletes the entry afterwards.
\documentclass{article}
\usepackage{expl3,fancyvrb}
\begin{document}
\ExplSyntaxOn
\cs_generate_variant:Nn \prop_gput:Nnn {Nxn}
\cs_generate_variant:Nn \prop_get_gdel:NnN {NxN}
\cs_generate_variant:Nn \prop_if_in:NnT {NxT}
\prop_new:N \g_verb_label_prop
\newcommand \VerbLabel [2] {
\prop_gput:Nxn \g_verb_label_prop { \tl_to_str:n {#2} } {#1}
}
\renewcommand \FancyVerbFormatLine [1] {
#1
\prop_if_in:NxT \g_verb_label_prop { \tl_to_str:n {#1} }
{
\prop_get_gdel:NxN \g_verb_label_prop { \tl_to_str:n {#1} } \l_tmpa_tl
\label {verbline:\l_tmpa_tl}
}
}
\ExplSyntaxOff
\VerbLabel{linex}{a $ a}
\VerbLabel{liney}{c & c}
See lines \ref{verbline:linex}--\ref{verbline:liney}
\begin{Verbatim}[numbers=left]
1 _ 1
a $ a
b # b
c & c
9 ^ 9
\end{Verbatim}
\end{document}
There is a bug in this code which means it doesn't work for lines that contain the # char, but that's not an unsolvable problem.
minteduses thefancyvrbenvironment, but doesn't seem to support thecommandcharsoption: this is a problem here :-( Perhaps Konrad can help out, as he actually writesminted. – Joseph Wright♦ Nov 15 '10 at 9:50