4

This question already has an answer here:

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}

marked as duplicate by Ian Thompson, Mico, clemens, Community Jun 29 '17 at 7:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

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.

3

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}

Not the answer you're looking for? Browse other questions tagged or ask your own question.