4

I'd like to include programming codes in LaTeX by using the verbatim package. For the code I use, the enumeration is continuous. So if a new code starts, the numbers are ongoing, which should not be the case. The use of the linenumbers package did not help.

Further I'd like to have a vertical line between the numbers and the code to indicate the left margin.

I have attached the code.

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

Thanks.

strpeter
  • 5,215
Matthias
  • 41
  • 2

3 Answers3

2

My verbatimbox package can do this. However, I had to renew the verbnobox environment in order to not reset the line number counter (I'll be fixing this in a future release of the package). To reset the line number manually, \setcounter{VerbboxLineNo}{-1}.

In this MWE, I set the line number in \scriptsize and the code in \small, both of which can be altered in the definition of \tmp. The variable which I define as \tmp is key to setting the content and appearance of the verbatim as well as its marginal heading.

EDITED to guarantee that new environment picks up on sequential line number where count was last left off.

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

enter image description here

1

Use the features provided by 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}

enter image description here

Adding a rule is more complicated.

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

enter image description here

For restarting the numbering, call

\begin{NVerbatim}[firstnumber=1]
egreg
  • 1,121,712
  • thanks @egreg, do you know how to add the vertical line between the code and the numbers ? (2nd part of the question) – Matthias Sep 04 '15 at 03:18
1

If you do not restrict yourself only to the verbatim package, you can alternatively use the package listings which allows you to use code line enumeration automatically.

For this package the same question has been raised and answered successfully: What can I use to typeset MATLAB code in my document?

Minimal working code example (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}

Output result

strpeter
  • 5,215