5

My verbatim environments are generated automatically. As a consequence some have empty first line - but not all. Is it possible to redefine verbatim env. and force it to remove first empty line if exists? I should have added that I must use verbatim and no other packages. Here is a sample

\documentclass{article}
\usepackage{verbatim}

\begin{document}

First line is empty in verb.
\begin{verbatim}

Empty line above.
3rd line.
\end{verbatim}

First line is not empty.
\begin{verbatim}
Empty line above?
3 line.
\end{verbatim}

\end{document}
2
  • (1) welcome, (2) the listings package with its emptylines option might be useful
    – daleif
    Jul 5, 2018 at 14:59
  • 1
    It looks like you've got two separate accounts, which means you cannot edit your original post or leave comments. The Stack Exchange staff can merge them together for you. Jul 5, 2018 at 17:53

2 Answers 2

4

With verbatim, check whether the first line is empty:

\documentclass{article}
\usepackage{verbatim}

\makeatletter
\newif\iffirstlinecheck
\renewcommand{\verbatim@processline}{%
  \iffirstlinecheck
    \if\relax\the\verbatim@line\relax
    \else
      \the\verbatim@line\par
    \fi
  \global\firstlinecheckfalse
  \else
    \the\verbatim@line\par
  \fi
}
\edef\verbatim{\global\noexpand\firstlinechecktrue\unexpanded\expandafter{\verbatim}}
\makeatother

\begin{document}

First line is empty in verb.
\begin{verbatim}

Empty line above.
3rd line.
\end{verbatim}

First line is not empty.
\begin{verbatim}
Empty line above?
3 line.
\end{verbatim}

\end{document}

Otherwise, with the more customizable fancyvrb, issue a negative spacing if the first line is empty:

\documentclass{article}
\usepackage{fancyvrb}

\newif\iffirstlinecheck
\renewenvironment{verbatim}
 {\VerbatimEnvironment\global\firstlinechecktrue
  \renewcommand\FancyVerbFormatLine[1]{%
    \iffirstlinecheck
      \if\relax\detokenize{##1}\relax
        \aftergroup\aftergroup\aftergroup\cancelline
      \fi
    \global\firstlinecheckfalse
    \fi
    ##1%
  }%
  \begin{Verbatim}%
 }
 {\end{Verbatim}}
\def\cancelline{\vspace{-\baselineskip}}

\begin{document}

First line is empty in verb.
\begin{verbatim}

Empty line above.
3rd line.
\end{verbatim}

First line is not empty.
\begin{verbatim}
Empty line above?
3 line.
\end{verbatim}

\end{document}

enter image description here

2

The verbatim-environment from the verbatim package does line-wise collect the material to typeset within a token register whose name is \verbatim@line. When the end of a line is reached, the content of that token register is typeset by the internal macro \verbatim@processline.

Therefore an approach to the matter could be to first copy/save the definition of \verbatim@processline as another control sequence, then redefine \verbatim@processline to set itself to the saved definition and to typeset the content of \verbatim@line only if \verbatim@line is not empty.

Setting of \verbatim@processline to the saved definition takes place inside the local scope opened up by the verbatim environment. Thus other instances of the verbatim environment will not be affected. Setting of \verbatim@processline to the saved definition takes place when \verbatim@processline is carried out the first time, i.e., when processing the very first line of the environment's body. Thus checking the line for emptiness does in any case affect only the very first line of the environment's body:

\documentclass{article}
\usepackage{verbatim}

\makeatletter
\newcommand\savedverbatim@processline{}%
\let\savedverbatim@processline=\verbatim@processline
\renewcommand{\verbatim@processline}{%
  \let\verbatim@processline=\savedverbatim@processline
  \if\relax\the\verbatim@line\relax
  \else
    \the\verbatim@line\par
  \fi
}%
\makeatother

\begin{document}

The first line of \verb|verbatim|'s body is not empty.
\begin{verbatim}
First Line
Second Line
Third Line
\end{verbatim}

The first line of \verb|verbatim|'s body is empty.
\begin{verbatim}

Second line.
Third line.
\end{verbatim}

Both the first and the second line of \verb|verbatim|'s body are empty.\\
Only the first empty line will be removed.
\begin{verbatim}


Third line.
\end{verbatim}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .