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:
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}
A slightly different approach from Heiko's fine answer:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstnewenvironment{bash}[1][]
{\lstset{language=C}\lstset{%
numbers=left,numberstyle=\normalsize,stepnumber=1,numbersep=5pt,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape\color{blue},
stringstyle=\color{maroon},
commentstyle=\color{black},
xleftmargin=5pt,
xrightmargin=5pt,
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
backgroundcolor=\color{gray!20}, #1
}}
{}
%
%%% Always I forget this so I created some aliases
\def\ContinueLineNumber{\lstset{firstnumber=last}}
\def\StartLineAt#1{\lstset{firstnumber=#1}}
\let\numberLineAt\StartLineAt
\begin{document}
\begin{bash}[name=Test]
echo xyz
\end{bash}
\StartLineAt{30}
\begin{bash}[name=Test]
echo xyz
\end{bash}
\ContinueLineNumber
\begin{bash}[name=Test]
echo xyz
\end{bash}
\end{document}
The code offers two macros that I personally find convenient
\ContinueLineNumber
and
\StartLineAt
If you wouldn't like all your code to have consecutive numbering you might find it useful.
Best Answer
The simplest way to do this is to use the formatting commands
\Indm
and\Indp
to decrease and increase the indentation. Used around the input/output commands makes these lines flushleft, just like the subsequent line numbers. The example below shows this in action in a cut-down version of the second example from the manual.For more general indentation changes there is
\Indentp
, which is defined in the style class, but not mentioned in the manual.\Indm
is equivalent to\Indentp{-1em}
,\Indp
is\Indentp{1em}
. There is also\Indmm
and\Indpp
which adjust the indentation by0.5em
. You need an extra0.5em
for each extra digit of line number. In particular, if you have two digit line numbers you can use\Indm\Indmm
as follows:There is no internal variable storing the width of the line numbers. The package uses
\llap
to them in a box of width zero, with the numbers plus their space from the algorithm sticking out to the right of this box. As the examples in the documentation indicate, it is up to the user to adjust the margins to suit the given algorithm.