# Environments with matched parens in \begin and \end

Suppose I have this sort of expression in a document.

\multicolumn{8}{l}{%
\parbox{25em}{%
\begin{itemize}
\addtolength{\itemsep}{-1em}
\item foo
\item bar
\end{itemize}%
}}


I'd like to define a new environment, call it "comment" that would allow me to simple write

\begin{comment}
\item foo
\item bar
\end{comment}


and produce the same output. The problem I've encountered is that the parentheses that are opened at the start are only balanced at the very end. That creates a problem if I try to define the "begin"-section of the new environment in the flatfooted way:

\newenvironment{comment}{%
\multicolumn{8}{l}{%
\parbox{25em}{%
\begin{itemize}
\addtolength{\itemsep}{-1em}
}


The parentheses are unbalanced right now, and if I were to balance them inside the definition of the "begin" part of the environment, I'd get the wrong result.

Here's an MWE

    \documentclass{article}

\def\Boxes{%
$\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$%
}
\usepackage{fullpage}

\begin{document}

\begin{table}[h!]
\begin{tabular}{l|ccccccc}
& Tue & Wed & Thur & Fri & Sat & Sun & Mon\\
\hspace{1em}B$^{\flat}$ Major & \Boxes\\

\multicolumn{8}{l}{%
\parbox{25em}{%
\begin{itemize}
\addtolength{\itemsep}{-1em}
\item foo
\item bar
\end{itemize}%
}}
\\
\end{tabular}
\end{table}
\end{document}

-
You could use environ which captures the contents of the environment in a macro \BODY. However, you need \multicolumn to be the first element in a cell. Can you show a use-case of your requirement in the form of a minimal working example (MWE)? –  Werner Oct 2 '12 at 23:51
Just use the environment form of \parbox which is minipage then you can put \begin{minipage} in your start code and \end{minipage} in the code and so keep braces balanced. In the case of multicolumn use the lrbox environment as above to save the parbox and then use \multicolumn{}{..\usebox...} in the end code to use the collected text. –  David Carlisle Oct 2 '12 at 23:59
As Werner said, you cannot hide \multicolumn inside the \begin part of an environment. It executes \begingroup quite at the beginning before the start code of the environment is executed. \begingroup destroys the possibility for \multicolumn. –  Heiko Oberdiek Oct 3 '12 at 0:47
Supplied an MWE. @DavidCarlisle, your suggestion is a little bit too telegraphic for me. Can you say more? –  Bernhard Oct 3 '12 at 11:53

## 1 Answer

As noted in comments you can collect up the environment in lrbox. As noted in Werner's and Heiko's comments you can't use a standard \begin before \multicolumn so this fudges \begin slightly.

    \documentclass{article}
%! LaTeX Error: Command \Box not provided in base LaTeX2e.
% make supplied MWE processable, grumble
\usepackage{amssymb}

\def\Boxes{%
$\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$ & $\Box$%
}
\usepackage{fullpage}

\makeatletter
% only need this for \begin{spanlist} syntax otherwise skip this and use
% \spanlist....\endspanlist
\let\oldbegin\begin
\let\oldend\end
\def\begin#1{%
\expandafter\ifx\csname#1\endcsname\spanlist
\expandafter\@firstoftwo
\else
\expandafter\expandafter\expandafter\oldbegin\expandafter\@gobble
\fi
\spanlist{#1}}
\def\end#1{%
\expandafter\ifx\csname#1\endcsname\spanlist
\expandafter\@firstoftwo
\else
\expandafter\expandafter\expandafter\oldend\expandafter\@gobble
\fi
\endspanlist{#1}}
% end of scary \begin/\end redefinition.
\makeatother

\newenvironment{spanlist}
{\omit\begin{lrbox}{0}\begin{minipage}{25em}\begin{itemize}\addtolength{\itemsep}{-1em}}
{\end{itemize}\end{minipage}\end{lrbox}%
\span\multicolumn{7}{l}{\usebox{0}}}

\begin{document}

% never use[h] on its own
%\begin{table}[h!]
\begin{table}[htp!]
\begin{tabular}{l|ccccccc}
& Tue & Wed & Thur & Fri & Sat & Sun & Mon\\
\hspace{1em}B$^{\flat}$ Major & \Boxes\\
\begin{spanlist}
\item foo
\item bar
\end{spanlist}
\\
\end{tabular}
\end{table}
\end{document}
`
-