# verbatim-environment inside group implies unwanted paragraph-break. Why? How to avoid?

When I wrap a verbatim-environment into an additional group, a new paragraph will be started after that group.
(In the example below, \parindent-glue is inserted before the YYY when \begingroup and \endgroup are there.)

Why? How can this be avoided?

\documentclass{article}
\parindent=2cm

\begin{document}

XXX\begin{verbatim}
\LaTeX
\LaTeX
\LaTeX
\end{verbatim}
YYY

\noindent\null\hrulefill\null

XXX\begingroup\begin{verbatim}
\LaTeX
\LaTeX
\LaTeX
\end{verbatim}
\endgroup YYY

\end{document}


• While the \parindent is not there in the first case, there is, it would seem, a \par of some sort. Just set \parskip=5ex, for example, to see where the paragraphs are inserted. That is to say, \endgroup\noindent would appear to give the same result as the 1st case, regardless of \parskip and \parindent. – Steven B. Segletes Mar 26 at 11:00
• @StevenB.Segletes In the article-class the verbatim-environment delivers the verbatimized-text as a \trivlist. This implies: In case the verbatim-environment itself is not the begin of a new paragraph, there is vertical \topsep-glue at the top and at the bottom of the verbatimized text. In case the verbatim-environment itself is the begin of a new paragraph, there is vertical \topsep-glue and vertical \partopsep-glue at the top and at the bottom of the verbatimized text. – Ulrich Diez Mar 26 at 11:15
• This looks like an XY-question. Why are you using the group to begin with? If it is for setting a different font size, then there are better methods. – egreg Mar 26 at 11:16
• @egreg I'm about to write an environment which does read its body under verbatim-catcode-régime and write it to .aux-file, wrapped into another environment which also does read its body under verbatim-catcode-régime for defining a macro where that verbatimized-read body is wrapped into a group/a local scope and into \scantokens. Some sort of \label-\ref-thingie for saving arbitrary snippets of code where the \label-part is done by an environment whose argument holds the label name and whose body contains the snippet of code which might as well form a verbatim-environment... – user184198 Mar 26 at 15:47

You can avoid this by realizing the surrounding local scope by means of an environment.

\documentclass{article}
\parindent=2cm

\newenvironment{localscope}{}{}

\begin{document}

XXX\begin{verbatim}
\LaTeX
\LaTeX
\LaTeX
\end{verbatim}
YYY

\noindent\null\hrulefill\null

XXX\begingroup\begin{verbatim}
\LaTeX
\LaTeX
\LaTeX
\end{verbatim}
\endgroup YYY

\noindent\null\hrulefill\null

XXX\begin{localscope}%
\begin{verbatim}
\LaTeX
\LaTeX
\LaTeX
\end{verbatim}
\end{localscope}YYY

\end{document}