![根据第一列中的选择对 LaTeX 表进行条件行格式化](https://linux22.com/image/463788/%E6%A0%B9%E6%8D%AE%E7%AC%AC%E4%B8%80%E5%88%97%E4%B8%AD%E7%9A%84%E9%80%89%E6%8B%A9%E5%AF%B9%20LaTeX%20%E8%A1%A8%E8%BF%9B%E8%A1%8C%E6%9D%A1%E4%BB%B6%E8%A1%8C%E6%A0%BC%E5%BC%8F%E5%8C%96.png)
有没有办法根据第一列中的选择突出显示表格中的特定行。例如“HEA160”,此特定行变为红色。
\documentclass{scrartcl}
\usepackage{filecontents}
\begin{filecontents*}{lab01-data.csv}
ID;h;b;tw;tf;r
HEA100;96;100;5.0;8.0;12
HEA120;114;120;5.0;8.0;12
HEA140;133;140;5.5;8.5;12
HEA160;152;160;6.0;9.0;15
HEA180;171;180;6.0;9.5;15
HEA200;190;200;6.5;10.0;18
HEA220;210;220;7.0;11.0;18
HEA240;230;240;7.5;12.0;21
HEA260;250;260;7.5;12.5;24
HEA280;270;280;8.0;13.0;24
HEA300;290;300;8.5;14.0;27
\end{filecontents*}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\begin{document}
\begin{table}[]
\centering
\footnotesize
\caption{Section data}
\label{tab:data}
\pgfplotstabletypeset[
header=true,
col sep=semicolon,
use comma,
columns={ID,h,b,tw,tf,r},
columns/ID/.style ={string type},
columns/h/.style ={dec sep align, fixed zerofill, precision=2},
columns/b/.style ={dec sep align, fixed zerofill, precision=0},
columns/tw/.style ={dec sep align, fixed zerofill, precision=1},
columns/tf/.style ={dec sep align, fixed zerofill, precision=1},
columns/r/.style ={dec sep align, fixed zerofill, precision=2,column type/.add={}{@{}}},
% <dec sep align> doubles column
every head row/.style={
output empty row,
before row={
\toprule
% pay attention to column doubline due to <dec sep align>
& % empty: first two columns
\multicolumn{6}{l}{Dimensions} \\
%
\cmidrule(l){2-11}
%
\multicolumn{1}{@{}c}{$Profile$} &
\multicolumn{2}{c}{$h$} &
\multicolumn{2}{c}{$b$} &
\multicolumn{2}{c}{$t_{w}$} &
\multicolumn{2}{c}{$t_{f}$} &
\multicolumn{2}{c@{}}{$r$} \\
%
\cmidrule(lr){2-3}
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
\cmidrule(lr){8-9}
%\cmidrule(lr){10-14}
\cmidrule(l){10-11}
%
\multicolumn{1}{@{}c}{\si{}} &
\multicolumn{2}{c}{\si{mm}} &
\multicolumn{2}{c}{\si{mm}} &
\multicolumn{2}{c}{\si{mm}} &
\multicolumn{2}{c}{\si{mm}} &
\multicolumn{2}{c@{}}{\si{mm}} \\
},
after row=\midrule,
},
every last row/.style={after row=\bottomrule},
]{lab01-data.csv}
\end{table}
\end{document}
答案1
使用({NiceTabular}
您nicematrix
需要多次编译)。
\documentclass{article}
\usepackage{nicematrix}
\usepackage{collcell}
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}
\ExplSyntaxOn
\NewDocumentCommand{\mycolor}{m}
{ \str_if_eq:nnT { #1 } { HEA160 } { \rowcolor{red!15} } #1 }
\ExplSyntaxOff
\newcolumntype{E}{>{\collectcell\mycolor}l<{\endcollectcell}}
\begin{NiceTabular} [colortbl-like]
{
E
S[table-format=3.2,minimum-decimal-digits=2]
S[table-format=3.2,minimum-decimal-digits=2]
S[table-format=1.1]
S[table-format=2.1]
S[table-format=3.2,minimum-decimal-digits=2]
}
\toprule
\Block{3-1}{\emph{Profile}} & \Block[l]{1-5}{Dimensions} \\
\cmidrule{2-6}
& {$h$} & {$b$} & {$t_w$} & {$t_f$} & {$r$} \\
& \unit{mm} & \unit{mm} & \unit{mm} & \unit{mm} & \unit{mm} \\
\midrule
HEA100 & 96 & 100 & 5.0 & 8.0 & 12 \\
HEA120 & 114 & 120 & 5.0 & 8.0 & 12 \\
HEA140 & 133 & 140 & 5.5 & 8.5 & 12 \\
HEA160 & 152 & 160 & 6.0 & 9.0 & 15 \\
HEA180 & 171 & 180 & 6.0 & 9.5 & 15 \\
HEA200 & 190 & 200 & 6.5 & 10.0 & 18 \\
HEA220 & 210 & 220 & 7.0 & 11.0 & 18 \\
HEA240 & 230 & 240 & 7.5 & 12.0 & 21 \\
HEA260 & 250 & 260 & 7.5 & 12.5 & 24 \\
HEA280 & 270 & 280 & 8.0 & 13.0 & 24 \\
HEA300 & 290 & 300 & 8.5 & 14.0 & 27 \\
\bottomrule
\end{NiceTabular}
\end{document}
如果您想从 CSV 文件中读取数据,这里有一个解决方案和用于读取 CSV 文件的{NiceTabular}
包。csvsimple
\documentclass{article}
\begin{filecontents*}{lab01-data.csv}
ID;h;b;tw;tf;r
HEA100;96;100;5.0;8.0;12
HEA120;114;120;5.0;8.0;12
HEA140;133;140;5.5;8.5;12
HEA160;152;160;6.0;9.0;15
HEA180;171;180;6.0;9.5;15
HEA200;190;200;6.5;10.0;18
HEA220;210;220;7.0;11.0;18
HEA240;230;240;7.5;12.0;21
HEA260;250;260;7.5;12.5;24
HEA280;270;280;8.0;13.0;24
HEA300;290;300;8.5;14.0;27
\end{filecontents*}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[l3]{csvsimple}
\begin{document}
\ExplSyntaxOn
\NewDocumentCommand{\Test}{m}
{ \str_if_eq:VnT #1 { HEA160 } { \rowcolor{red!15} } #1 }
\ExplSyntaxOff
\begin{NiceTabular} [colortbl-like]
{
l
S[table-format=3.2,minimum-decimal-digits=2]
S[table-format=3.2,minimum-decimal-digits=2]
S[table-format=1.1]
S[table-format=2.1]
S[table-format=3.2,minimum-decimal-digits=2]
}
\toprule
\Block{3-1}{\emph{Profile}} & \Block[l]{1-5}{Dimensions} \\
\cmidrule{2-6}
& {$h$} & {$b$} & {$t_w$} & {$t_f$} & {$r$} \\
& \unit{mm} & \unit{mm} & \unit{mm} & \unit{mm} & \unit{mm} \\
\midrule
\csvreader[head to column names, separator = semicolon]{lab01-data.csv}{}
{ \Test{\ID} & \h & \b & \tw & \tf & \r \\ }%
\\ \noalign{\vspace{-12pt}}
\bottomrule
\end{NiceTabular}
\end{document}