I was wondering whether I could use the labeling mechanism explained here to get the line numbers that I want to use for highlighting code via highlightlines in minted.

I am imagining something like this (Modified example from https://tex.stackexchange.com/a/483714/85983):

\begin{minted}[linenos=true, escapeinside=!!, highlightlines=\ref{myline}]{c++}
    i = i + 1 ;  
    j = j + 1 ; !\label{myline}!
    k = k + 1 ;
The important line is line \ref{myline}.

In the resulting output the line j = j + 1 ; should be highlighted.

1 Answer 1


Both \ref and \pageref are not fully expandable, but refcount package provides a \getrefnumber{<label name>} which fully expands to the linked counter value of that <label name>. Hence your requirement can be implemented by:


% expand value of key "highlightlines"

    highlightlines={\getrefnumber{myline}, \getrefnumber{yourline}}
    i = i + 1 ;  
    j = j + 1 ; !\label{myline}!
    k = k + 1 ;
    l = l + 1 ; !\label{yourline}!
The important lines is line \ref{myline} and line \ref{yourline}.


enter image description here

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.