在 \tabulary 中使用 \cellcolor

在 \tabulary 中使用 \cellcolor

虽然在 中可以工作,但\cellcolorpackage 中的命令似乎colortbl在 环境中不起作用。这是一个最小的例子:tabularytabular

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tabulary}
\usepackage{colortbl}

\begin{document}

\begin{tabular}{c}
\cellcolor{green}42
\end{tabular}

\begin{tabulary}{\textwidth}{C}
\cellcolor{green}42
\end{tabulary}

\end{document}

还有什么需要添加,或者有解决方法吗?

答案1

tabulary检测colortbl并插入针对该情况的修改后的定义,但在 2001 年\cellcolor“最近”添加:并且未更新以匹配。colortbltabulary

% \changes{v0.1j}{2001/02/13}{\cs{cellcolor} (唐纳德·阿瑟诺)}

在此处输入图片描述

\documentclass[a4paper]{article}

\usepackage{colortbl}
\usepackage{tabulary}
\usepackage{etoolbox}

\begin{document}
\makeatletter% needs to be after begin document

\patchcmd\@mkpream
{\let\CT@row@color\relax}
{\let\CT@row@color\relax\let\CT@cell@color\relax}
{\typeout{good}}
{\typeout{bad}}


\patchcmd\TY@classz
{\CT@row@color}
{\CT@row@color\CT@cell@color}
{\typeout{good}}
{\typeout{bad}}


\makeatletter

\begin{tabular}{>{\columncolor{red}}c}
a
\\
\cellcolor{green}42
\end{tabular}

\begin{tabulary}{\textwidth}{>{\columncolor{red}}C}
a
\\
\cellcolor{green}42
\end{tabulary}

\end{document}

相关内容