我有下表:
\documentclass[a4paper]{report}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabular}{l c c c}
\toprule
Group & Par. & Measure & Metric\\
\midrule
\rowcolor{gray!25} \cellcolor{white} Group 1 & & 1 & \(Formula_1=\displaystyle\frac{A}{B}\cdot 2 \)\\ \addlinespace[3pt]
\rowcolor{gray!25} \cellcolor{white} & F & 2 & \(Formula_2=\displaystyle\frac{C}{D}\cdot 3.1 \) \\ \addlinespace[3pt]
\rowcolor{gray!25} \cellcolor{white} & & 3 & \(Formula_3=\displaystyle\frac{E}{F}\cdot 2 \)\\ \addlinespace[3pt]
& & 3a &\\ \addlinespace[3pt]
& E & 3b &\\
& & 4 & \(Formula_4=\displaystyle\frac{G}{H}\)\\ \addlinespace[12 pt]
\rowcolor{gray!25} \cellcolor{white} Group 2 & & 5 & \(Formula_5=\displaystyle\frac{I}{J}\)\\ \addlinespace[3pt]
\rowcolor{gray!25} \cellcolor{white} & P & 6 & \(Formula_6=\displaystyle\frac{K}{L}\)\\ \addlinespace[3pt]
\rowcolor{gray!25} \cellcolor{white} & & 7 & \(Formula_7=M-N \)\\ \addlinespace[3pt]
& & 7a &\\ \addlinespace[3pt]
& N & 7b &\\
& & 8 & \(Formula_8=\displaystyle\frac{O}{P}\)\\ \addlinespace[3 pt]
\bottomrule
\end{tabular}%
\caption{Hi, I am the caption.}
\label{measures}%
\end{table}%
\end{document}
产生以下结果:
有两个明显的问题我无法克服:
- 这丑陋的灰色行之间的空白,由于“\addlinespace[3pt]”,我反过来又增加了它来提高分数的可读性
- 带有方程式的灰线中的垂直空间:我希望它们不要太靠近单元格顶部和底部边框(第一行非常清晰:“A/B”)
到目前为止我已经尝试过:
- 解决第一个问题没有什么大不了的...我尝试使用“rowcolor”和“cellcolor”,但没有明显的效果...
- 我之前尝试添加“\vphantom{\Huge{A}}”
\(Formula_1
但它只解决了一半的问题,如下图所示:
- 我也尝试过此解决方案,但它并不能解决问题,而且它给我的文档中的所有表格都上了颜色(而我希望只有这一个表格有颜色)。
希望一切都清楚
答案1
请总是发布完整的文档而不仅仅是片段,我必须猜测您正在使用哪些包(第一次猜错了)。
如果间距在行内而不是行间,则彩色面板将垂直增长。有多种方法可以完成设置\arraystretch
或\extrarowheight
使用[3pt]
选项\\
或添加不可见的“支柱”或多或少任何东西除了booktabs 行分隔符命令在这里会起作用。因此,如果您将间距放在单元格内,则可以解决这两个问题:公式周围的填充会增加,并且不会出现白色间隙:
\documentclass{article}
\usepackage{booktabs,colortbl,xcolor}
\begin{document}
\begin{table}[htbp]
\newcommand\bigstrut{\rule[-20pt]{20pt}{0pt}}
\setlength\extrarowheight{7pt}
\renewcommand\arraystretch{1.2}
\centering
\begin{tabular}{l c c c}
\toprule
Group & Par. & Measure & Metric\\
\midrule
\rowcolor{gray!25} \cellcolor{white} Group 1 & & 1 &\bigstrut \(Formula_1=\displaystyle\frac{A}{B}\cdot 2 \)\\[3pt]
\rowcolor{gray!25} \cellcolor{white} & F & 2 &\bigstrut \(Formula_2=\displaystyle\frac{C}{D}\cdot 3.1 \) \\[3pt]
\rowcolor{gray!25} \cellcolor{white} & & 3 &\bigstrut \(Formula_3=\displaystyle\frac{E}{F}\cdot 2 \)\\[7pt]
& & 3a &\\
& E & 3b &\\
& & 4 &\bigstrut \(Formula_4=\displaystyle\frac{G}{H}\)\\[6 pt]
\rowcolor{gray!25} \cellcolor{white} Group 2 & & 5 &\bigstrut \(Formula_5=\displaystyle\frac{I}{J}\)\\[3pt]
\rowcolor{gray!25} \cellcolor{white} & P & 6 &\bigstrut \(Formula_6=\displaystyle\frac{K}{L}\)\\[3pt]
\rowcolor{gray!25} \cellcolor{white} & & 7 &\bigstrut \(Formula_7=M-N \)\\[7pt]
& & 7a &\\
& N & 7b &\\
& & 8 & \(Formula_8=\displaystyle\frac{O}{P}\)\\
\bottomrule
\end{tabular}%
\caption{Hi, I am the caption.}
\label{measures}%
\end{table}%
\end{document}
答案2
与。{NiceTabular}
nicematrix
\documentclass[a4paper]{report}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{table}[htbp]
\centering
\renewcommand{\arraystretch}{1.2}
\begin{NiceTabular}{l c c c}[cell-space-limits=2pt]
\toprule
Group & Par. & Measure & Metric \\
\midrule
Group 1 & \Block[fill=gray!25]{3-3}{}
& 1 & \(Formula_1=\dfrac{A}{B}\cdot 2 \)\\
& F & 2 & \(Formula_2=\dfrac{C}{D}\cdot 3.1 \) \\
& & 3 & \(Formula_3=\dfrac{E}{F}\cdot 2 \)\\
& & 3a \\
& E & 3b \\
& & 4 & \(Formula_4=\dfrac{G}{H}\)\\
Group 2 & \Block[fill=gray!25]{3-3}{}
& 5 & \(Formula_5=\dfrac{I}{J}\)\\
& P & 6 & \(Formula_6=\dfrac{K}{L}\)\\
& & 7 & \(Formula_7=M-N \)\\
& & 7a \\
& N & 7b \\
& & 8 & \(Formula_8=\dfrac{O}{P}\)\\
\bottomrule
\end{NiceTabular}
\caption{Hi, I am the caption.}
\label{measures}%
\end{table}
\end{document}