编程代码清单:枚举和左边距

编程代码清单:枚举和左边距

我想使用该verbatim包将编程代码包含在 LaTeX 中。对于我使用的代码,枚举是连续的。因此,如果开始新的代码,数字就会持续下去,但情况不应该如此。使用该linenumbers包没有帮助。

此外,我希望在数字和代码之间有一条垂直线来指示左边距。

我已附上代码。

\documentclass{article} 

\usepackage{listings}
\usepackage{fancyvrb}
\usepackage{lineno}

\begin{document} 
  \begin{linenumbers}
    \begin{verbatim}
    function I = simpsons(f,a,b,n)
    % This function computes the integral "I" via Simpson's rule in the
    % interval [a,b] with n+1 equally spaced points
    % 
    % Syntax: I = simpsons(f,a,b,n)
    % 
    % Where,
    %  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
    %  containing equally spaced values of the function to be integrated
    %  a= Initial point of interval
    %
    \end{verbatim}
  \end{linenumbers}
  NO NUMBERS SHOULD APPEAR HERE, THE NUMBERS SHOULD START IN THE NEXT LINE\\
  \begin{linenumbers}
    \begin{verbatim}
    function I = simpsons(f,a,b,n)
    % This function computes the integral "I" via Simpson's rule in the
    % interval [a,b] with n+1 equally spaced points
    % 
    % Syntax: I = simpsons(f,a,b,n)
    % 
    % Where,
    %  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
    %  containing equally spaced values of the function to be integrated
    %  a= Initial point of interval
    \end{verbatim}
  \end{linenumbers}
\end{document}

谢谢。

答案1

我的包可以做到这一点。但是,为了不重置行号计数器,verbatimbox我必须更新环境(我将在包的未来版本中修复此问题)。要手动重置行号,请。verbnobox\setcounter{VerbboxLineNo}{-1}

在这个 MWE 中,我在 中设置了行号\scriptsize,在 中设置了代码\small,这两者都可以在 的定义中更改\tmp。我定义为 的变量\tmp是设置逐字内容和外观及其边注标题的关键。

已编辑以保证新环境能够根据上次停止计数的连续行号进行拾取。

\documentclass{article}
\usepackage{verbatimbox}
\makeatletter
\setcounter{VerbboxLineNo}{-1}
\renewenvironment{verbnobox}{%
%  \setcounter{VerbboxLineNo}{-1}%
  \let\my@par\par%
  \def\verbatim@processline{%
    {\addtocounter{VerbboxLineNo}{1}%
    \@tmp\setbox0=\hbox{\@tmp\the\verbatim@line}%
    \hsize=\wd0 \the\verbatim@line\my@par}}%
\verbatim\verbbox@inner%
}
{%
  \endverbatim%
  \global\def\@tmp{}%
  \addtocounter{VerbboxLineNo}{-1}%
}
\makeatother
\begin{document}
\def\tmp{\makebox[0pt][r]{\scriptsize\theVerbboxLineNo\ \smash{%
  \rule[-1.4\dp\strutbox]{1pt}{1.4\baselineskip}}\ }\small}
\begin{verbnobox}[\tmp]
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the
% interval [a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
%
\end{verbnobox}
NO NUMBERS SHOULD APPEAR HERE, THE NUMBERS SHOULD START IN THE NEXT LINE
\begin{verbnobox}[\tmp]
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the  interval 
[a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
\end{verbnobox}
RESET LINE NUMBERS
\setcounter{VerbboxLineNo}{-1}
\begin{verbnobox}[\tmp]
Reset Line Numbers should appear here
\end{verbnobox}
\end{document}

在此处输入图片描述

答案2

使用提供的功能fancyvrb

\documentclass{article} 

\usepackage{fancyvrb}

\begin{document} 

\begin{Verbatim}[numbers=left]
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the
% interval [a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or avector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
%
\end{Verbatim}
NO NUMBERS SHOULD APPEAR HERE, THE NUMBERS SHOULD START IN THE NEXT LINE
\begin{Verbatim}[numbers=left,firstnumber=last]
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the  interval 
[a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
\end{Verbatim}

\end{document}

在此处输入图片描述

添加规则更加复杂。

\documentclass{article} 

\usepackage{fancyvrb}
\usepackage{etoolbox}
\makeatletter
\DefineVerbatimEnvironment{NVerbatim}{Verbatim}{%
  numbers=left,
  firstnumber=last,
  numbersep=4pt,
}
\preto\NVerbatim{\let\FV@Numbers@left=\matthias@Numbers@left}
\def\matthias@add@rule{%
  \ifx\FV@FontSize\relax\normalsize\else\FV@FontSize\fi
  \kern\FV@NumberSep\strut\vrule
}
\def\matthias@Numbers@left{%
  \let\FV@RightListNumber\relax
  \def\FV@LeftListNumber{%
  \@tempcnta=\FV@CodeLineNo
  \@tempcntb=\FV@CodeLineNo
  \divide\@tempcntb\FV@StepNumber
  \multiply\@tempcntb\FV@StepNumber
  \ifnum\@tempcnta=\@tempcntb
      \if@FV@NumberBlankLines
        \hbox to\z@{\hss\theFancyVerbLine\,\matthias@add@rule\kern\FV@NumberSep}%
      \else
        \ifx\FV@Line\empty
        \else
          \hbox to\z@{\hss\theFancyVerbLine\,\matthias@add@rule\kern\FV@NumberSep}%
        \fi
      \fi
  \fi}}
\makeatother

\begin{document} 

\begin{NVerbatim}
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the
% interval [a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or avector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
%
\end{NVerbatim}
NO NUMBERS SHOULD APPEAR HERE, THE NUMBERS SHOULD START IN THE NEXT LINE
\begin{NVerbatim}
function I = simpsons(f,a,b,n)
% This function computes the integral "I" via Simpson's rule in the
% interval [a,b] with n+1 equally spaced points
% 
% Syntax: I = simpsons(f,a,b,n)
% 
% Where,
%  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
%  containing equally spaced values of the function to be integrated
%  a= Initial point of interval
\end{NVerbatim}

\end{document}

在此处输入图片描述

要重新开始编号,请拨打

\begin{NVerbatim}[firstnumber=1]

答案3

如果您不局限于该包,您也可以使用允许您自动使用代码行枚举的verbatim包。listings

对于这个包,同样的问题已经提出并得到成功回答:我可以用什么来在我的文档中排版 MATLAB 代码?

最小工作代码示例(MWE)

\documentclass{article}

\usepackage{listings}

\lstset{language=Matlab,%
        numbers=left, % display line numbers at the left margin
        numberstyle={\tiny \color{black}}, % size of the numbers
        numbersep=9pt % this defines how far the numbers are from the text
}

\begin{document}

% Include Matlab file
\lstinputlisting{simpsons.m}

% Include Matlab code directly
\begin{lstlisting}
    function I = simpsons(f, a, b, n)
    % This function computes the integral "I" via Simpson's rule in the
    % interval [a,b] with n+1 equally spaced points
    % 
    % Syntax: I = simpsons(f,a,b,n)
    % 
    % Where,
    %  f= can be either an anonymous function (e.g. f=@(x) sin(x)) or a vector
    %  containing equally spaced values of the function to be integrated
    %  a= Initial point of interval
    %
\end{lstlisting}

\end{document}

输出结果

相关内容