下面是我的代码。如何为第一行添加背景颜色(请仅添加第一行)?
\begin{table}[!htb]
\centering
\begin{tabular}{c c c}
\toprule
\textbf{Temperature} [K] & \textbf{Thickness}$[\mu m]$ & \textbf{Permittivity}\\
\midrule
$H \# /k$ & K & 17762 \\
\bottomrule
\end{tabular}
答案1
您不能使用booktabs
带颜色的行,因为它会在水平线周围引入一些垂直空间,这些空间不会被着色。相反,我建议使用\boldline
,这是捆绑包中的一个小包shipunov
,它允许hline
带variable thickness
。语法是\hlineB{number}
(或\clineB
),它将绘制一条粗细等于 的水平线number × \arrayrulewidth
。
要替换添加的垂直间距booktabs
,您可以使用cellspace
包,它可以在单元格的顶部和底部添加一些最小的垂直填充。
\documentclass[a4paper]{article}
\usepackage[table, x11names]{xcolor}
\usepackage{array, booktabs, boldline} %
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\begin{document}
\begin{table}[!htb]
\centering
\begin{tabular}{Sc c c}
\hlineB{2}
\rowcolor{SeaGreen3!30!} \textbf{Temperature} [K] & \textbf{Thickness}$[\mu m]$ & \textbf{Permittivity}\\
\hlineB{1.5}
$H \# /k$ & K & 17762 \\
\hlineB{2}
\end{tabular}
\end{table}
\end{document}
答案2
只需使用\hline
代替\toprule
、\midrule
或\bootomrule
。或者设置 arrayrulewidth
\PassOptionsToPackage{table,x11names}{xcolor}% only needed if you get an option clash
\documentclass[a4paper]{article}
\usepackage{xcolor}
\usepackage{booktabs}
\begin{document}
\def\arraystretch{1.5}% vertical stretch
\setlength\arrayrulewidth{1pt}% thicker line
\begin{tabular}{c c c}\hline
\rowcolor{SeaGreen3!30!}\global\setlength\arrayrulewidth{0.4pt}
\textbf{Temperature} [K] & \textbf{Thickness}$[\mu m]$ & \textbf{Permittivity}\\\hline\global\setlength\arrayrulewidth{1pt}
$H \# /k$ & K & 17762 \\\hline
\end{tabular}%
\setlength\arrayrulewidth{0.4pt}% set to old value
\end{document}
答案3
您可以使用{NiceTabular}
与nicematrix
兼容的booktabs
。您需要两个编译。
\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs, nicematrix}
\begin{document}
\begin{table}[!htb]
\centering
\begin{NiceTabular}{ccc}[colortbl-like]
\toprule
\rowcolor{red!15}
\textbf{Temperature} [K] & \textbf{Thickness}$[\mu m]$ & \textbf{Permittivity}\\
\midrule
$H \# /k$ & K & 17762 \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}
答案4
通过使用与规则兼容tabularray
的库booktabs
(调用同名包)的包booktabs
。对于单元使用siunitx
包:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\begin{document}
\begin{table}[!htb]
%\sisetup{detect-weight}
\centering
\begin{tblr}{colspec = {ccc},
row{1} = {font=\bfseries, bg=red!15}
}
\toprule
Temperature [\si{\kelvin}]
& Thickness [\si{\micro\metre}]
& Permittivity \\
\midrule
$H \# /k$ & K & 17762 \\
\bottomrule
\end{tblr}
\end{table}
\end{document}