# how to redefine environments like \begin{xxx} … \end{xxx}

I'm translating some text that use the comment package, it's usage seems like that:

\begin{en}
... some English text
\end{en}

\begin{zh}
... corresponding Chinese text
\end{zh}


By using \excludecommand{en} or \includecommand{en} before \begin{document}, we can control the text out or in the final PDF document.

But this too trouble to add \begin{}/\end{} for every paragraph, what I want is following:

\en{
... some English text
}

\zh{
... corresponding Chinese text
}


this is much more simple to type. and I create the commands like this:

\newcommand{\zh}[1]{\begin{zh}#1\end{zh}}
\newcommand{\en}[1]{\begin{en}#1\end{en}}


But this cause compile error like runaway argument, how to make it work under this situation by using newcommand?

-
You can try \let\OldEn\en and use \OldEn, instead of \begin{en} and \enden, instead of \end{en}. Also, while code snippets are useful in explanations, it is always best to compose a fully compilable MWE that reproduces the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Mar 19 at 14:18
I think the best way to do things would be to keep the environments and to configure your editor so that with a shortcut it typesets the environment and puts the cursor in the middle. –  Bernard Mar 19 at 14:27
Note that not all environments can be turned into commands (anything with verbatim or ams alignments will cause trouble at least) also in a large document it is a lot easier to see matching begin/end than working out what is ended by } any reasnobel editor shoudl allow you to enter \begin{en} \end{en} with 1 or 2 keystrokes –  David Carlisle Mar 19 at 14:28
You can't: The comment environment is looking for the string \end{en} while skipping over the rest of the text. Also it is problematic to put large portions of text in an argument: You can't change catcodes there and so a lot of things (e.g. verbatim) are not possible. –  Ulrike Fischer Mar 19 at 14:29
@Bernard: what I'm planning is sed or awk... –  coanor Mar 19 at 14:29

The problem is that you are trying to redefine an existing command \en which gets defined when the en environment is defined (as does \enden). The solution to redefine most environments is to first save the definition of xxx as \OldXxx:

\let\OldXxx\xxx


and then redefine the \xxx macro using \OldXxx and \endxxx:

\renewcommand{\xxx}[1]{\OldXxx#1\endxxx}


## Note:

• As per the comments, this won't work with any verbatim type of environments.

## Code:

\documentclass{article}

\newenvironment{en}{%
}{%
}%

\begin{document}
\begin{en}
\ldots some English text in environment
\end{en}

\let\OldEn\en
\renewcommand{\en}[1]{\OldEn#1\enden}

\en{\ldots some more English text in macro \ldots}

\end{document}

-

Use just one other character for the command:

\documentclass{article}
\newenvironment{en}{}{}
\newenvironment{zh}{}{}

\newcommand\Zh[1]{\begin{zh}#1\end{zh}}
\newcommand\En[1]{\begin{en}#1\end{en}}

%\renewcommand\Zh[1]{} %%% enable if it should be excluded
%\renewcommand\En[1]{} %%% enable if it should be excluded

\begin{document}
\En{ \ldots some English text in environment (En)}

\Zh{ \ldots some more English text in macro \ldots (Zh)}

\end{document}

-
It works but with the \includecommand{Zh}/\excludecommand{En}, why? –  coanor Mar 19 at 15:04
You can simply redefine the command as empty. Then it throws its argument away. See my edited answer. –  Herbert Mar 19 at 15:23