
在我的中table
,為了使文本在每個單元格中居中,我使用
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
p{1.5cm}
我使用的不是簡單的
>{\centering\arraybackslash}p{1.5cm}
我也在使用\toprule
, \midrule
,\bottomrule
和\usepackage{booktabs}
。
我的程式碼如下:
\documentclass[12pt]{article}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{| >{\centering\arraybackslash}p{1.5cm} | >{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm} |>{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm} | >{\centering\arraybackslash}p{1.2cm}|}
\toprule
\multirow{2}{*}{} & \multicolumn{4}{c|}{Payoffs} & \multicolumn{4}{c}{ROR} \\
\cline{2-9}
& $\theta =1$ & $\theta =2$ & $E(P)$ & $\sigma(P)$ & $\theta =1$ & $\theta =2$ & $E(r)$ & $\sigma(r)$ \\
\midrule
Asset 1 & 110 & 95 & & & & & & \\
Asset 2 & 105 & 90 & & & & & & \\
Asset 3 & 120 & 90 & & & & & & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
我的結果看起來很奇怪,如下所示:
首先,最右邊的垂直線短;第一行右側未封閉。
其次,所有垂直線都被剪掉。
有什麼想法嗎?
答案1
右邊的豎線不是短,是少了,因為你用過
\multicolumn{4}{c}{ROR}
代替
\multicolumn{4}{c|}{ROR}
但是,不要添加垂直規則,而是遵循指引建議來自booktabs
(因為無論如何你都在使用它):
如果您始終記住兩個簡單的指導原則,您就不會犯下太大的錯誤:
- 永遠、永遠不要使用垂直規則。
- 切勿使用雙重規則。
\documentclass{article}
\usepackage{booktabs,array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{tabular}{ P{15mm} *{8}{P{12mm}} }
\toprule
& \multicolumn{4}{c}{Payoffs} & \multicolumn{4}{c}{ROR} \\
\cmidrule(lr){2-5}\cmidrule(lr){6-9}
& $\theta = 1$ & $\theta = 2$ & $E(P)$ & $\sigma(P)$ & $\theta = 1$ & $\theta = 2$ & $E(r)$ & $\sigma(r)$ \\
\midrule
Asset 1 & 110 & 95 & & & & & & \\
Asset 2 & 105 & 90 & & & & & & \\
Asset 3 & 120 & 90 & & & & & & \\
\bottomrule
\end{tabular}
\end{document}
值的柱狀堆疊促進視覺對齊,從而在一定程度上使用垂直規則過時的。
答案2
您不能將 booktabs
規則與垂直規則一起使用,因為 booktabs 在其水平規則周圍添加了一些垂直填充。出於同樣的原因,嘗試在帶有書籤的表格單元格中使用顏色時會遇到問題。
您可以替換booktabs
為boldlines
,它允許表格中的可變寬度規則,以模擬不同類型的書籤規則。然而,許多人認為,如果僅使用水平線,您的表格看起來會更好。
我在下面的程式碼中給出了兩種解決方案的範例。請注意,您不需要用於\multirow
空白儲存格。
\documentclass[12pt]{article}
\usepackage{array, boldline}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{booktabs}
\begin{document}
\begin{table}[!h]
\centering\renewcommand\arraystretch{1.25}
\begin{tabular}{|P{1.5cm} | *{8}{P{1.2cm} |}}
\hlineB{2}
& \multicolumn{4}{c|}{Payoffs} & \multicolumn{4}{c|}{ROR} \\
\cline{2-9}
& $\theta =1$ & $\theta =2$ & $E(P)$ & $\sigma(P)$ & $\theta =1$ & $\theta =2$ & $E(r)$ & $\sigma(r)$ \\
\hlineB{2}
Asset 1 & 110 & 95 & & & & & & \\
Asset 2 & 105 & 90 & & & & & & \\
Asset 3 & 120 & 90 & & & & & & \\
\hlineB{2}
\end{tabular}
\end{table}
\vskip1cm
\begin{table}[!h]
\centering\renewcommand\arraystretch{1.25}
\begin{tabular}{P{1.5cm}*{8}{P{1.2cm}}}
& \multicolumn{4}{c}{Payoffs} & \multicolumn{4}{c}{ROR} \\
\cmidrule[0.6pt](lr){2-5}\cmidrule[0.6pt](lr){6-9}
& $\theta =1$ & $\theta =2$ & $E(P)$ & $\sigma(P)$ & $\theta =1$ & $\theta =2$ & $E(r)$ & $\sigma(r)$ \\
\midrule[\heavyrulewidth]
Asset 1 & 110 & 95 & & & & & & \\
Asset 2 & 105 & 90 & & & & & & \\
Asset 3 & 120 & 90 & & & & & & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}