Text Formatting'listings' package line numbering problems

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ckambler
Posts: 1
Joined: Thu Jul 30, 2009 9:37 pm

'listings' package line numbering problems

Post by ckambler »

So i've been using the listings package to include some C and MATLAB source code in my document. The code is fairly long, so i'd like to have line numbers along the left margin. I only need them every 5th line or so. Something like 5,10,15,20. Nice and even looking. The problem is, if i set stepnumber=5, it doesn't number them this way. It gives me 1,6,11,16,21, etc. I know it's a relatively minor issue, but i was wondering if anyone knows how to fix this. Also, for some reason setting numberfirstline=false does not seem to do anything. no matter what, it will put a number on line 1.

Any thoughts?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

'listings' package line numbering problems

Post by gmedina »

Hi,

yes, there seems to be some bug with the numberfirstline option. (I do not know if it is really a bug or if it was planned to work that way, and the documentation doesn't clarifies this). The following test code will produce (as you already experienced) a numbered first line:

Code: Select all

\documentclass{article}
\usepackage{listings}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=5,%
numberfirstline=false, numbersep=5pt}

\begin{document}

The first line will be numbered:
\begin{lstlisting}[name=Test]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}

\end{document}
However, adding something like firstnumber=1, the numberfirstline=false option works as expected:

Code: Select all

\documentclass{article}
\usepackage{listings}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=5,%
numberfirstline=false, numbersep=5pt, firstnumber=1}

\begin{document}

The first line won't be numbered:
\begin{lstlisting}[name=Test]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply