我试图使用来生成表格,tabular*
因为我还想使用等来获得不同的线条粗细。但是,在下面的尝试中,当我还尝试为表格的各个部分着色时,我得到了如下颜色的空格:midrule
bottomrule
我已经看过这些相关解决方案但想知道是否有更简单的解决方案可以使用,因为我仍然没有得到我想要的结果。
这是我的代码:
\documentclass{article}
\usepackage{float}
\usepackage{xcolor,colortbl,tabularx}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{table}[H]
\centering
\caption{Not too good looking table here.\label{tab:table1}}
\footnotesize
% Table generated by Excel2LaTeX from sheet 'Sheet2'
\begin{tabularx}{\textwidth}{c @{\extracolsep{\fill}} c|ccc}
%\begin{tabular*}{cc|cc}
%\toprule
\hline
\multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
%\midrule
\hline
FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\\
FLTR0 & 80 & 80 & FLTR0 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \\
FLTR6 & 6 & 44 & FLTR4 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \\
FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\\
\hline
%\bottomrule
\end{tabularx}%
%\end{tabular*}
\end{table}
\end{document}
toprule
如何让颜色填充单元格而没有任何间隙,同时使用midrule
等?
答案1
- 使用
@{\extracolsep{\fill}}
在无法着色的列之间插入额外空格\rowcolor{...}
因此,使用的方法tabularx
是一种很有前途的方法,但在其中你不应该使用@{\extracolsep{\fill}}
- 使用时
tabularx
要求至少有一列是类型X
- 如果所有列都可以具有相同的宽度,则表格使用的代码
tabularx
可以是:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}[ht]
\centering
\caption{Not too good looking table here.\label{tab:table1}}
\footnotesize
\begin{tabularx}{\textwidth}{CC|CC}
\hline
\multicolumn{2}{c|}{\textbf{Test1}}
& \multicolumn{2}{c}{\textbf{Test2}} \\
Test11 & Test12 & Test13 & Test14 \\
\hline
FLTR2 & 2 & 40 & FLTR0 \\
FLTR0 & 80 & 80 & FLTR0 \\
\rowcolor{red!30}
\textcolor{purple}{FLTR0}
& \textcolor{purple}{160}
& \textcolor{purple}{204}
& \textcolor{purple}{FLTR4} \\
FLTR6 & 6 & 44 & FLTR4 \\
\rowcolor{red!30}
\textcolor{purple}{FLTR16}
& \textcolor{purple}{16}
& \textcolor{purple}{43¸}
& \textcolor{purple}{FLTR3} \\
FLTR4 & 44 & 6 & FLTR6 \\
\hline
\end{tabularx}%
\end{table}
\end{document}
我认为这个表格的列内容之间有太多空白。如果你愿意减少表格宽度,效果会更好。
编辑:
一个较窄的表格的示例,其中仅在第一和第二列宽度中使用tabularray
和siunitx
包的某些单元格着色,MWE 可以是:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx, varwidth}
\NewTableCommand\SCC[2]{\SetCell{bg=#1, fg=#2}}
\begin{document}
\begin{table}[ht]
\centering
\caption{Not too good looking table here.}
\label{tab:table1}
\begin{tblr}{colspec={Q[l] Q[c, si={table-format=3.0}] |
Q[c, si={table-format=3.0}] Q[l] },
row{1} = {font=\bfseries, guard},
row{2} = {guard}
}
\toprule
\SetCell[c=2]{c} Test 1
& & \SetCell[c=2]{c} Test 2
& \\
Test11 & Test12 & Test13 & Test14 \\
\midrule
FLTR2 & 2 & 40 & FLTR0 \\
FLTR0 & 80 & 80 & FLTR0 \\
\SetRow{bg=red!20, fg=purple}
FLTR0 & 160 & 204 & FLTR4 \\
FLTR6 & 6 & 44 & FLTR4 \\
\SCC{red!20}{purple}
FLTR16 & \SCC{red!20}{purple}
16 & 43 & FLTR3 \\
FLTR4 & 44 & 6 & FLTR6 \\
\bottomrule
\end{tblr}%
\end{table}
\end{document}
答案2
也许这有帮助——欢迎提问
平均能量损失
\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{NiceTabular}{c @{\extracolsep{\fill}} c|ccc}[colortbl-like]
\toprule
\multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
\midrule
FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\\
FLTR0 & 80 & 80 & FLTR0 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \\
FLTR6 & 6 & 44 & FLTR4 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \\
FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\\
\bottomrule
\end{NiceTabular}
\end{document}
要将表格扩展到整个text width
--提供X columns
类似的列tabularx
\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{NiceTabular}{c X[c]|c X[c]}[colortbl-like]
\toprule
\multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
\midrule
FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\\
FLTR0 & 80 & 80 & FLTR0 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \\
FLTR6 & 6 & 44 & FLTR4 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \\
FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\\
\bottomrule
\end{NiceTabular}
\end{document}
答案3
通过nicematrix
将键中的 {tabular*} 替换为 {NiceTabular*} colortbl-like
,您可以获得以下结果:
\documentclass{article}
\usepackage{float}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{table}[H]
\centering
\caption{Not too good looking table here.\label{tab:table1}}
\footnotesize
% Table generated by Excel2LaTeX from sheet 'Sheet2'
\begin{NiceTabular*}{\textwidth}{c @{\extracolsep{\fill}} c|ccc}[colortbl-like]
%\begin{tabular*}{cc|cc}
%\toprule
\hline
\multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
%\midrule
\hline
FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\\
FLTR0 & 80 & 80 & FLTR0 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \\
FLTR6 & 6 & 44 & FLTR4 \\
\rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \\
FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\\
\hline
%\bottomrule
\end{NiceTabular*}%
%\end{tabular*}
\end{table}
\end{document}
请注意,表格的序言宣布有 5 列,但您只使用了 4 列。