获取 colortbl 的 \arrayrulecolor 的当前值

获取 colortbl 的 \arrayrulecolor 的当前值

我想定义一个特殊tabular环境,并允许用户独立于常规tabular环境的值指定文本和规则的颜色。对于文本颜色,这很容易,但我不知道如何为规则做到这一点。这是一个高度简化的示例。

\documentclass[12pt]{article}
\usepackage{colortbl}

% Set default colors
\def\xtablerulecolor{black}
\def\xtabletextcolor{black}

% Make color of rules easier to see
\arrayrulewidth0.5mm

\newenvironment{xtable}%
    {%
        % save current value of \arrayrulecolor
        %\gdef\origarrayrulecolor{???}%
        \arrayrulecolor{\xtablerulecolor}%
        \color{\xtabletextcolor}
        \begin{tabular}{|c|}%
    }%
    {%
        \end{tabular}%
        % restore \arrayrulecolor
        %\arrayrulecolor{\origarrayrulecolor}%
    }%

\begin{document}

\renewcommand{\xtablerulecolor}{blue}
\renewcommand{\xtabletextcolor}{green}

\arrayrulecolor{red}
\color{magenta}

\texttt{xtable}:

\medskip

\begin{xtable}
    A\\
    B
\end{xtable}    

\bigskip

\texttt{tabular}:
Rules should be red

\medskip

\begin{tabular}{|c|}
    X\\
    Y
\end{tabular}

\end{document}

结果是

代码输出

文本的颜色是局部的xtable,这很好,但不是arrayrulecolor。因此,我可以在启动tabular由定义的环境之前获取该颜色的当前值xtable,并在结束环境后恢复它,就像示例中注释掉的代码一样。但我如何才能获得当前的值\arrayrulecolor?(也就是说,???代码中应该是什么?)

这个问题\colorlet是相关的。我需要的类似物吗\arrayrulecolor

答案1

您可以保存和恢复\CT@arc@

\documentclass[12pt]{article}
\usepackage{colortbl}

% Set default colors
\def\xtablerulecolor{black}
\def\xtabletextcolor{black}

% Make color of rules easier to see
\arrayrulewidth0.5mm

\makeatletter
\newenvironment{xtable}%
    {%
        % save current value of \arrayrulecolor
        %\gdef\origarrayrulecolor{???}%
        \global\let\saved@CT@arc@\CT@arc@
        \arrayrulecolor{\xtablerulecolor}%
        \color{\xtabletextcolor}
        \begin{tabular}{|c|}%
    }%
    {%
        \end{tabular}%
        % restore \arrayrulecolor
        %\arrayrulecolor{\origarrayrulecolor}%
        \global\let\CT@arc@\saved@CT@arc@
    }%
\makeatletter

\begin{document}

\renewcommand{\xtablerulecolor}{blue}
\renewcommand{\xtabletextcolor}{green}

\arrayrulecolor{red}
\color{magenta}

\texttt{xtable}:

\medskip

\begin{xtable}
    A\\
    B
\end{xtable}    

\bigskip

\texttt{tabular}:
Rules should be red

\medskip

\begin{tabular}{|c|}
    X\\
    Y
\end{tabular}

\end{document}

相关内容