我遇到了一个问题,即\hline
一个表的长度不同。源代码如下:
\documentclass[11pt,a4paper]{article}
\usepackage{fourier}
\usepackage[english]{babel}
\usepackage{float}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{tabularx,ragged2e}
\def\btc{\begin{tabular}{c}}
\def\etc{\end{tabular}}
\begin{document}
\begin{table}[H]
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\vspace{0.5em}
\tiny
\begin{tabularx}{0.5\textwidth}{r|ccc}
\hline
\multirow{2}{1em}{} & \multicolumn{3}{c}{\scriptsize Processed Reads} \\
\cline{2-4}
&No. Reads & \btc Base Pairs (bp) \etc & \btc Length\\ (bp) \etc \\
\hline
\color{violet}{SUM}&\color{violet}{1,923,313,717}&\color{violet}{203,289,006,008}&\color{violet}{106} \\
\color{red}{AVERAGE}&\color{red}{4,006,903}&\color{red}{423,518,762}&\color{red}{106}\\
\color{green}{MEDIAN}&\color{green}{3,046,151}&\color{green}{323,854,746}&\color{green}{107} \\[1pt]
\hline
\end{tabularx}
\end{table}
\end{document}
这是输出表。如你所见,第二行比其他行长。
答案1
你必须X
如果您希望tabularx
有效,请至少使用一列。
\documentclass[11pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage{fourier}
\usepackage[table]{xcolor}
\usepackage{tabularx,ragged2e,array}
\newcommand{\splitcell}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}
\begin{document}
\begin{table}
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\medskip
\tiny
\begin{tabularx}{0.5\textwidth}{
@{}
l
|
*{3}{>{\centering\arraybackslash}X}
@{}
}
\hline
& \multicolumn{3}{c}{\scriptsize \strut Processed Reads} \\
\cline{2-4}
&No. Reads & \splitcell{Base Pairs \\ (bp)} & \splitcell{Length \\ (bp)} \\
\hline
\textcolor{violet}{SUM}&\textcolor{violet}{1,923,313,717}&\textcolor{violet}{203,289,006,008}&\textcolor{violet}{106} \\
\textcolor{red}{AVERAGE}&\textcolor{red}{4,006,903}&\textcolor{red}{423,518,762}&\textcolor{red}{106}\\
\textcolor{green}{MEDIAN}&\textcolor{green}{3,046,151}&\textcolor{green}{323,854,746}&\textcolor{green}{107} \\[1pt]
\hline
\end{tabularx}
\end{table}
\end{document}
请注意,这\color{red}{x}
是错误的语法(不幸的是,它被 MathJax 使用)。
我不确定颜色应该起什么作用;在我看来,它们只是让表格难以阅读。
更好的实现:
\documentclass[11pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage{fourier}
\usepackage[table]{xcolor}
\usepackage{siunitx,booktabs}
\sisetup{group-separator={,}}
\newcommand{\splitcell}[1]{{\begin{tabular}{@{}c@{}}#1\end{tabular}}}
\begin{document}
\begin{table}
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\medskip
\begin{tabular}{
@{}
l
S[table-format=10.0]
S[table-format=12.0]
S[table-format=3.0]
@{}
}
\toprule
& \multicolumn{3}{c}{Processed Reads} \\
\cmidrule{2-4}
&{No. Reads} & \splitcell{Base Pairs \\ (bp)} & \splitcell{Length \\ (bp)} \\
\midrule
SUM & 1923313717 & 203289006008 & 106 \\
AVERAGE & 4006903 & 423518762 & 106 \\
MEDIAN & 3046151 & 323854746 & 107 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
您的 MWE 有两个错误:
- 较新的用途
tabularx
,无需使用其列类型X
! - 彩色文本的正确语法是
\textcolor{<color>}{ text }
考虑上述规则,我得到以下结果(表格设计有一些小的改动):
\documentclass[11pt,a4paper]{article}
\usepackage{fourier}
\usepackage[english]{babel}
%\usepackage{float}
\usepackage[table]{xcolor}
%\usepackage{multirow}
\usepackage{tabularx,ragged2e}
\def\btc{\begin{tabular}{@{} c @{}}}
\def\etc{\end{tabular}}
\usepackage{caption}% <-- added
\begin{document}
\begin{table}[h]
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\renewcommand\arraystretch{1.3} % <-- added
\setlength\tabcolsep{3pt} % <-- added
\scriptsize%\tiny % <-- changed
\begin{tabularx}{0.5\textwidth}{r|
>{\centering\arraybackslash}X % <-- added
cc}
\hline
& \multicolumn{3}{c}{Processed Reads} \\
\cline{2-4}
& No. Reads & \btc Base Pairs (bp) \etc & \btc Length\\ (bp) \etc \\
\hline
\textcolor{violet}{SUM} % <-- changed in all colored text
&\textcolor{violet}{1,923,313,717} &\textcolor{violet}{203,289,006,008} &\textcolor{violet}{106} \\
\hline
\textcolor{red}{AVERAGE}
&\textcolor{red}{4,006,903} &\textcolor{red}{423,518,762} &\textcolor{red}{106}\\
\hline
\textcolor{green}{MEDIAN}
&\textcolor{green}{3,046,151} &\textcolor{green}{323,854,746} &\textcolor{green}{107} \\
\hline
\end{tabularx}
\end{table}
\end{document}
我不会评判您的表格设计,您可以在 Johannes_B 的回答中看到更好的格式(符合我的品味)(强烈建议使用它)。另外,我没有猜到,数字是由逗号分隔的一系列数字还是一个巨大的数字,为此最好使用包S
中的列类型siunitx
,正如 Johannes_B 在他的回答中所建议的那样。
答案3
采用完全不同的方法和不同的布局。
\documentclass[11pt,a4paper]{article}
\usepackage{fourier}
\usepackage[english]{babel}
\usepackage[table]{xcolor}
\usepackage{tabularx,ragged2e}
%\def\btc{\begin{tabular}{c}}
% \def\etc{\end{tabular}}% Never do stuf like that!
\usepackage{showframe}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}%[H]
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\tiny
\begin{tabularx}{0.5\textwidth}{rS[table-format=10]S[table-format=10]S[table-format=3]}
\toprule
& \multicolumn{3}{c}{Processed Reads} \\
\cmidrule{2-4}
& \multicolumn{1}{L}{No. Reads} & \multicolumn{1}{L}{Base~Pairs (bp)} & \multicolumn{1}{L}{Length (bp)} \\
\midrule
Sum & 1923313717 & 203289006008 & 106 \\
Average & 4006903 & 423518762 & 106 \\
Median & 3046151 & 323854746 & 107 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案4
发生这种情况的原因在于 cline 和 hline 之间的差异。hline 在整个表格上绘制一条水平线,而 \cline{xy} 绘制一条从 x 列开始到 y 列结束的线。如果我没记错的话,一列的结束时间略早于表格的结束时间,这就是它们的长度不同的原因。
一个解决方案是将所有 \hline 替换为 \cline{1-4}。
\documentclass[11pt,a4paper]{article}
\usepackage{fourier}
\usepackage[english]{babel}
\usepackage{float}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{tabularx,ragged2e}
\def\btc{\begin{tabular}{c}}
\def\etc{\end{tabular}}
\begin{document}
\begin{table}[H]
\centering
\caption{Trimming Summary}
\label{tab:trimming}
\vspace{0.5em}
\tiny
\begin{tabularx}{0.5\textwidth}{r|ccc}
\cline{1-4}
\multirow{2}{1em}{} & \multicolumn{3}{c}{\scriptsize Processed Reads} \\
\cline{2-4}
&No. Reads & \btc Base Pairs (bp) \etc & \btc Length\\ (bp) \etc \\
\cline{1-4}
\color{violet}{SUM}&\color{violet}{1,923,313,717}&\color{violet}{203,289,006,008}&\color{violet}{106} \\
\color{red}{AVERAGE}&\color{red}{4,006,903}&\color{red}{423,518,762}&\color{red}{106}\\
\color{green}{MEDIAN}&\color{green}{3,046,151}&\color{green}{323,854,746}&\color{green}{107} \\[1pt]
\cline{1-4}
\end{tabularx}
\end{table}