更改\lstinputlisting 上的起始号码

更改\lstinputlisting 上的起始号码

我正在尝试将一些 Matlab 代码的起始数字从 1 更改为 236。我尝试使用,firstline=236但编译后没有任何输出。

您能帮我将代码编号从 236 开始吗?

以下是我目前所掌握的信息:

\documentclass{book}

\usepackage{listings}
\usepackage{xcolor}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.m}
 % detect if next step is an obstacle
                if bug.isoccupied(robot + [dx; dy])
                    bug.message('(%d,%d) obstacle!', n);
                    bug.H(bug.j,:) = robot; % define hit point
                    bug.step = 2;
                    % get a list of all the points around the obstacle
                    bug.edge = edgelist(bug.occgridnav == 0, robot);
                    %bug.k = 2;  % skip the first edge point, we are already there
                    bug.k = size(bug.edge,2)
                else
                    n = robot + [dx; dy];
                end
            end % step 1
\end{filecontents*}


\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\ttfamily\small,
numbersep=22pt,
frame=none
}


\begin{document}


\lstinputlisting[%firstline = 236,
backgroundcolor=\color{blue!10},
style=Matlab-editor,
basicstyle=\mlttfamily\scriptsize,
caption={[First Change to the Bug2 Algorithm]{First Change to the Bug2 Algorithm}},
label = mat:line246]{\jobname.m}

\end{document} 

答案1

正确的选项是firstnumber=236

请参阅以下代码:

\documentclass{book}

\usepackage{listings}
\usepackage{xcolor}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.m}
 % detect if next step is an obstacle
                if bug.isoccupied(robot + [dx; dy])
                    bug.message('(%d,%d) obstacle!', n);
                    bug.H(bug.j,:) = robot; % define hit point
                    bug.step = 2;
                    % get a list of all the points around the obstacle
                    bug.edge = edgelist(bug.occgridnav == 0, robot);
                    %bug.k = 2;  % skip the first edge point, we are already there
                    bug.k = size(bug.edge,2)
                else
                    n = robot + [dx; dy];
                end
            end % step 1
\end{filecontents*}


\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\ttfamily\small,
numbersep=22pt,
frame=none
}


\begin{document}


\lstinputlisting[%
  %firstline = 236,
  firstnumber=236, % <==================================================
  backgroundcolor=\color{blue!10},
  style=Matlab-editor,
  basicstyle=\mlttfamily\scriptsize,
  caption={[First Change to the Bug2 Algorithm]{First Change to the Bug2 Algorithm}},
  label = mat:line246
]{\jobname.m}

\end{document}

结果:

enter image description here

相关内容