16

I'm using the listings package to include code directly from a file. In the first instance my code is numbered as I want it 1 at the 1st line, 11 at the 11th, ... However, the second block of code it numbers 2 on the 2nd line, 12 on the 12th line, ... then the first numbering 8 on the 8th line.

Any suggestions on how to fix this?

Here is my code:

\documentclass{article}
\usepackage{listings, color, setspace, graphics}
\usepackage{fancyhdr, amsmath, amssymb, setspace, amssymb}
\usepackage{pdfpages}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{ %
  language=C++,                % the language of the code
  basicstyle=\footnotesize,           % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{gray},  % the style that is used for the line-numbers
  stepnumber=10,                   % the step between two line-numbers. If it's 1, each line 
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},      % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{blue},          % keyword style
  commentstyle=\color{dkgreen},       % comment style
  stringstyle=\color{mauve},         % string literal style
}

\renewcommand{\baselinestretch}{1}
\setlength{\textheight}{9in}
\setlength{\textwidth}{6.5in}

\setlength{\topmargin}{0in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\parindent}{.3in}

\pagestyle{fancy}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}
\setcounter{page}{11}
\lhead{John Haase}
\chead{AME 60614 Numerical Methods - Homework 2}
\rhead{\thepage}
\cfoot{}

\section*{Problem 1 Source Code}
\subsection*{C++ Code}
\lstset{language=C++}
\lstinputlisting{Problem1/Problem1.cpp}
\clearpage

\section*{Problem 2 Source Code}
\subsection*{C++ Code}
\lstset{language=C++}
\lstinputlisting{Problem2/Problem2.cpp}
\clearpage

\subsection*{MATLAB Code}
\lstset{language=Matlab}
\lstinputlisting{Problem2/Problem2.cpp}
\clearpage

\end{document}
3
  • 3
    This seems to be a bug in listings. But, are you sure you want lines numbered, 1,11,21?? A more usual numbering would be 1,10,20, etc??? if it is the later, you can do it with stepnumber=10, firstnumber=1, numberfirstline=true,. Oct 27, 2012 at 2:04
  • Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question.
    – Werner
    Oct 30, 2012 at 14:15
  • You might be interested in the matlab-prettifier package; see this answer.
    – jub0bs
    Apr 28, 2014 at 15:22

1 Answer 1

5

If you want the more usual format of listings and number every to 10th line, you need to include the option firstnumber=1 so that each listing begins from 1. If you also want the first line numbered, then you need the option numberfirstline=true.

Here is a before and after comparrison:

enter image description here enter image description here

Notes:

  • This appears to only be necessary if stepnumber is set to something other than 1.
  • Your code will work without this change, but I also removed the language setting form the initial ltset and specified the language as part of the optional parameter to \lstinputlisting (instead of using lstset). I think this more clearly captures the intent of what you want.

Code:

\documentclass{article}
\usepackage{listings, color, setspace, graphics}
\usepackage{fancyhdr, amsmath, amssymb, setspace, amssymb}
\usepackage{pdfpages}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\usepackage{filecontents}
\begin{filecontents*}{Problem1.cpp}
A1
B1
C1
D1
E1
F1
G1
A1
B1
C1
D1
E1
F1
G1
\end{filecontents*}
\begin{filecontents*}{Problem2.cpp}
A2
B2
C2
D2
E2
F2
G2
A2
B2
C2
D2
E2
F2
G2
\end{filecontents*}
\begin{filecontents*}{Problem3.cpp}
A3
B3
C3
D3
E3
F3
G3
A3
B3
C3
D3
E3
F3
G3
A3
B3
C3
D3
E3
F3
G3
A3
B3
C3
D3
E3
F3
G3
\end{filecontents*}


\lstset{%
%  language=C++,                % the language of the code
  basicstyle=\footnotesize,           % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{gray},  % the style that is used for the line-numbers
  stepnumber=10,                   % the step between two line-numbers. If it's 1, each line 
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},      % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{blue},          % keyword style
  commentstyle=\color{dkgreen},       % comment style
  stringstyle=\color{mauve},         % string literal style
  %
  firstnumber=1, numberfirstline=true,% <------- newly ad added lines
}

\renewcommand{\baselinestretch}{1}
\setlength{\textheight}{9in}
\setlength{\textwidth}{6.5in}

\setlength{\topmargin}{0in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\parindent}{.3in}

\pagestyle{fancy}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}
%\setcounter{page}{11}
\lhead{John Haase}
\chead{AME 60614 Numerical Methods - Homework 2}
\rhead{\thepage}
\cfoot{}

\section*{Problem 1 Source Code}
\subsection*{C++ Code}
%\lstset{language=C++}
\lstinputlisting[language=C++]{Problem1.cpp}
\clearpage

\section*{Problem 2 Source Code}
\subsection*{C++ Code}
%\lstset{language=C++}
\lstinputlisting[language=C++]{Problem2.cpp}
\clearpage

\subsection*{MATLAB Code}
%\lstset{language=Matlab}
\lstinputlisting[language=Matlab]{Problem3.cpp}
\clearpage

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .