2

I am using the fancyvrb package in LaTeX2e.

I have chosen to display line numbers as I am displaying some code.

(The code I am displaying is Maple, which unfortunately is not supported by the listings package).

As some of the lines of my code are too long to fit across the page, I break them manually in my .tex file.

I place a carriage return character at the end of this manual break to indicate to the reader that this line of code continues on the next line of displayed text. On the next line of text, I place an arrow symbol (left-to-right) to indicate to the reader that this line of text is a continuation of the line of code. (For completeness, the last line of text of such a broken line of code does not contain a carriage return character).

In such circumstances (i.e. of these manually-broken lines with explicit break and continuation characters), I would like to write a special numbering macro that appends a lower case letter to the line number assigned, (following an alphabetical sequence starting with a ), and ending at the last line of continuation.

Following such a broken line of code, the numbering for the next line of code would then resume normally and the lower case letter counter would reset to a.

I have looked carefully through the fancyvrb package documentation but to write such a macro is beyond me. I have also tried to find samples of macros performing similar tasks, however I have been unsuccessful.

Any help would be much appreciated(!)

Many thanks.

  • Can you show the code you have up to now? – egreg Jul 17 '13 at 19:02
2

This may not be exactly what you asked for, but perhaps you can adapt. In this case, a user-settable continuation char is placed in position 1 of a line if it is part of a split line, rather than at the end of a line and the beginning of the next line. The user may also set the label width of the numbering scheme.

This approach is a modification of the verbnobox environment of the verbatimbox package. Here, I call it fancynum.

\documentclass{article}
\usepackage{verbatimbox}
\usepackage{stringstrings}

\makeatletter
\newenvironment{fancynum}{%
  \setcounter{VerbboxLineNo}{-1}%
% FOR SOME REASON, USING \my@par INSTEAD OF \par PREVENTS EXTRA SPACE
% ABOVE verbbox WHEN USING OPTIONAL ARGUMENTS
  \let\my@par\par%
  \def\verbatim@processline{%
%   FIRST \@tmp APPLIES OPTIONAL ARGUMENT TO EACH VERBATIM LINE
    {\addtocounter{VerbboxLineNo}{1}%
    \@tmp\setbox0=\hbox{\the\verbatim@line}%
    \hsize=\wd0 \decidenow\the\verbatim@line\my@par}}%
\verbatim\verbbox@inner%
}
{%
  \endverbatim%
  \global\def\@tmp{}%
}
\newcounter{contindex}
\setcounter{contindex}{0}
\def\newvline{}
\def\decidenow{}
\def\contflag{F}
\def\decidenow{}

\newcommand\checkcontinued{%
  \isnextbyte[q]{\continuechar}{\the\verbatim@line}%
  \if T\theresult%
    \if T\contflag\addtocounter{VerbboxLineNo}{-1}\fi%
    \global\def\contflag{T}%
    \stepcounter{contindex}%
    \makebox[\vlabelwidth][l]{\arabic{VerbboxLineNo}\alph{contindex}:}%
    \global\def\decidenow{\expandafter\@gobble}
  \else%
    \global\def\contflag{F}%
    \setcounter{contindex}{0}%
    \makebox[\vlabelwidth][l]{\arabic{VerbboxLineNo}:}%
    \global\def\decidenow{}
  \fi%
}
%USER CAN SET THESE TWO VALUES
\def\continuechar{|}
\def\vlabelwidth{5ex}
\makeatother

\begin{document}

\begin{fancynum}[\checkcontinued]
first line \|$^%
|Second line
|coontinuation of Second line \|$^%
Third Line
Fourth Line
|Fifth Line
|continuation of Fifth Line
|continuation of Fifth Line
|continuation of Fifth Line
Sixth Line
Seventh Line
\end{fancynum}

\end{document}

enter image description here

  • Dear @Steven - this is just what I was looking for - thank you vey much! – pravda Aug 5 '13 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.