# Textual replacement macros to wrap environment

I want to create simple textual replacement macros for an environment:

\def \myEnvStart {\begin{someEnvironment}[enormous list of arguments]}
\def \myEnvEnd {\end{someEnvironment}}


This doesn't work:

\myEnvStart
stuff
\myEnvEnd


This works:

\myEnvStart
stuff
\end{someEnvironment}


Why? Is it possible to make the first code snippet work somehow?

• The first version works too, so I guess, it might depend on the particular someEnvironment – user31729 Mar 20 '15 at 17:56
• My guess is you're trying to use your macro-like setup for something like align, right? What is the specific use-case? – Werner Mar 20 '15 at 17:58
• Be careful of what you want may be a side effect. \myEnvStart stuff \myEnvEnd is not \begin{myEnvStart} stuff \end{myEnvEnd} – touhami Mar 20 '15 at 18:11

In general, this works, when you're dealing with ordinary environments that merely perform replacements of their \begin-\end counterparts. Here's an example:

\documentclass{article}

\usepackage[nopar]{lipsum}% Just for this example

\def\myEnvStart{\begin{center}}
\def\myEnvEnd{\end{center}}

\begin{document}

\lipsum[1]

\myEnvStart% \begin{center}
\lipsum[2]% Center this text.
\myEnvEnd% \end{center}

\lipsum[3]

\end{document}


However, "not all environments are created equal". Some are created in a special way that make them require to see their \end{<env>} counterpart explicitly. Here's an example of that using the verbatim environment:

\documentclass{article}

\usepackage[nopar]{lipsum}% Just for this example

\def\myEnvStart{\begin{verbatim}}
\def\myEnvEnd{\end{verbatim}}

\begin{document}

\lipsum[1]

\myEnvStart% \begin{verbatim}
\lipsum[2]% Center this text.
\myEnvEnd% \end{verbatim}

\lipsum[3]

\end{document}


Replacing \myEnvStart/\myEnvEnd with \begin{verbatim}/\end{verbatim}, of course, yields the desired result:

In a broader sense, some environments are written in this special way because they do more than just insert their environment start/end replacements. They may which to capture the entire environment content, perhaps to allow multiple processing of the content - tabularx does this, as does the align-and friends environments of amsmath.

It may not necessarily be for multiple processing as listings's lstlisting requires the same treatment because of category code changes (similar to our verbatim example above). Category codes need to be restored at the end of the environment, and to do so correctly, some environment gobble their contents wholly before working on it.

So the answer is "Yes", it works, but only in some cases.

Of course, the next question is whether a work-around to this exists. Sure there is a work-around... but also only in some cases. For example, you may be able to get away with your macro-like interface by grabbing the "environment" content yourself before passing it on to "the real environment". Here's such a way when looking at the first example:

\long\def\myEnvStart#1\myEnvEnd{%
\begin{center}
#1
\end{center}}


The macro parameter text now includes the anticipated "environment end code" \myEnvEnd, implying that #1 will be the entire "environment" content. The environ package provides exactly this approach, although it requires the use of a \begin/\end pair.

You can't employ the above-suggested method with verbatim-like text (including lstlisting) as they change category codes, so again, only in some instances.