1

I've recently swapped from the listing package to the minted package for my code listings. When I used lstlistings, I was able to redefine \lst@Key to automatically place labels of the format \lst@label:\arabic{lstnumber} at every line number.

After skimming through the minted and fancyvrb source I have not been able to find a way to do this for the minted package however.

Redefining \theFancyVerbLine seems like the obvious solution, but i don't have access to the minted label there.

Any ideas for how this might be implemented?

candr
  • 21
  • What's the rationale about adding “automatic labels”? You need to check the line number anyway, don't you? – egreg Oct 14 '22 at 12:37
  • @egreg It's just for hyperref's. It's nice to be able to jump straight to the line being referenced. – candr Oct 14 '22 at 12:39
  • There is a deleted answer which pretty much does that https://tex.stackexchange.com/a/122906/36296 – samcarter_is_at_topanswers.xyz Oct 14 '22 at 12:40
  • I don't see it; is there any way to view deleted answers? @samcarter_is_at_topanswers.xyz – candr Oct 14 '22 at 12:49
  • 1
    Here is a screenshot: https://i.stack.imgur.com/M6ZH5.png (the line cut off reads \def\theFancyVerbLine{\rmfamily\tiny\arabic{FancyVerbLine}\label{minted:line:\the\value{mintedcount}:\arabic{FancyVerbLine}}}) – samcarter_is_at_topanswers.xyz Oct 14 '22 at 12:50
  • @candr Please don't add your solution into your question. You could write a self-answer instead. – samcarter_is_at_topanswers.xyz Oct 14 '22 at 13:53
  • Thanks for the screenshot - that was exaclty the pointer i needed! As you can see in the edit, I ended up wrapping minted in a new enviornment so i could access the label of individual listings instead of just a global minted counter. This also makes hyperlinks jump to the exact line, instead of all going to where mintedcount was last refstepped. – candr Oct 14 '22 at 13:56
  • 1
    @samcarter_is_at_topanswers.xyz oh i didnt know you could do that. I've moved my solution to an answer now (: – candr Oct 14 '22 at 13:57

1 Answers1

1

I ended up wrapping minted in a new environment, allowing me to make \theFancyVerbLine place labels with the <label> : <linenum> format, which jump to the exact line.

\newcounter{lstlinenum}
\newenvironment{lst}[4][]
{
    \setcounter{lstlinenum}{0}
    \def\theFancyVerbLine{
        \rmfamily\tiny\arabic{FancyVerbLine}
        \refstepcounter{lstlinenum}
        \label{#4:\arabic{lstlinenum}}
    }
    \VerbatimEnvironment
    \begin{listing}[htp]
    \caption{#3}\label{#4}
    \begin{minted}[#1]{#2}%
}{
    \end{minted}
    \end{listing}
    \def\theFancyVerbLine{\rmfamily\tiny\arabic{FancyVerbLine}}%
}
candr
  • 21