# argument not recognized in second set of braces in newenvironment [duplicate]

In the following code, versionB doesn't work. the argument/parameter #1 seems to be not recognized when in second set of braces. any help?

\newenvironment{versionA}[1]
{#1} {}

\newenvironment{versionB}[1]
{} {#1}

\begin{versionA}{5}
Hello world!
\end{versionA}

\begin{versionB}{5}
Good Bye, cruel world!
\end{versionB}


It seems that the arguments are only seen by the initial part of the environment. One way around this is to set a dummy variable in the top of the environment and then use it when you close the environment.

\documentclass{article}

\newenvironment{versionA}[1]
{#1} {}

\newenvironment{versionB}[1]
{\def\versionBOne{#1}} {\versionBOne}

\begin{document}
\begin{versionA}{5}
Hello world!
\end{versionA}

\begin{versionB}{5}
Good Bye, cruel world!
\end{versionB}

\end{document}


Of course, this won't work with nesting, but if you are going to nest these environments then you could set a counter and make the dummy variable name depend on the counter. Environments like enumerate etc use a similar trick.

Didn't find an answer for newenvironment but found an alternative - NewDocumentEnvironment. With this the following works as required.

\usepackage{xparse}

\NewDocumentEnvironment{versiona}{m}
{#1}{}

\NewDocumentEnvironment{versionb}{m}
{}{#1}

\begin{versiona}{5}
Hello world!
\end{versiona}

\begin{versionb}{5}
Good Bye, cruel world!
\end{versionb}