我是 Latex 的新用户,这是我第一次尝试为我的论文构建表格。我希望表格看起来像这样:
在网上进行了一些研究后,我得到了以下代码(忽略 Latex 代码和 Word 文档之间的列标题差异)
\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{lrrrr}
\toprule
\multicolumn{2}{c}{{BCom} &
\multicolumn{2}{c}{B.Bus.Sci} \\
\cmidrule{2-3} \cmidrule{4-5} &
Number of courses required to pass & Cumulative Total of Courses && Number of courses & Cumulative Total of Courses
\midrule
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabular}
\label{table:mr}
\end{table}
它根本不起作用!
答案1
更好的版本:
\documentclass{article}
\usepackage{booktabs}
\usepackage[margin=1in]{geometry}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{document}
\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{@{}p{0.12\textwidth}*{4}{L{\dimexpr0.22\textwidth-2\tabcolsep\relax}}@{}}
\toprule
& \multicolumn{2}{c}{BCom} &
\multicolumn{2}{c}{B.Bus.Sci} \\
\cmidrule(r{4pt}){2-3} \cmidrule(l){4-5}
& Number of courses required to pass & Cumulative Total of Courses & Number of courses & Cumulative Total of Courses\\
\midrule
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabular}
\label{table:mr}
\end{table}
\end{document}
为什么你的代码无法编译?
你错过
&
了\multicolumn{2}{c}{{BCom} & \multicolumn{2}{c}{B.Bus.Sci} \\
将第一个条目放入第一列。
&&
你有Number of courses required to pass & Cumulative Total of Courses && Number of courses & Cumulative Total of Courses
应该是
&
,你应该&
在行首放一个。而在这一行的末尾,你刚好\\
在之前漏掉了一个\cmidrule
。
现在修改
array
在纠正了以上所有内容之后,我在包的帮助下引入了一种新的列类型:
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\raggedright
添加是为了避免难看的对齐效果,因为列可能很窄,单词的长度也可能不同。这也能解决坏框的问题。
我已经使用L
宽度为
\dimexpr0.22\textwidth-2\tabcolsep\relax
通过
*{4}{L{\dimexpr0.22\textwidth-2\tabcolsep\relax}}
而不是重复。
附注:可以将eft 或ight\cmidrule
缩写为l
r
\cmidrule(r{4pt}){2-3}
这{4pt}
是我们缩短的量。可以像 一样省略\cmidrule(r){4-5}
。
答案2
除了使用该booktabs
包(以获得美观的、即间距适当的水平线)之外,我还建议您使用该tabularx
包来简化获取四个等宽列的工作。
\documentclass{article}
\usepackage{booktabs,tabularx}
\usepackage[margin=1in]{geometry} % set margins to meet your document's needs
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
% use ragged-right, not fully-justified, look in narrow columns
\begin{document}
\begin{table}[t]
\caption{Minimum Requirements for Automatic Readmission
into the Commerce Faculty} \label{table:mr}
%\centering % not needed, since table is as wide as text block
\begin{tabularx}{\textwidth}{@{}lYYYY@{}}
\toprule
&\multicolumn{2}{c}{\bfseries BCom}
&\multicolumn{2}{c}{\bfseries B.Bus Sci} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
&Number of courses required to pass
&Cumulative Total of Courses
& Number of courses
& Cumulative Total of Courses\\
\midrule
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
附录:顺便说一句,您在帖子中展示的表格的外观可以不是使用该包的命令即可实现。如果您确实需要所有这些垂直线和水平线,则booktabs
不应使用命令\toprule
、\bottomrule
和。相反,只需使用 basic-LaTeX指令即可。(当然,您仍然可以使用该包来简化表格第 2 列至第 5 列等宽列的创建。)\midrule
\cmidrule
\hline
tabularx
这些修改将导致表格如下所示。我希望你会同意“书本标签外观”——没有垂直线,总体上水平线较少,并且间隔适当——比所谓的“电子表格外观”要好得多。
\documentclass{article}
\usepackage{tabularx}
\usepackage[margin=1in]{geometry} % choose margin as needed
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[h!]
\caption{Minimum Requirements for Automatic Readmission
into the Commerce Faculty} \label{table:mr}
\begin{tabularx}{\textwidth}{|l|Y|Y|Y|Y|}
\hline
&\multicolumn{2}{c|}{\bfseries B. Com}
&\multicolumn{2}{c|}{\bfseries B. Bus. Sci} \\ \hline
& Number of courses required to pass
& Cumulative Total of Courses
& Number of courses required to pass
& Cumulative Total of Courses \\ \hline
First Year & 4 & 8 & 4 & 18 \\ \hline
Second Year & 10 & 16 & 11 & 16 \\ \hline
Third Year & 18 & 24 & 20 & 25 \\ \hline
\end{tabularx}
\end{table}
\end{document}
答案3
看看下面的内容:
\documentclass{scrartcl}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{p{0.2\textwidth}p{0.2\textwidth}p{0.2\textwidth}p{0.2\textwidth}p{0.2\textwidth}} \\ \toprule
& \multicolumn{2}{c}{BCom} & \multicolumn{2}{c}{B. Bus Scii} \\ \midrule
& Number of courses required to pass & Cumulative Total of Courses & Number of courses & Cumulative Total of Courses \\ \midrule
First year & 4 & 8 & 4 & 8 \\
Second year & 10 & 16 & 11 & 16 \\
Third year & 18 & 24 & 20 & 25 \\ \bottomrule
\end{tabular}
\end{table}
\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{p{0.18\textwidth}p{0.18\textwidth}p{0.18\textwidth}p{0.18\textwidth}p{0.18\textwidth}} \\ \toprule
& \multicolumn{2}{c}{BCom} & \multicolumn{2}{c}{B. Bus Scii} \\ \midrule
& \multicolumn{1}{p{3.5cm}}{Number of courses required to pass} & Cumulative Total of Courses & \multicolumn{1}{p{2cm}}{Number of courses} & Cumulative Total of Courses \\ \midrule
First year & 4 & 8 & 4 & 8 \\
Second year & 10 & 16 & 11 & 16 \\
Third year & 18 & 24 & 20 & 25 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
答案4
你可以尝试
\documentclass{amsart}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{@{}lp{3cm}p{3cm}p{3cm}p{3cm}@{}}
\toprule
&
\multicolumn{2}{c}{BCom} &
\multicolumn{2}{c}{B.Bus.Sci} \\
\cmidrule(r){2-3} \cmidrule(l){4-5}
\\
& Number of courses required to pass & Cumulative total of courses & Number of courses & Cumulative total of courses
\\
\midrule
\\
First year & 4 & 8 & 4 & 18 \\
\bottomrule
\end{tabular}
\label{table:mr}
\end{table}
\newcommand*{\head}[1]{\parbox{2cm}{\begin{flushright}#1\end{flushright}}}
\begin{table}
\caption{Minimum Requirements for Automatic Readmission into the Commerce Faculty}
\centering
\begin{tabular}{@{}lrrr@{}}
\toprule
& Year & \head{Courses required to pass} & \head{Cumulative total}
\\
\midrule
\\
BCom &
1 & 4 & 8 \\
& 2 & 4 & 8 \\
& 3 & 4 & 8 \\
B.Bus Sci &
1 & 4 & 18 \\
& 2 & 4 & 8 \\
& 3 & 4 & 8 \\
\bottomrule
\end{tabular}
\label{table:mr.2}
\end{table}
\end{document}