I dont think you can do without some manual labour, if you don't have a system of numbering. However you can trick listings
to use other values.
For reference it is the counter lstnumber
which holds the current line number.
So the solution is to do conditional checking with the counter and insert the correct format, or reference.
I have created a macro which collects information on the line number and inserts the correct format upon the line number reached.
% Save the original way of printing the number
\let\othelstnumber=\thelstnumber
\def\createlinenumber#1#2{
\edef\thelstnumber{%
\unexpanded{%
\ifnum#1=\value{lstnumber}\relax
#2%
\else}%
\expandafter\unexpanded\expandafter{\thelstnumber\othelstnumber\fi}%
}
\ifx\othelstnumber=\relax\else
\let\othelstnumber\relax
\fi
}
This macro will consecutively store any information that you wish to replace with the lstnumber
counter.
So calling:
\createlinenumber{105}{3.1}
\createlinenumber{107}{3.1.5}
will globally change lines 105 and 107 to 3.1, 3.1.5, respectively.
Thus the following
\documentclass{article}
\usepackage{listings}
\begin{document}
% Save the original way of printing the number
\let\othelstnumber=\thelstnumber
\def\createlinenumber#1#2{
\edef\thelstnumber{%
\unexpanded{%
\ifnum#1=\value{lstnumber}\relax
#2%
\else}%
\expandafter\unexpanded\expandafter{\thelstnumber\othelstnumber\fi}%
}
\ifx\othelstnumber=\relax\else
\let\othelstnumber\relax
\fi
}
\bgroup
\createlinenumber{105}{3.1}
\createlinenumber{107}{3.1.5}
\begin{lstlisting}[numbers=left,firstnumber=100,numberstyle=\ttfamily]
begin { empty lines }
end; { empty lines }
\end{lstlisting}
\egroup
\end{document}
will produce:
Notice the use of \bgroup
and \egroup
to not being globally changed.
Only manually selected lines
If you do not wish to display any other than yours provided you simply need to tell TeX
to discard those. Notice that you cannot use numbers=none
as that will never execute \thelstnumber
.
Thus the code looks like this:
\def\createlinenumber#1#2{
\edef\thelstnumber{%
\unexpanded{%
\ifnum#1=\value{lstnumber}\relax
#2%
\fi}%
\ifx\thelstnumber\relax\else
\expandafter\unexpanded\expandafter{\thelstnumber}%
\fi
}
}
However the above requires an initialisation before first use. You then have to add: \let\thelstnumber\relax
after the first \bgroup
but before the first execution of \createlinenumber
.
As bonus info. This was originally intended for a friends thesis who would love to have 007
printed on page 7
. Hence I of course used it on \thepage
. :)
I just expanded it to do other things as well.
The listings
code to write the line numbers is stored in \lst@PlaceNumber
. Under numbers=left
it is defined as:
\def\lst@PlaceNumber{\llap{\normalfont
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}}%
which prints the number as a l
eft overlap
, causing the "problems" you experience. You could just push the entire lstlisting
over by at least the above amount (say, 1em+\lst@numbersep
) to allow the left overlap to still be within the text block boundary:
\documentclass[twocolumn]{article}
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\begin{figure*}
\makeatletter%
\hspace*{\dimexpr 1em+\lst@numbersep}%
\makeatother%
\begin{minipage}{.4\textwidth}
\begin{lstlisting}[numbers=left]
i = 0;
j = 1;
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.4\textwidth}
\includegraphics{figure}
\end{minipage}
\caption{My caption}
\label{fig:blah}
\end{figure*}
\end{document}
I've added showframe
to highlight the text block boundary, and the [demo]
option to graphicx
in order to run the MWE. They are not needed in your final document.
Alternatively you can set the numbers to be shown left globally (using \lstset{numbers=left}
) and then redefine \lst@PlaceNumber
accordingly:
\makeatletter%
\def\lst@PlaceNumber{\makebox[\dimexpr 1em+\lst@numbersep][l]{\normalfont
\lst@numberstyle{\thelstnumber}}}%
\makeatother%
The global setting is required in order to have the redefinition last beyond \begin{lstlisting}
.
Best Answer
Change your definition to
Full code: