1

I have the following environment:

\documentclass{book}
\usepackage{fancyvrb}

\newenvironment{codesample}
{
\scriptsize
\begin{center}
\BVerbatim
}
{
\endBVerbatim
\end{center}
}

\begin{document}
\begin{codesample}
sdfsdfsdf
\end{codesample}
\end{document}

When I try to use I get error:

File ended while scanning use of \FancyVerbGetLine.

How can I achieve small and centered verbatim text with the environment? Like this:

enter image description here

2
  • 2
    Where do the \BVerbatim and \endBVerbatim macros come from? Please provide an MWE (from \documentclass... to \end{document}) that we can compile to try and solve your problem. Dec 27, 2020 at 19:00
  • @PeterWilson thank you, updated
    – Yola
    Dec 27, 2020 at 19:41

2 Answers 2

4

You should define the environment in a slightly different way: declaring \VerbatimEnvironment is the key.

\documentclass{book}
\usepackage{fancyvrb}

\usepackage{lipsum} % mock text

\newenvironment{codesample}
{\VerbatimEnvironment\begin{center}\begin{BVerbatim}[fontsize=\scriptsize]}
{\end{BVerbatim}\end{center}}

\begin{document}

\lipsum[1][1-4]
\begin{codesample}
sdfsdfsdf
\end{codesample}
\lipsum[1][5-7]

\end{document}

enter image description here

1

Your code within the definition of codesample worked well when used by itself. I don't know why there is a problem when used within codesample. Perhaps you can just use it without a surrounding environment.

% verbprob.tex  SE 576576

\documentclass{article}
\usepackage{fancyvrb}

\newenvironment{codesample}
{
%\scriptsize
\begin{center}
\scriptsize
% BVerbatim
\begin{BVerbatim}}
{%
%\endBVerbatim
\end{BVerbatim}
\end{center}
}

\begin{document}

\begin{center}
\scriptsize
\begin{BVerbatim}
x = false       // The other Boolean value
x = null
x = undefined  // Undefined is another special value like null
\end{BVerbatim}
\end{center}

Two other ...

\end{document}

\begin{codesample}
x = false
x = null
x = undefined
\end{codesample}

Two other ...
\end{document}

enter image description here

BTW you still didn't provide an MWE.

2
  • ? I have just copy-pasted the code from the question into overlead and the error is there.
    – Yola
    Dec 27, 2020 at 21:01
  • Sorry, can't accept an answer like this. But +1 for the try ;)
    – Yola
    Dec 27, 2020 at 21:03

You must log in to answer this question.

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