我正在尝试根据从 CSV 文件导入的文本值为表格单元格着色。CSV 文件有三个可能的文本值,分别是“高”、“中”和“低”。如果单元格中有“高”,我尝试将表格单元格颜色设为红色;如果单元格中有“中”则将表格单元格颜色设为黄色;如果单元格中有“低”,则将表格单元格颜色设为绿色。
测试1.csv:
Sign
High
我的 MWE 如下:
\documentclass{report}
\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'
\begin{document}
\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
\setlength\tabcolsep{2mm}
\bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
\csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
{\\\hline\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}
在这里我希望单元格颜色为红色的类似地,如果单元格文本值为“Intermediate”,则单元格颜色应为黄色的, 等等。
这是一个类似的问题表格:基于内容的单元格颜色/条件单元格着色并尝试去实现,但没有成功。
还有其他方法可以实现这一目标吗?
答案1
可能\colorlet
是你的朋友。;-)
\begin{filecontents*}{Test1.csv}
Sign
High
Intermediate
Low
\end{filecontents*}
\documentclass{report}
\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'
\colorlet{High}{red}
\colorlet{Intermediate}{yellow}
\colorlet{Low}{green}
\begin{document}
\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
\setlength\tabcolsep{2mm}
\bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
\csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
{\\\hline\expandafter\cellcolor\expandafter{\romannumeral`\^^@\csvcoli}\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}
如果您需要从短语到颜色的更复杂的映射,则可以使用分隔参数:
\begin{filecontents*}{Test1.csv}
Sign
High
Intermediate
Low
\end{filecontents*}
\makeatletter
\@ifdefinable\@gobbletoexclam{\long\def\@gobbletoexclam#1!{}}%
\newcommand\exchange[2]{#2#1}%
%
% \mycolormapping{<Tokens>}{<s.th. that expands to one of the phrases High/Intermediate/Low>}
% -> <Tokens>{red}
% or <Tokens>{yellow}
% or <Tokens>{green}
% or <nothing>
%
\newcommand\mycolormapping[2]{%
% Expand #2 until encountering a non-expandable token.
% (If that non-expandable token is a space-token it will be discarded.)
% Then call the mapping-routine.
\expandafter\exchange\expandafter{\expandafter{\romannumeral`\^^@#2}}{\@mycolormapping{#1}}%
}%
\newcommand\@mycolormapping[2]{%
% Check whether #2 does contain "!". If so it is not one of the phrases
% High/Intermediate/Low but it could be a phrase which erroneously
% matches up delimiters due to containing (parts of) the delimiter "!High!Intermediate!Low!".
% If not so, apply the macro \mycolormappingfork which via delimited arguments
% cranks out the right cases.
\ifcat$\detokenize\expandafter{\@gobbletoexclam#2!}$%
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\mycolormappingfork!#2!Intermediate!Low!{#1{red}}%
!High!#2!Low!{#1{yellow}}%
!High!Intermediate!#2!{#1{green}}%
!High!Intermediate!Low!{}%
!!!!%
}{}%
}%
\@ifdefinable\mycolormappingfork{%
% Let \mycolormappingfork grab the argument #2 behind a
% delimiter "!High!Intermediate!Low!". Stuff before that
% delimiter is removed as unused #1. Stuff behind the
% second argument is removed as unused `!!!!`-delimited
% argument #3. The delimiter in turn is completed by
% \@mycolormapping's second argument. Depending on which
% of the sequences "!#2!Intermediate!Low!",
% "!High!#2!Low!", "!High!Intermediate!#2!",
% "!High!Intermediate!Low!" matches the delimiter you can
% crank out which of the phrases `High`, `Intermediate`
% or `Low` was provided via \@mycolormapping's #2. The
% last one, "!High!Intermediate!Low!", is for the case
% that \@mycolormapping's #2 formed neither of these
% phrases.
\long\def\mycolormappingfork#1!High!Intermediate!Low!#2#3!!!!{#2}%
}%
\makeatother
\documentclass{report}
\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}%set margin of page
\usepackage{csvsimple,booktabs,array,filecontents,siunitx}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{color, colortbl}%enable color use in table cell
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}%this will align cell content of table to center via using 'P'
\colorlet{High}{red}
\colorlet{Intermediate}{yellow}
\colorlet{Low}{green}
\begin{document}
\begin{center}
{\renewcommand{\arraystretch}{1.5}
\begin{tabular}{| P{3.5cm} | } \rowcolor{cyan!60!black}
\hline
\setlength\tabcolsep{2mm}
\bfseries \rule{0pt}{1pt} \color{white}SIGN% specify table head and its font color
\csvreader[head to column names, late after last line =\\\hline]{./Test1.csv}{}% use head of csv as column names
{\\\hline\mycolormapping{\cellcolor}{\csvcoli}\csvcoli}% specify your coloumns here
\end{tabular}
}
\end{center}
\end{document}
答案2
这是一个解决方案csvsimple-l3
和tabularray
包装:
\documentclass{report}
\usepackage[a4paper,left=1.9cm,right=1.9cm,top=2cm,bottom=2cm]{geometry}
\usepackage{csvsimple-l3}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{filecontents*}[overwrite]{\jobname A.csv}
Sign
High
\end{filecontents*}
\begin{filecontents*}[overwrite]{\jobname B.csv}
Sign
Intermediate
\end{filecontents*}
\begin{filecontents*}[overwrite]{\jobname C.csv}
Sign
Low
\end{filecontents*}
\begin{document}
\begin{center}
\csvreader[
tabularray = {
hlines, vlines, colspec = {Q[c,3.5cm]},
stretch=0, rowsep=5pt,
row{1} = {font=\bfseries,bg=azure4,fg=white},
},
head to column names,
table head = {SIGN\\},
]{\jobname A.csv}{}{
\ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{}
\ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{}
\ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{}
\csvexpval\csvcoli
}
\end{center}
\begin{center}
\csvreader[
head to column names,
tabularray = {
hlines, vlines, colspec = {Q[c,3.5cm]},
stretch=0, rowsep=5pt,
row{1} = {font=\bfseries,bg=azure4,fg=white},
},
table head = {SIGN\\},
]{\jobname B.csv}{}{
\ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{}
\ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{}
\ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{}
\csvexpval\csvcoli
}
\end{center}
\begin{center}
\csvreader[
head to column names,
tabularray = {
hlines, vlines, colspec = {Q[c,3.5cm]},
stretch=0, rowsep=5pt,
row{1} = {font=\bfseries,bg=azure4,fg=white},
},
table head = {SIGN\\},
]{\jobname C.csv}{}{
\ifcsvstrcmp{\csvcoli}{High}{\SetCell{red6}}{}
\ifcsvstrcmp{\csvcoli}{Intermediate}{\SetCell{yellow6}}{}
\ifcsvstrcmp{\csvcoli}{Low}{\SetCell{green6}}{}
\csvexpval\csvcoli
}
\end{center}
\end{document}