我想在表格中居中繪製內部垂直線。
範例程式碼:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{R} & \multicolumn{2}{c|}{No. Jobs Completed/Submitted} \\ \cline{2-3}
& C1 & C2 \\ \hline
r1 & a & b \\ \hline
r2 & a & b \\ \hline
\end{tabular}
\end{document}
其輸出:
想要的輸出:如果可能的話,我希望 C1 和 C2 列均等分割。
---------------------------------
|R |No. Jobs Completed/Submitted|
|r1| C1 | C2 |
|-------------------------------|
|r2| a | b |
--------------------------------
答案1
您可以使用套件w
中的 -column 類型array
:
\documentclass{article}
\usepackage{multirow,array}
\begin{document}
\setlength{\extrarowheight}{1pt}
\begin{tabular}{|c|w{c}{3cm}|w{c}{3cm}|}
\hline
\multirow{2}{*}{R} & \multicolumn{2}{c|}{No. Jobs Completed/Submitted} \\ \cline{2-3}
& C1 & C2 \\ \hline
r1 & a & b \\ \hline
r2 & a & b \\ \hline
\end{tabular}
\end{document}
答案2
透過該tabularray
包,您可以使用該hspan=even
選項來均勻分佈列:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
colspec={|c|c|c|},
hspan=even
}
\hline
\SetCell[r=2]{} R & \SetCell[c=2]{} No. Jobs Completed/Submitted & \\ \cline{2-3}
& C1 & C2 \\ \hline
r1 & a & b \\ \hline
r2 & a & b \\ \hline
\end{tblr}
\end{document}
答案3
與尼斯矩陣
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}[width=0.6\linewidth]{cX[c]X[c]}[hvlines,cell-space-limits=2pt]
\Block{2-1}{R} & \Block{1-2}{No. Jobs Completed/Submitted} & \\
& C1 & C2 \\
r1 & a & b \\
r2 & a & b \\
\end{NiceTabular}
\vspace{\baselineskip}
or
\vspace{\baselineskip}
\begin{NiceTabular}{c W{c}{3cm} W{c}{3cm}}[hvlines,cell-space-limits=2pt]
\Block{2-1}{R} & \Block{1-2}{No. Jobs Completed/Submitted} & \\
& C1 & C2 \\
r1 & a & b \\
r2 & a & b \\
\end{NiceTabular}
\end{document}
答案4
精神上與史蒂芬的回答,但是透過測量來使用表頭的自然寬度:
\documentclass{article}
\usepackage{multirow, array}
\newlength\mylength
\begin{document}
\settowidth\mylength{No. Jobs Completed/Submitted}
\mylength=.5\dimexpr\mylength-\arrayrulewidth-2\tabcolsep\relax
\begin{tabular}{|c|*2{w{c}{\mylength}|}}
\hline
\multirow{2}{*}{R} & \multicolumn{2}{c|}{No. Jobs Completed/Submitted} \\ \cline{2-3}
& C1 & C2 \\ \hline
r1 & a & b \\ \hline
r2 & a & b \\ \hline
\end{tabular}
\end{document}
booktabs
恕我直言,透過刪除所有垂直規則並使用水平規則來美化表格的版本:
\documentclass{article}
\usepackage{multirow, booktabs, array}
\newlength\mylength
\begin{document}
\settowidth\mylength{No. Jobs Completed/Submitted}
\mylength=.5\dimexpr\mylength-2\tabcolsep\relax
\begin{tabular}{c*2{w{c}{\mylength}}}
\toprule
\multirow{2}{*}{R} & \multicolumn{2}{c}{No. Jobs Completed/Submitted} \\
& C1 & C2 \\
\midrule
r1 & a & b \\
r2 & a & b \\
\bottomrule
\end{tabular}
\end{document}