我想仅将下表中的数字居中。我尝试了一些居中命令的变体,但它们会影响整个表格(我对 Latex 还比较陌生)。
梅威瑟:
\documentclass{article}
\usepackage{booktabs, threeparttable}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabular}{@{}p{0.18\textwidth}*{6}{L{\dimexpr0.15\textwidth-2\tabcolsep\relax}}@{}}
\toprule
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c}{\bfseries Science Faculty} \\
\cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}
& F, S, H Courses & W Courses & F, S, H Courses & W Courses & F, S, H Courses & W Courses \\
\midrule
First-year & 18 & 36 & Variable & Variable & 18 & 36 \\
Second-year & 18 & 36 & Variable & Variable & 24 & 48 \\
Third-year & 18 & 36 & Variable & Variable & 36 & 72 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
答案1
您可以使用tabularx
环境 -- 预设宽度为\textwidth
-- 来代替tabular
环境。这样做可以让您省去繁琐(且容易出错)的手动计算第 2 列到第 7 列所需的宽度。在下面的示例中,L
列类型用于右侧不规则的材料,而C
列类型用于居中设置的材料;这两种列类型都基于包X
的列类型tabularx
。主列类型是,C
因为大多数行中的第 2 列到第 7 列应该居中;L
列类型用于列标题“F、S、H Courses”和“W Courses”;请注意,无需明确指定这些标题中的换行符。
在下图中, 下方的细水平线由\bottomrule
创建\hrule
;它放在那里纯粹是为了说明表格的宽度等于\textwidth
。
\documentclass{article}
\usepackage{booktabs, threeparttable}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabularx}{\textwidth}{@{}l*{6}{C}@{}}
\toprule
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c@{}}{\bfseries Science Faculty} \\ % note use of "c@{}"
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-7} % left- and right-trimming
& \multicolumn{1}{L}{F, S, H Courses} &
\multicolumn{1}{L}{W Courses} &
\multicolumn{1}{L}{F, S, H Courses} &
\multicolumn{1}{L}{W Courses}&
\multicolumn{1}{L}{F, S, H Courses} &
\multicolumn{1}{L@{}}{W Courses} \\ % note use of "L@{}"
\midrule
First-year & 18 & 36 & Variable & Variable & 18 & 36 \\
Second-year & 18 & 36 & Variable & Variable & 24 & 48 \\
Third-year & 18 & 36 & Variable & Variable & 36 & 72 \\
\bottomrule
\end{tabularx}
\end{threeparttable}
\end{table}
\hrule % just to indicate the width of the text block
\end{document}
答案2
为居中的列声明一个新的列类型(如果需要,您可以随时使用覆盖特定单元格的定义\multicolumn
):
\documentclass[draft]{article}
\usepackage{booktabs, threeparttable}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newlength\mylen
\setlength\mylen{\dimexpr0.135\textwidth-2\tabcolsep\relax}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\setlength\tabcolsep{5pt}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabular}{
@{}
p{0.18\textwidth}
*{2}{C{\mylen}}
*{2}{L{\mylen}}
*{2}{C{\mylen}}@{}
}
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c@{}}{\bfseries Science Faculty} \\
\cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}
& \multicolumn{1}{l}{F, S, H} & \multicolumn{1}{l}{W} & \multicolumn{1}{l}{F, S, H} & \multicolumn{1}{l}{W} & \multicolumn{1}{l}{F, S, H} & \multicolumn{1}{l@{}}{W} \\
& \multicolumn{1}{l}{Courses} & \multicolumn{1}{l}{Courses} & \multicolumn{1}{l}{Courses} & \multicolumn{1}{l}{Courses} & \multicolumn{1}{l}{Courses} & \multicolumn{1}{l@{}}{Courses} \\
\midrule
First-year & 18 & 36 & \small Variable & \small Variable & 18 & 36 \\
Second-year & 18 & 36 & \small Variable & \small Variable & 24 & 48 \\
Third-year & 18 & 36 & \small Variable & \small Variable & 36 & 72 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
为了防止表格突出到右边距,我重新计算了列宽并减少了\tabcolsep
。
答案3
使用该包的解决方案makecell
,可让您进一步自定义单元格。作为演示,我在以下代码中要求以粗体打印数字,并在单元格上方和下方添加一些垂直空间。此外,从第二列开始,每组两列的宽度相同 - 与第一组的宽度相同:
\documentclass{article}
\centering\setlength{\tabcolsep}{4.4pt}
\usepackage{booktabs, threeparttable}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\usepackage{makecell}
\renewcommand{\cellgape}{\bfseries\Gape[2pt]}
\renewcommand{\theadalign}{lc}
\renewcommand{\theadfont}{\normalsize}
\newlength{\headwd}
\settowidth{\headwd}{\bfseries Commerce Faculty}
\begin{document}
\begin{table}[htbp]
\begin{threeparttable}
\caption{Number of Credits per Course by Year}
\label{table:nc}
\begin{tabular}{@{}l*{6}{p{\dimexpr 0.5\headwd-\tabcolsep \relax}}@{}}
\addlinespace
\toprule
& \multicolumn{2}{c}{\bfseries Commerce Faculty} &
\multicolumn{2}{c}{\bfseries EBE Faculty} &
\multicolumn{2}{c}{\bfseries Science Faculty} \\
\cmidrule(lr){2-3}\cmidrule(lr){4-5} \cmidrule(lr){6-7}
&\thead{ F, S, H \\ Courses} & \thead{W\\ Courses }&\thead{ F, S, H \\ Courses } & \thead{W\\ Courses} &\thead{ F, S, H\\ Courses} & \thead{W\\
Courses} \\
\cmidrule(lr){2-3}\cmidrule(lr){4-5} \cmidrule(lr){6-7}
First-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{18} & \makecell{36} \\
Second-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{24} & \makecell{48} \\
Third-year & \makecell{18} & \makecell{36} & Variable & Variable & \makecell{36} & \makecell{72} \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
答案4
为什么?我不知道,但我喜欢tap
主意。混合 Plain TeX 和 LaTeX 可能还远远不够,但仍然可以玩一玩。
\documentclass{scrartcl}
\input{tap}
\usepackage{kantlipsum}
\begin{document}
\kant[1]
\medskip
\thistable{\desiredwidth\hsize}
%\moveright\parindent
\begintable
\begintableformat & \left \endtableformat
\B" ! @6 \= \E!
\B": ! @2 \textbf{Commerce Faculty} | @2 \textbf{EBE Faculty} | @2 \textbf{Science Faculty} \E!
\B" ! @6 \- \E!
\B"^ ! F, S, H | W | F, S, H | W | F, S, H | W \E!
\B"_ ! Courses | Courses | Courses | Courses | Courses | Courses \E!
\B! @7 \= \E!
\B!^ First-year | \center{18} | \center{36} | Variable | Variable | \center{18} | \center{36} \E!
\B!+ Second-year | \center{18} | \center{36} | Variable | Variable | \center{24} | \center{48} \E!
\B!_ Third-year | \center{18} | \center{36} | Variable | Variable | \center{36} | \center{72} \E!
\=
\endtable
\medskip
\kant[4]
\end{document}