I basically want every 5th line numbered, but would like to keep to 5,10,15,20,... as that seems intuitive to readers. So that means 1,5,10,15,... The first gap is 4, all subsequent ones are 5. Is there a way to do this? Thanks!
2 Answers
This can be done with a combination of firstnumber
and numberfirstline
:
\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,stepnumber=5,firstnumber=1,numberfirstline}
\begin{document}
\begin{lstlisting}
a
b
c
d
A
B
C
D
E
a
b
c
d
e
Q
\end{lstlisting}
\end{document}
yields:
[I assume you are using the listings
package.]
When you use the key, stepnumber=5
, the numbers are incremented in steps of 5. However, under that scenario, your numbers will come out to be 5, 10... Not the sequence you asked for. So, you will have to coax LaTeX to start your number from the very first line. You add numberfirstline,firstnumber=1
and for that purpose.
From the relevant documentation,
numberfirstline= true|false
Default: falseThe first line of each listing gets numbered (if numbers are on at all) even if the line number is not divisible by stepnumber.
firstnumber= auto|last| number
Default: auto
auto
lets the package choose the first number: a new listing starts with number one, a named listing continues the most recent same-named listing (see below), and a stand alone file begins with the number corresponding to the first input line. last continues the numbering of the most recent listing and number sets it to the number.
Anyway, here is your MWE.
\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{language=C,numberfirstline,firstnumber=1,numbers=left, stepnumber=5}
\begin{lstlisting}
#include <stdio.h>
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
printf("Integer that you have entered is %d\n", a);
return 0;
}
\end{lstlisting}
\end{document}
And here is the output,
I am not sure which language you are using, perhaps you would like to use,
basicstyle=\ttfamily
as well to make output more program like. (If you are not already using that.)