我是 Latex 新手,对如何仅根据特定列的单元格值为表格中的单元格着色的概念感到困惑。我正在创建一个 Latex 模板,该模板将用于内部审计报告,其中将包含一系列部分和相关的调查结果表,我想仅根据可能的 4 个预定义值(例如已实施、未实施、未评估)为最后一列着色...
如果有人能给我指明正确的方向,我将不胜感激。我找到的所有示例都适用于数字,而我目前对数字的理解还不够,无法从文本字符的示例开始。
\documentclass{article}
\begin{document}
\section{Dog House }
Summary that describes the state of the dog house.
\begin{tabular}{lll}
Control No & Control Description & Finding \\
43 & The cat sat on the hat & Implemented \\
76 & The dog ate my cupcake & Not Implemented \\
645 & The rate is a cat & Not Assessed \\
\end{tabular}
\section{Cats House}
\begin{tabular}{lll}
Control No & Control Description & Finding \\
43 & The cat sat on the hat & Implemented \\
76 & The dog ate my cupcake & Not Implemented \\
645 & The rate is a cat & Not Assessed \\
\end{tabular}
\end{document}
谢谢
答案1
在包的帮助下collcell
。
\documentclass{article}
\usepackage{collcell}
\usepackage[table]{xcolor}
\newcolumntype{H}{>{\collectcell\HighLightCell}l<{\endcollectcell}}
\ExplSyntaxOn
\NewDocumentCommand { \HighLightCell } { m }
{
\str_case:nnF { #1 }
{
{ Implemented } { \cellcolor{red!20}Implemented }
{ Not~Implemented } { \cellcolor{blue!20}Not~Implemented }
{ Not~Assessed } { \cellcolor{magenta!20}Not~Assessed }
}
{ #1 }
}
\ExplSyntaxOff
\begin{document}
\section{Dog House }
Summary that describes the state of the dog house.
\begin{tabular}{llH}
Control No & Control Description & Finding \\
43 & The cat sat on the hat & Implemented \\
76 & The dog ate my cupcake & Not Implemented \\
645 & The rate is a cat & Not Assessed \\
\end{tabular}
\end{document}
答案2
只需几个额外的包就可以相对容易地实现:etoolbox
用于字符串比较的许多其他有用的附加功能;以及tabularray
,它添加了一个选项来为每个单元格运行宏
\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{tabularray}
\newcommand\setcolor[1]{%
\ifstrequal{#1}{Not Implemented}{\textcolor{red}{#1}}{%
\ifstrequal{#1}{Not Assessed}{\textcolor{black!40!green}{#1}}{%
\ifstrequal{#1}{Implemented}{\textcolor{blue}{#1}}{%
\ifstrequal{#1}{Assessed}{\textcolor{magenta}{#1}}{%
#1}}}}}
\begin{document}
Summary that describes the state of the dog house.
\section{Dog House }
\begin{tblr}{
colspec = {lll},
cell{2-Z}{3} = {cmd=\setcolor},
}
Control No & Control Description & Finding \\
23 & The cat sat on the hat & Assessed \\
43 & The cat sat on the hat & Implemented \\
76 & The dog ate my cupcake & Not Implemented \\
645 & The rate is a cat & Not Assessed \\
\end{tblr}
\end{document}