# Use environment into (new) xparse environment

I'm writing a small library, and for some reasons, it seems that I can't nest environments with xparse's new way of defining environments. It's quite annoying as my users may want to nest environments. Any idea what's going wrong?

Error:

ERROR: Paragraph ended before \@xverbatim was complete.


MWE:

\documentclass{article}
\usepackage{xparse}

\NewDocumentEnvironment{mythm}{O{}+b}{%
Theorem:

#2

===%
}

\begin{document}

\section{What I'd like to produce}
Theorem:

\begin{verbatim}
I am verbatim
\end{verbatim}

===%

\section{What I'd like to write}
\begin{mythm}
\begin{verbatim}
I am verbatim
\end{verbatim}
\end{mythm}

\end{document}

• it is always the case that verbatim can not be used in another command or in environments that grab their body. you would see the same with environments defined using newenviron, or tabularx etc, – David Carlisle May 6 '19 at 13:50
• @DavidCarlisle Is there any reason for that? And is there any workaround? I can see that beamer has a fragile option for that, what does it do? – tobiasBora May 6 '19 at 14:04
• yes it applies to \verb and all such commands, the input is tokenized while the argument is parsed so verbatim (which changes the way characters are tokenized) has no effect at all, compare \verb|\fbox| with \mbox{\verb|\fbox|} inside \mbox, \verb does not work at all. – David Carlisle May 6 '19 at 14:18
• You can define mythm as a standard environment then verbatim will work. That's always an advantage of using an environment rather than a command. – David Carlisle May 6 '19 at 14:19
• @tobiasBora Use the b argument type only if you need to process the environment's contents before typesetting it. In this case there seems to be no need at all for it. Remember, anyway, that the “end part” is still mandatory, even if usually empty; with your code, the end part is the \par generated by the blank line. – egreg May 6 '19 at 17:19

As the new b ... #2 syntax tries to make clear, environments that grab their body are essentially using environment syntax but really passing the body as the argument to a command. As such they inherit the usual restrictions on arguments that verbatim commands do not work. This applies whether the environment is defined using xparse or the environ package, or earlier custom implementations of the same idea such as tabularx or the amsmath align environments.
If (as in the example in the question) you do not need to access the environment body as a single argument, you could define mythm using a standard \newenvironemnt, then verbatim would work, or if in your real use case you do need the feature to grab the enviornment body then you need to document that the standard restriction on verbatim material in arguments of other commands applies.