我正在尝试对一个交替行的表格进行文本处理。它可以工作,但是行之间有一个非彩色的间隙/间距,所以看起来很奇怪:
以下是上面示例的代码:
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[htbp]
\caption{My Title}
\rowcolors{2}{gray!20}{white}
\setlength{\tabcolsep}{0.2cm}
\centering\vspace{3pt}
\begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
\textbf{Type} & \textbf{Name} \\\midrule
Animal & Monkey \\\midrule
Animal & Lion \\\midrule
Fruit & Apple \\
Fruit & Banana \\\bottomrule
\end{tabular}
\end{table}
\end{document}
有谁知道如何解决这个问题,而无需对软件包/标记进行太多更改?是否有一些参数需要调整?
非常感谢,并致以最诚挚的问候,
菲利克斯
答案1
使用\hline
而不是\midrule
:
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[htbp]
\caption{My Title}
\rowcolors{2}{gray!20}{white}
\setlength{\tabcolsep}{0.2cm}
\centering\vspace{3pt}
\begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\hline
\textbf{Type} & \textbf{Name} \\
\hline
Animal & Monkey \\
\hline
Animal & Lion \\
\hline
Fruit & Apple \\
\hline
Fruit & Banana \\
\hline
\end{tabular}
\end{table}
\end{document}
输出:
答案2
这是因为在规则(、和等)之前和之后booktabs
引入了\aboverulesep
和。没有用颜色绘制这个空间,所以我们得到了间隙。可以通过在上方和下方绘制与行相同的颜色的高度来防止这种情况。\belowrulesep
\toprule
\bottomrule
\midrule
\rowcolors
\hrule
\aboverulesep
\belowrulesep
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand*{\belowrulesepcolor}[1]{%
\noalign{%
\vspace{-\belowrulesep}
\bgroup
\color{#1}%
\hrule height\belowrulesep
\egroup
}%
}
\newcommand*{\aboverulesepcolor}[1]{%
\noalign{%
\bgroup
\color{#1}%
\hrule height\aboverulesep
\egroup
\vspace{-\aboverulesep}
}%
}
\begin{document}
\begin{table}[htbp]
\caption{My Title}
\rowcolors{2}{gray!20}{white}
\setlength{\tabcolsep}{0.2cm}
\centering\vspace{3pt}
\begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
\textbf{Type} & \textbf{Name} \\\midrule
Animal & Monkey \\\midrule
\belowrulesepcolor{gray!20}
Animal & Lion \\
\aboverulesepcolor{gray!20}\midrule
Fruit & Apple \\
% \belowrulesepcolor{gray!20} %% not needed since there is no midrule.
Fruit & Banana \\\aboverulesepcolor{gray!20}
\bottomrule
\end{tabular}
\end{table}
\end{document}
但更好的方法是放弃这些,\midrule
因为行是用颜色突出显示的
答案3
这是我最终选择的解决方案的代码:
% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\renewcommand*{\arraystretch}{1.2}
\begin{document}
\begin{table}[htbp]
\caption{My Title}
\rowcolors{2}{gray!20}{white}
\setlength{\tabcolsep}{0.2cm}
\centering\vspace{3pt}
\begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
\textbf{Type} & \textbf{Name} \\\midrule
Animal & Monkey \\\midrule
Animal & Lion \\\midrule
Fruit & Apple \\
Fruit & Banana \\\bottomrule
\end{tabular}
\end{table}
\end{document}