Environment definition separates matching braces

For applying some macro/command to the body of an environment, you may want to extract the entire body into a macro itself. This can be done (to whatever limited extent) using the environ package. Here is a short example:

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{itquote}{%
  \itshape% Set shape to italics
  \begin{quote}
    \BODY% regular \BODY of itquote environment
  \end{quote}
}
\begin{document}
\begin{quote}% Original quote environment
  Here is a very simple quote
\end{quote}
\begin{itquote}% New itquote environment
  Here is a very simple quote
\end{itquote}
\end{document}
​

enter image description here

In the above (admittedly elementary) example, itquote italicizes its contents (stored in the macro \BODY). For some, it provides a more intuitive way of working with environments within environments.

Additionally, it is always good to view the package documentation (and sometimes even the package source .sty). For example, although minipage is an environment and is typically used in the context of \begin{minipage}{<width>} ... \end{minipage} it can also be used in a "macro pair form" using \minipage{<width} ... \endminipage. Again, for some, this allows for a more intuitive way of splitting environment begin/end's over a new environment definition.


You can't leave unbalanced braces in a definition. That's forbidden by TeX's syntax rules.

There is a workaround:

\newsavebox{\cardbox}
\newenvironment{card}
  {\begin{lrbox}{\cardbox}
   \begin{tabular*}{55mm}{| p{50.5mm} |}
   \hline
  }
  {\hline
   \end{tabular}
   \end{lrbox}%
   \resizebox{55mm}{85mm}{\usebox{\cardbox}}%
  }

The contents of the environment is saved in the \cardbox bin which is later processed.

See Werner's answer for another method which is particularly useful in other situations.


Here is an example of defining a new environment:

\documentclass{article}
\usepackage{lipsum}
\newenvironment{MyQuote}{%
    \begin{quote}%
    % Add customization that goes after the start of the environment here
}{%
    % Add customization that goes before the end of the environment here
    \end{quote}%
}%

\begin{document}
  \begin{MyQuote}
    \lipsum[1]
  \end{MyQuote}
\end{document}

I suspect that you are trying to use some macro of the form \macro{} and attempting to add the \macro{ at the beginning and } at the end environment. Don't think this is allowed with the standard \newenvironment. You could use the environ package, with which provides access to the body of the environment via \BODY. Here is an example:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{environ}

\NewEnviron{MyQuote}{%
  \quote%
  \textcolor{red}{\BODY}%
}{%
  \endquote%
}
\begin{document}
  \begin{MyQuote}
    \lipsum[1]
  \end{MyQuote}
\end{document}

Tags:

Environments