Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
add comment

1 Answer 1

up vote 1 down vote accepted

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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