
xcolor
我正在嘗試使用帶有選項的套件創建一個具有交替行顏色的表格[table]
。以下範例示範了行著色(取消註解\rowcolors..
)可以隱藏/覆蓋表格內容。特別是 @ 表達式似乎有問題。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
%\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
- 我們怎麼才能防止這種情況發生呢?
- 第二個問題是是否有簡單的方法可以用顏色覆蓋整條線?人們可以觀察到灰線中的白色填充物。
答案1
對於第一個問題,您可以\tabcolsep
分別刪除這兩個列,例如
r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
其次,你可以定義一個\bottomrulec
類似的
\newcommand{\bottomrulec}{%
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
並用它代替\bottomrule
.如果您需要的話,\toprule
這裡有彩色版本。\midrule
\newcommand{\toprulec}{%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}
}
\newcommand{\midrulec}{%
\arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}
您修改的程式碼:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
\begin{document}
\begin{table}
\footnotesize
\centering
\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrulec
\end{tabular}
\end{table}
\end{document}
答案2
空白來自於在booktabs
表格規則上方和下方添加一些垂直填充。解決方法包括將此填充設置為 $0$ pt 並用cellspace
包的垂直間距替換它:它定義了一個最小的 此類垂直間距與相容\rowcolors
(列說明符前面必須帶有字母S
)。
至於 的問題,我用 和來@{}
替換它,在第二列中留下單元格並輸入第三列時添加負水平間距。>{}
<{}
\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
\begin{document}
\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
$ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
&& & \text{Col 1} & \text{Col 2} \\
\toprule
\text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
\text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
\bottomrule
\end{array} $
\end{table}
\end{document}
答案3
環境{NiceTabular}
提供nicematrix
了與 類似的工具,colortbl
但使用 PGF/Tikz 進行繪圖。
使用該環境,您可以直接獲得所需的內容(但由於使用 PGF/Tikz 節點,因此需要多次編譯nicematrix
)。
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
\begin{NiceTabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
\CodeBefore
\rowcolors{2}{gray!15}{}
\Body
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}