4

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!

1
  • How come this has an accepted answer when the question is clearly LyX oriented? None of the answers below, so far, has any hint for LyX. Nov 11, 2014 at 11:58

2 Answers 2

4

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:

listings

[I assume you are using the listings package.]

2

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: false

The 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,

enter image description here


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.)

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.