TransWikia.com

How to center verbatim in environment

TeX - LaTeX Asked on June 12, 2021

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 Answers

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}
{VerbatimEnvironmentbegin{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

Correct answer by egreg on June 12, 2021

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.

Answered by Peter Wilson on June 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP