我只想將下表中的數字置中。我嘗試了居中命令的一些變體,但它們會影響整個表格(我對 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 課程”和“W 課程”;請注意,無需在這些標頭中明確指定換行符。
在下圖中, 下方的細水平線是由;\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}