4

This may also be a potentially trivial question, but did not find the answer. Edit: It may be nontrivial...

...

\usepackage{lineno}

...

\begin{linenumbers}
\begin{verbatim}

A
B

C

\end{verbatim}
\end{linenumbers}

...

Will produce of course

1
2 A
3 B
4
5 C
6

But what is preferred is instead


1 A
2 B
3
4 C

such that either the first line is number is 0 and invisible, and the same for the last line number, or simply numbering begins from line 2 and concludes before the last line.

Question: What options, what things can I redefine such can I set to make this happen?

Edit. The question is not about adding line numbers to verbatim. Many ways to do that. Nor commenting. Issue is that it is not clear how to redefine, minimally, the environment or create one such that the 1st, Nth, Tth, Zth, ..., line whether or not it has text in it are skipped in numbering. To be set in the definition before the document begins. Same every time the environment is called.

2
  • "Will produce of course"... No, it doesn't, unless you have some code you are withholding.
    – campa
    Feb 8 '20 at 12:20
  • Ty for noticing - fixed. See revised example: The point is there may be a space or a comment or something else, a :) or :( or... Also possibly at the end, and possibly in the middle, in between say lines 20 and 30 is always 10 lines of commentary that should not be numbered. The goal is to be able to skip numbering anything like that. How? Feb 9 '20 at 3:27
3
+50

I think for this task it is easier to drop the lineno package and implement the line number typesetting with the verbatim package. This is based on an example from the verbatim package documentation, p. 5. Edit: I modified the solution to resume the numbering from before it was switched off; I assume that's the behavior you want.

\usepackage{verbatim}

\makeatletter
\newif\ifprintlineno
\newcounter{reallineno} % the line number in the input
\newcounter{lineno} % the line number displayed

\newtoks\testlinenotoks
\testlinenotoks={\printlinenotrue}
\def\testlineno{\the\testlinenotoks}

\def\verbatim@processline{%
  \addtocounter{reallineno}{1}
  \testlineno
  \ifprintlineno
    \addtocounter{lineno}{1}
    \leavevmode\llap{\scriptsize\thelineno\hskip1em\hskip\@totalleftmargin}%
    \the\verbatim@line\par
  \else\the\verbatim@line\par\fi
}
\edef\verbatim@finish{\verbatim@finish
  \setcounter{lineno}{0}\setcounter{reallineno}{0}}
\makeatother

\newcommand\SuppressLineNoFromTo[2]{%
  \testlinenotoks=\expandafter{\the\testlinenotoks
  \ifnum\value{reallineno}>\numexpr#1-1\relax
  \ifnum\value{reallineno}<\numexpr#2+1\relax
    \printlinenofalse\fi\fi}}
\newcommand\SuppressLineNoAt[1]{%
  \testlinenotoks=\expandafter{\the\testlinenotoks
  \ifnum\value{reallineno}=#1
  \printlinenofalse\fi}}
\newcommand\ClearSuppressLineNo{\testlinenotoks={\printlinenotrue}}

This defines three commands:

  • \SuppressLineNoFromTo will suppress the line numbering from line number #1 to line number #2 in all verbatim environments
  • \SuppressLineNoAt will suppress the line number at line #1 only
  • \ClearSuppressLineNo clears all the rules defined with the two commands above if you want to use different sets of rules with different environments

You can combine them in various ways. The command \verbatim@processline handles how each line in a verbatim environment is typeset, and the \llap part sets the line number into the margin, so adaptations should go there.

Here is an example assuming the above setup:

\SuppressLineNoFromTo{5}{10}
\SuppressLineNoFromTo{15}{16}
\SuppressLineNoAt{1}

\begin{document}

\begin{verbatim}

A
B

C


D
E
F
G
H
I
J


\end{verbatim}
\end{document}

example


Old answer:

I don't know if this is what you are looking for, but with this setup here

\usepackage{lineno}
\usepackage{verbatim}

{\catcode`\%=13
\gdef\getnextchar#1\par{\setbox0 \hbox{\ifx%#1\gdef\nextchar{c}\else\ifx!#1\gdef\nextchar{!}\else\gdef\nextchar{?}\fi\fi}}
\gdef\ignorenext#1{}
\gdef\recat{\catcode`\%=13
  \long\def%##1\par{\getnextchar##1\par\if\nextchar c\%\fi\if\nextchar!\%\ignorenext##1\par\else
    {\nolinenumbers##1\par}\fi}}
\globaldefs=1 
\newenvironment{myverbatim}{\verbatim\recat%}{\endverbatim}}

you get an environment myverbatim within which the following works:

  • a comment character % is invisible in the output and suppresses the line number of the current line
  • the combination %% creates a verbatim comment that will be visible in the output, the line will not have a number
  • the combination %! creates a comment where the line will have a number

Example:

\documentclass{article}
\usepackage{lineno}
\usepackage{verbatim}

{\catcode`\%=13
\gdef\getnextchar#1\par{\setbox0 \hbox{\ifx%#1\gdef\nextchar{c}\else\ifx!#1\gdef\nextchar{!}\else\gdef\nextchar{?}\fi\fi}}
\gdef\ignorenext#1{}
\gdef\recat{\catcode`\%=13
  \long\def%##1\par{\getnextchar##1\par\if\nextchar c\%\fi\if\nextchar!\%\ignorenext##1\par\else
    {\nolinenumbers##1\par}\fi}}
\globaldefs=1 
\newenvironment{myverbatim}{\verbatim\recat%}{\endverbatim}}

\begin{document}
\begin{linenumbers}
\begin{myverbatim}
%
A
B
%this input will be visible, the line does not have a number
just like this one %
C
%% this comment will show up, without line number
D %% like this one

E
%! this comment has a line number
F %! like this one
G

H
%
%
\end{myverbatim}
\end{linenumbers}
\end{document}

old example

2
  • Not actually looking for commenting necessarily, but to skip lines by predetermined number set in the definition. Still useful hence upvoting. But actually looking to set, at the start of document that every time a verbatim line numbered environment is called the Nth, Tth, Zth, ..., lines have no numbers attached. Mar 24 '20 at 15:14
  • @GottfriedWilliam I have updated my answer, is that what you need?
    – wave
    Mar 24 '20 at 17:14
1

Updated

A fancyvrb approach.

  • The implementation is a bit long, but a fairly part of code is to parse the comma-separated number ranges.
  • A new fancyvrb option ignorenumberlines is provided, having the similar usage to highlightlines provided by fvextra package.
\documentclass{article}
\usepackage{etoolbox, fancyvrb} % or load package "fvextra" solely


\makeatletter

%% new fv option "ignorenumberlines", similar to "highlightlines" in fvextra pkg
\define@key{FV}{ignorenumberlines}{\def\FV@IgnoreLinesList{#1}}
\fvset{ignorenumberlines=}



%% parsing \FV@IgnoreLinesList, similar to "highlightlines"-related codes
%% For each line number <n>, let \FV@IgnoreLine:<n> to \relax.
\def\FV@IgnoreLinesPrep{%
  \ifx\FV@IgnoreLinesList\@empty
  \else
    \expandafter\FV@IgnoreLinesPrep@i
  \fi}

\def\FV@IgnoreLinesPrep@i{%
  \renewcommand{\do}[1]{%
    \ifstrempty{##1}{}{\FV@IgnoreLinesParse##1-\FV@Undefined}}%
  \expandafter\docsvlist\expandafter{\FV@IgnoreLinesList}}
\def\FV@IgnoreLinesParse#1-#2\FV@Undefined{%
  \ifstrempty{#2}%
   {\FV@IgnoreLinesParse@Single{#1}}%
   {\FV@IgnoreLinesParse@Range{#1}#2\relax}}

\def\FV@IgnoreLinesParse@Single#1{%
  \expandafter\let\csname FV@IgnoreLine:\detokenize{#1}\endcsname\relax}

\newcounter{FV@IgnoreLinesStart}
\newcounter{FV@IgnoreLinesStop}

\def\FV@IgnoreLinesParse@Range#1#2-{%
  \setcounter{FV@IgnoreLinesStart}{#1}%
  \setcounter{FV@IgnoreLinesStop}{#2}%
  \stepcounter{FV@IgnoreLinesStop}%
  \FV@IgnoreLinesParse@Range@Loop}

\def\FV@IgnoreLinesParse@Range@Loop{%
  \ifnum\value{FV@IgnoreLinesStart}<\value{FV@IgnoreLinesStop}\relax
    \expandafter\let\csname FV@IgnoreLine:\arabic{FV@IgnoreLinesStart}\endcsname\relax
    \stepcounter{FV@IgnoreLinesStart}%
    \expandafter\FV@IgnoreLinesParse@Range@Loop
  \fi}

\ifcsname FV@FormattingPrep@PreHook\endcsname % defined if fvextra is loaded
  \expandafter\g@addto@macro\expandafter\FV@FormattingPrep@PreHook
\else
  \expandafter\g@addto@macro\expandafter\FV@FormattingPrep
\fi
{\FV@IgnoreLinesPrep\setcounter{FancyVerbLineOffset}{0}}



\newcounter{FancyVerbLineOffset}

%% redefine
\def\FV@ListProcessLine#1{%
  \hbox to \hsize{%
    \kern\leftmargin
    \hbox to \linewidth{%
      %% skip ignored line number
      \ifcsname FV@IgnoreLine:\number\c@FancyVerbLine\endcsname
        \addtocounter{FancyVerbLineOffset}{-1}%
        \FV@LeftListFrame
        \FancyVerbFormatLine{#1}\hss
        \FV@RightListFrame
      \else
        \FV@LeftListNumber
        \FV@LeftListFrame
        \FancyVerbFormatLine{#1}\hss
        %% DG/SR modification begin - Jan. 28, 1998 (for numbers=right add-on)
        %%      \FV@RightListFrame}%
        \FV@RightListFrame
        \FV@RightListNumber
        %% DG/SR modification end
      \fi
    }%
    \hss}}

\renewcommand{\theFancyVerbLine}{%
  \@arabic\numexpr\c@FancyVerbLine+\c@FancyVerbLineOffset\relax
}

\makeatother


\begin{document}
\begin{Verbatim}[numbers=left, ignorenumberlines={1-2}]
A1
A2
A3
A4
\end{Verbatim}

\begin{Verbatim}[numbers=right, ignorenumberlines={2,4}]
B1
B2
B3
B4
\end{Verbatim}
\end{document}

fancyvrb approach to skip selected line numbers

Original answer

Your incomplete example indicates that you want to use a verbatim-like environment, with line numbers at the left side.

Therefore I recommend using verbatim-specific packages, for example listings or fancyvrb. Both of them have rich configurable options. Pick one of them but not both.

(Personally I prefer fancyvrb, for its potential to use full-functional code highlighting extended by package minted.)

\documentclass{article}
\usepackage{listings}
\usepackage{fancyvrb}


\begin{document}
\verb|listings| example
\lstset{basicstyle=\ttfamily, numbers=left, numberstyle=\tiny}
\begin{lstlisting}
A
B

C
\end{lstlisting}


\verb|fancyvrb| example
\fvset{numbers=left}
\begin{Verbatim}
A
B

C
\end{Verbatim}
\end{document}

left line numbers using listings or fancyvrb

PS: lineno package is mostly used for printing line numbers for every line of a document, so that the proofreading can use "change xxx on page n, line m" to refer to a specific document position.

4
  • I am actually looking to set, at the start of document that every time a verbatim line numbered environment is called the Nth, Tth, Zth, ..., lines have no numbers attached. Not the numbering itself which is ok. It is one of those customization problems where no package really allows doing it. Requires a redefine or renew of something . . . Mar 24 '20 at 15:24
  • @GottfriedWilliam I have put my understanding to my answer. Want to know if i understand your requirement correctly. Mar 24 '20 at 19:12
  • "Want to know if i understand your requirement correctly." Yes. The other answer also posted a solution meanwhile. But if you know how to do it alternatively with fancyvrb that is also good to know. Knowing many different ways to do something is always useful. Mar 24 '20 at 23:52
  • @GottfriedWilliam Answer updated. Hope that helps. Mar 25 '20 at 1:50
0

minted do that by default. You can find some info about this package in this question, the main "difficulty" is that it requires the --shell-escape option to compile your document (look at this question to learn how to automate this process), and some external packages to be installed (nothing too difficult, but that can be a barrier to adopting it).

The following document:

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
%  Need to be compiled with the --shell-escape option!
% E.g., pdflatex --shell-escape test.tex 
\documentclass{article}
\usepackage{minted}

\begin{document}
% I'm assuming you want to highlight text.
% For a list of possible languages, refer to
% minted's documentation, or open a command-line
% interface and run
%  pygmentize -L

\begin{minted}[linenos]{text}

A
B

C


\end{minted}
\end{document}

will produce

enter image description here

You can even specify at which number the numbering should begin, with

\begin{minted}[linenos, firstnumber=3]{text}

A
B

C


\end{minted}

which will produce

enter image description here

If you want to "pick by hand" the line numbers, that's possible, but difficult.

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.