1

I wanted to create a consistent setting for Verbatim so I decided to create a macro out of this:

\newcommand*{\VerbatimCustom}{
\begin{Verbatim}[numbers=left,xleftmargin=5mm]
}

And use it like this:

\begin{figure}[htbp]
    \centering
\VerbatimCustom
module GRAMMAR[] begin
  phylum Grammar;

  phylum Item;
  phylum Items := SEQUENCE[Item];

  phylum Production;
  phylum Productions := SEQUENCE[Production];

  constructor terminal(s: Symbol) : Item;
  constructor nonterminal(s: Symbol) : Item;
  constructor prod(nt: Symbol; children: Items) : Production;
  constructor grammar(prods: Productions) : Grammar;

  pragma root_phylum(type Grammar);
end;    
\end{Verbatim}
    \caption{Structure of a context-free grammar in APS}
    \label{fig:cfg-structure}
\end{figure}

Error:

Extraneous input ` ' between \begin{Verbatim}[<key=value>] and line end . \FV@Error ... {FancyVerb Error: \space \space #1 } l.38 \VerbatimCustom This input will be discarded. Hit to continue.

I appreciate any help or hint.

1
  • 3
    as @Ulrich shows, you can make this work but I'd strongly advise that you don't do that, the result is then working tex code but you have mis-matched \end in the document, This looks weird to any human reading the source and will most lilley confuse editors and syntax checkers and probably latex-to-anything convertors. There is no need to do this at all as you can use the fancyvrb definition forms to define a custom environment that includes your common options. Oct 18 at 6:47
2

This answer is intended to explain the coming-into-being of the error-message.
It is not intended to provide a fix/workaround obeying good-practice-methods.
Resolving the issue according to good-practice-methods via \RecustomVerbatimEnvironment is shown in egreg's answer.
Reasons for obeying good-practice-methods are given in David Carlisle's comment.


TeX's eyes do pre-process .tex-input line by line:

  • All characters of the line are converted to TeX's internal character-representation scheme which either is ASCII or is Unicode.
  • All space-characters at the right end of the line are removed.
  • A character is appended at the end of the line whose code-point-number in TeX's internal character-representation scheme equals the value of the integer-parameter \endlinechar.
  • Then characters of the line get tokenized "on demand", i.e., whenever TeX's gullet needs tokens some of the characters get tokenized.

With the definition

\newcommand*{\VerbatimCustom}{  %<- spurious space token
\begin{Verbatim}[numbers=left,xleftmargin=5mm]   %<- spurious space token
}

spurious space-tokens come into being due to the \endlinechar-mechanism and \endlinechar at the time of tokeizing the definition having the value 13, denoting the carriage-return-character, and the carriage-return-character having catcode 5(return) which in turn implies the coming into being of

  • no token at all if TeX's reading apparatus is in state "skipping blanks",
  • an explicit space-token if TeX's reading apparatus is in state "middle of line",
  • the control-word-token \par if TeX ´'s reading apparatus is in state "new line".

Afaik fancyvrb for its environments changes the catcode of \endlinechar from 5(return) to 12(other) so that in any case at the end of lines carriage-return-character-tokens of catcode 12(other) come into being.

Thus if you would do \begin{Verbatim}[numbers=left,xleftmargin=5mm] directly, right behind the last token of the last argument of the environment there would not be a space-token but there would be a carriage-return-character-token of catcode 12(other) denoting the end of the line.

But due to the sequence coming from a macro-definition which includes a space-token, there is a space-token between the last token of the last argument of the environment and the carriage-return-character-token of catcode 12(other) denoting the end of the line.

The Verbatim-environment is implemented to raise an error-message if something is found between the last token of the last argument of the environment and the carriage-return-character-token of catcode 12(other) denoting the end of the line holding that last token.

Preventing the coming-into-being of these space-tokens by adding comment-chars at line-endings yields:

\documentclass{article}
\usepackage{fancyvrb}
\newcommand*{\VerbatimCustom}{%%%%%%
 \begin{Verbatim}[numbers=left,xleftmargin=5mm]%%%%%%
}
\begin{document}

\begin{figure}[htbp]
    \centering
\VerbatimCustom
module GRAMMAR[] begin
  phylum Grammar;

  phylum Item;
  phylum Items := SEQUENCE[Item];

  phylum Production;
  phylum Productions := SEQUENCE[Production];

  constructor terminal(s: Symbol) : Item;
  constructor nonterminal(s: Symbol) : Item;
  constructor prod(nt: Symbol; children: Items) : Production;
  constructor grammar(prods: Productions) : Grammar;

  pragma root_phylum(type Grammar);
end;    
\end{Verbatim}
    \caption{Structure of a context-free grammar in APS}
    \label{fig:cfg-structure}
\end{figure}

\end{document}

enter image description here

When compiling the example, I don't get any error-messages.


This answer is intended to explain the coming-into-being of the error-message.
It is not intended to provide a fix/workaround obeying good-practice-methods.
Resolving the issue according to good-practice-methods via \RecustomVerbatimEnvironment is shown in egreg's answer.
Reasons for obeying good-practice-methods are given in David Carlisle's comment.

2

If you want that all Verbatim environments follow the specification

numbers=left,xleftmargin=5mm

then the best way is to issue

\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{numbers=left,xleftmargin=5mm}

Full example. Look inside it for more comments.

\documentclass{article}
\usepackage{fancyvrb}

\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{numbers=left,xleftmargin=5mm}

\begin{document}

Some text before the verbatim environment.
Some text before the verbatim environment.
Some text before the verbatim environment.
Some text before the verbatim environment.

\begin{Verbatim}
This is {verbatim}
\another\line
\end{Verbatim}

You can also locally override a verbatim environment

\begin{Verbatim}[commandchars=\\\{\}]
This is almost verbatim in \LaTeX
\end{Verbatim}

\end{document}

enter image description here

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.