This is an alternative. Numbers are still typesetted as usual, but instead of typesetting it before each line is typesetted, all line numbers are typesetted after the whole code block is typesetted.
If the PDF viewer respect the "text order" in the PDF, it should work fine.
Needs 2 compilation passes.
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
\NewDocumentCommand\fakeTypesetLineNumber{m}{%
\tikz[remember picture] \coordinate (line\the\value{lstnumber});% remember this location
% --
\tiny\phantom{#1}% leave space equal to the line number itself
% \tiny must be outside any group so that it applies to the following \kern
% --
\xappto\typesetPendingLineNumbers{\actualTypesetLineNumber{\the\value{lstnumber}}}% remember to typeset it later
}
% this command must be robust because it's used inside \xappto
\NewDocumentCommand\actualTypesetLineNumber{m}{%
\node [anchor=south west, inner sep=0pt] at (line#1){\normalfont\tiny#1};% actually typeset it now, need to copy the \tiny here
}
\AddToHook{env/lstlisting/begin}{%
\gdef\typesetPendingLineNumbers{}%
}
\AddToHook{env/lstlisting/end}{%
\begin{tikzpicture}[remember picture, overlay]%
\typesetPendingLineNumbers
\end{tikzpicture}%
}
\lstset
{
language={[LaTeX]TeX},
numbers=left,
numbersep=1em,
numberstyle=\fakeTypesetLineNumber,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
breaklines=true,
basicstyle=\small\tt,
keywordstyle=\color{blue},
commentstyle=\color[rgb]{0.13,0.54,0.13},
backgroundcolor=\color{yellow!10},
tabsize=2,
columns=flexible,
morekeywords={maketitle},
escapeinside={%LISTING:}{^^M},
}
\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle %LISTING:\label{title}
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}
Line \ref{title} shows the title.
\lstset{ numberstyle=\tiny }
\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle %LISTING:\label{title2}
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}
\end{document}
Of course, this will fail silently for code blocks that span across more than one page.
listingscounterpart for\thelinenumber. – kiss my armpit May 24 '12 at 06:06accsupppackage as mentioned in the question @Marco linked to, and use\renewcommand*\thelstnumber{}to change how the line numbers are formatted. – Peter Grill May 24 '12 at 06:07lstaddonsbundle. – Martin Scharrer May 24 '12 at 08:26lstaddons, BTW is the feature mentioned in the question implemented in yourlstaddons. (suggestion: Another extension can be to have to have a filename in the margin, e.g. next to the line numbers) – alfC Sep 13 '13 at 07:53gobbleoption fromlstaddonsallows to remove whitespaces in front of every line in the verbatim code. This way the verbatim code can be indented. – Martin Scharrer Sep 14 '13 at 17:38