使用 listings 包的行号从随机数开始

使用 listings 包的行号从随机数开始

我正在使用listings包裹直接从文件中包含代码。在第一个例子中,我的代码按我想要的方式编号,第一行编号为 1,第 11 行编号为 11,...但是,第二个代码块在第二行编号为 2,第 12 行编号为 12,...然后第一个代码块在第 8 行编号为 8。

对于如何解决这个问题,有任何的建议吗?

这是我的代码:

\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}

答案1

如果您想要更常见的列表格式并对每行编号到第 10 行,则需要包含选项firstnumber=1以便每个列表从 1 开始。如果您还希望对第一行进行编号,那么您需要选项numberfirstline=true

以下是前后的比较:

在此处输入图片描述 在此处输入图片描述

笔记:

  • stepnumber仅当设置为 1 以外的其他值时,这才是必要的。
  • 您的代码无需进行此更改即可运行,但我还从language初始设置中删除了设置ltset,并将语言指定为可选参数的一部分\lstinputlisting(而不是使用lstset)。我认为这更清楚地体现了您想要的意图。

代码:

\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}

相关内容