正文中可编译的自定义环境在定义为新环境时变为不可编译

正文中可编译的自定义环境在定义为新环境时变为不可编译

我尝试制作一个表格,其中除了第一列和第一行以外,背景单元格都是灰色的,并且第一行是蓝色的。以下代码成功生成了所需的输出(虽然我不知道这种覆盖绘制方法是否是实现我想要的效果的好方法,并且该命令在and\arrayrulecolor{tabuline}之外全局有效)。\begingroup\endgroup

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{tabularbg}{gray}{0.8}
\definecolor{tabuline}{named}{white} % whiten the line color
\colorlet{tabularul}{blue!30}
\begin{document}
    \begingroup
        \setlength{\fboxsep}{0pt}
        \arrayrulecolor{tabuline}
        \colorbox{tabularbg}%
        {%
        \begin{tabular}{>{\columncolor{tabularul}}r|r|r|r}
            \rowcolor{tabularul}
            $\times$ & negative & zero & positive \\
            \hline
            negative & $+$ & $0$ & $-$ \\
            \hline
            zero & $0$ & $0$ & $0$ \\
            \hline
            positive & $-$ & $0$ & $+$
        \end{tabular}%
        }
    \endgroup
\end{document}

我首先得到的表

我想把这个表变成一个新的环境,比如mytabular环境,以下是我尝试过的:

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{tabularbg}{gray}{0.8}
\definecolor{tabuline}{named}{white} % whiten the line color
\colorlet{tabularul}{blue!30}
\newenvironment{mytabular}[3]{%
    \begingroup\setlength{\fboxsep}{0pt}
    \colorbox{#3}{%
    \begin{tabular}{>{\columncolor{#2}}#1}
        \rowcolor{#2}
}{%
    \end{tabular}%
    }\endgroup
}
\begin{document}
    \begin{mytabular}{rrrr}{tabularul}{tabularbg}
        $\times$ & negative & zero & positive \\
        \hline
        negative & $+$ & $0$ & $-$ \\
        \hline
        zero & $0$ & $0$ & $0$ \\
        \hline
        positive & $-$ & $0$ & $+$
    \end{mytabular}
\end{document}

然后,我收到一个错误! LaTeX Error: Missing \begin{document}.。如果我在上方放一个空行\begin{document},这次错误是:

! Misplaced \crcr.
\endarray ->\crcr
                  \egroup \egroup \gdef \@preamble {}\CT@end
l.17 ...n{mytabular}{rrrr}{tabularul}{tabularbg}

我觉得mytabular环境定义没有包含任何错误,因为两个错误都发生在这之后。我应该如何修复代码?

答案1

通常的方法是使用lrbox;唯一的怪癖是将第三个参数带到“结束部分”,这用起来很容易\colorlet

问题是您无法\colorbox{<color>}{在“开始部分”打开并在“结束部分”关闭它。因此,lrbox我们将表格存储在存储箱中,稍后将其用作 的参数\colorbox

\documentclass{article}
\usepackage[table]{xcolor}

\definecolor{tabularbg}{gray}{0.8}
\definecolor{tabuline}{named}{white} % whiten the line color
\colorlet{tabularul}{blue!30}

\newsavebox{\mytabularbox}
\newenvironment{mytabular}[3]{%
  \setlength{\fboxsep}{0pt}%
  \arrayrulecolor{tabuline}%
  \colorlet{mytabularcolor}{#3}%
  \begin{lrbox}{\mytabularbox}
  \begin{tabular}{>{\columncolor{#2}}#1}
  \rowcolor{#2}
}{%
  \end{tabular}
  \end{lrbox}%
  \colorbox{mytabularcolor}{\usebox{\mytabularbox}}%
}

\begin{document}

\begin{mytabular}{r|r|r|r}{tabularul}{tabularbg}
$\times$ & negative & zero & positive \\
\hline
negative & $+$ & $0$ & $-$ \\
\hline
zero & $0$ & $0$ & $0$ \\
\hline
positive & $-$ & $0$ & $+$
\end{mytabular}

\end{document}

在此处输入图片描述

答案2

使用environ包来捕获环境主体,以避免\colorbox在标准的开始部分打开和结束部分关闭时出现问题\newenvironment

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{environ}

\definecolor{tabularbg}{gray}{0.8}
\definecolor{tabuline}{named}{white} % whiten the line color
\colorlet{tabularul}{blue!30}

\NewEnviron{mytabular}[3]{%
    \setlength{\fboxsep}{0pt}
    \colorbox{#3}{%
    \arrayrulecolor{tabuline}%
    \begin{tabular}{>{\columncolor{#2}}#1}
        \rowcolor{#2}\BODY
    \end{tabular}}%
}

\begin{document}

\begin{mytabular}{r|rrr}{tabularul}{tabularbg}
        $\times$ & negative & zero & positive \\
        \hline
        negative & $+$ & $0$ & $-$ \\
        \hline
        zero & $0$ & $0$ & $0$ \\
        \hline
        positive & $-$ & $0$ & $+$
\end{mytabular}

\end{document}

在此处输入图片描述

环境形成一个组,因此不需要明确使用\begingroup, \endgroup

相关内容