假设我在文档中有这种表达。
\multicolumn{8}{l}{%
\parbox{25em}{%
\begin{itemize}
\addtolength{\itemsep}{-1em}
\item foo
\item bar
\end{itemize}%
}}
我想定义一个新的环境,称之为“评论”,这样我就可以简单地写
\begin{comment}
\item foo
\item bar
\end{comment}
并产生相同的输出。我遇到的问题是,在开头打开的括号只在最后才匹配。如果我尝试以不着边际的方式定义新环境的“开始”部分,就会出现问题:
\newenvironment{comment}{%
\multicolumn{8}{l}{%
\parbox{25em}{%
\begin{itemize}
\addtolength{\itemsep}{-1em}
}
括号现在不平衡,如果我在环境“开始”部分的定义中平衡它们,我会得到错误的结果。
这是 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}
答案1
正如评论中所述,您可以在中收集环境。正如 Werner 和 Heiko 的评论中所述,您之前lrbox
不能使用标准,因此这有点含糊其辞。\begin
\multicolumn
\begin
\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}