在上一个问题中(如何在使用命令时强制类型X
列包装其内容?\multicolumn
),我要求提供一个允许我使用\multicolumn
跨类型X
列的命令的代码。Zarko 为我提供了一个代码,解决了我的问题
multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep\relax}C}{% Time Needed With Our Approach \newline (Extended Brown's Method)}
受他的回答的启发,我编写了一个自定义命令如下
\newcommand{\multcolhsize}[1]{\dimexpr #1\hsize + #1\tabcolsep + \tabcolsep \relax}
这使得 Zarko 提供的原始代码能够简洁地应用
\multicolumn{n}{>{\hsize=\multcolhsize{n}\}X}{<text>}
但是,现在使用代码时,\justifying\arraybackslash
在类型列的序言中使用时,文本似乎没有正确对齐X
。同样,在使用时,它似乎没有准确地移动到中心\centering\arraybackslash
显然,自定义命令可以进一步改进,以保证它准确地到达中心或正确调整指定的列。
\documentclass{book}
\usepackage[a4paper]{geometry}
\geometry{twoside=true, showframe=false}
% Horizontal
\geometry{inner=10mm, outer=10mm, includemp=true, bindingoffset=5mm, marginparsep=3.5mm, marginparwidth=15mm}
% Vertical
\geometry{top=20mm, vmarginratio=3:5, includehead=true, includefoot=true, headheight=8pt, headsep=14pt, footskip=5mm}
\usepackage{ragged2e}
\usepackage{tabularx}
\def \tabularxcolumn#1{m{#1}}
\newcolumntype{C}{>{\arraybackslash\Centering}X}
\newcommand{\multcolhsize}[1]{\dimexpr #1\hsize + #1\tabcolsep + \tabcolsep \relax}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{adjustbox}
\begin{document}
\begin{table*}[htp]
\def \tabularxcolumn#1{p{#1}}
\caption{Caption}
\centering
\begin{tabularx}{\textwidth}{r p{3cm} *{10}{C}}
\toprule
&
&
\multicolumn{10}{>{\hsize=\multcolhsize{10}\centering\arraybackslash}X}{Complexity}
\\
\cmidrule(l){3-12}
&
Description &
\adjustbox{rotate=90}{Method 1} &
\adjustbox{rotate=90}{Method 2} &
\adjustbox{rotate=90}{Method 3} &
\adjustbox{rotate=90}{Method 4} &
\adjustbox{rotate=90}{Method 5} &
\adjustbox{rotate=90}{Method 6} &
\adjustbox{rotate=90}{Method 7} &
\adjustbox{rotate=90}{Method 8} &
\adjustbox{rotate=90}{Method 9} &
\adjustbox{rotate=90}{Method 10}
\\
\cmidrule(r){2-2} \cmidrule(l){3-12}
1. &
Description A &
VS &
S &
VS &
S &
VS &
S &
C &
VC &
C &
VC
\\
\addlinespace[0.1cm]
2. &
Description B &
S &
C &
S &
C &
S &
C &
S &
C &
S &
C
\\
\addlinespace[0.1cm]
3. &
Description C &
N &
N &
N &
N &
N &
N &
Y &
VC &
Y &
VC
\\
\addlinespace[0.1cm]
\bottomrule
\end{tabularx}
\end{table*}
\end{document}
答案1
你需要改变
\newcommand{\multcolhsize}[1]{\dimexpr #1\hsize + #1\tabcolsep + \tabcolsep \relax}
到
\newcommand{\multcolhsize}[2]{\dimexpr #1\hsize + #2\tabcolsep \relax}
然后,在
\multicolumn{10}{>{\hsize=\multcolhsize{10}\centering\arraybackslash}X}{Complexity}
声明,你需要改变
\hsize=\multcolhsize{10}
到
\hsize=\multcolhsize{10}{18}
但是,由于列标题“复杂性”很短,因此您可以这样写
\multicolumn{10}{c}{Complexity}
你可能会问, “为什么18
在 中插入\multcolhsize{10}{18}
?”默认情况下,LaTeX 会自动在\tabcolsep
任何表格类型的列的两侧插入 中的空格。空格的总量之间因此两列的宽度为2\tabcolsep
。要计算列的总可用宽度\multicolumn{10}{...}{...}
,必须同时考虑 10\hsize
和2*9=18 \tabcolsep
。
附录:掌握了这些信息,让我们回到@Zarko 的回答,他写道
\multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep\relax}C}{...}
由于跨越了三列,3\hsize
所以一定是正确的。但是对于三列,需要考虑两个列间距,每个列的宽度为2\tabcolsep
。因此,总可用宽度确实等于3\hsize+4\tabcolsep
。
最后,这是您的 MWE 调整形式的输出。
\documentclass{book}
\usepackage{geometry}
\geometry{a4paper,twoside=true}
% Horizontal
\geometry{inner=10mm, outer=10mm, includemp=true, bindingoffset=5mm,
marginparsep=3.5mm, marginparwidth=15mm}
% Vertical
\geometry{top=20mm, vmarginratio=3:5, includehead=true,
includefoot=true, headheight=8pt, headsep=14pt, footskip=5mm}
\usepackage{ragged2e}
\usepackage{tabularx}
\def\tabularxcolumn#1{m{#1}}
\newcolumntype{C}{>{\arraybackslash\Centering}X}
% Another way to define '\multcolhsize', with just 1 argument
\newcommand{\multcolhsize}[1]{\dimexpr%
#1\hsize+#1\tabcolsep+#1\tabcolsep-2\tabcolsep\relax}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{adjustbox}
\begin{document}
\begin{table}[htp]
\def\tabularxcolumn#1{p{#1}}
\caption{Caption}
\centering
\begin{tabularx}{\textwidth}{r p{3cm} *{10}{C}}
\toprule
& & \multicolumn{10}{>{\hsize=\multcolhsize{10}\Centering}X}{Complexity}
\\
\cmidrule(l){3-12}
& Description &
\adjustbox{rotate=90}{Method 1} &
\adjustbox{rotate=90}{Method 2} &
\adjustbox{rotate=90}{Method 3} &
\adjustbox{rotate=90}{Method 4} &
\adjustbox{rotate=90}{Method 5} &
\adjustbox{rotate=90}{Method 6} &
\adjustbox{rotate=90}{Method 7} &
\adjustbox{rotate=90}{Method 8} &
\adjustbox{rotate=90}{Method 9} &
\adjustbox{rotate=90}{Method 10} \\
\midrule
1. & Description A & VS & S & VS & S & VS & S & C & VC & C & VC \\
\addlinespace
2. & Description B & S & C & S & C & S & C & S & C & S & C \\
\addlinespace
3. & Description C & N & N & N & N & N & N & Y & VC & Y & VC \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
tblr
有了新的 LaTeX3 包环境,一切都变得轻而易举tabularray
:多列、X
列和单元格居中工作符合预期。
请注意,您需要将命令l
的修剪选项\cmidrule
放在方括号中。
\documentclass{book}
\usepackage{geometry}
\geometry{a4paper,twoside=true}
% Horizontal
\geometry{inner=10mm, outer=10mm, includemp=true, bindingoffset=5mm,
marginparsep=3.5mm, marginparwidth=15mm}
% Vertical
\geometry{top=20mm, vmarginratio=3:5, includehead=true,
includefoot=true, headheight=8pt, headsep=14pt, footskip=5mm}
\usepackage{adjustbox}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[htp]
\caption{Caption}
\centering
\begin{tblr}{r p{3cm} *{10}{X[c]}}
\toprule
& & \SetCell[c=10]{c} Complexity & & & & & & & & &
\\
\cmidrule[l]{3-12}
& Description &
\adjustbox{rotate=90}{Method 1} &
\adjustbox{rotate=90}{Method 2} &
\adjustbox{rotate=90}{Method 3} &
\adjustbox{rotate=90}{Method 4} &
\adjustbox{rotate=90}{Method 5} &
\adjustbox{rotate=90}{Method 6} &
\adjustbox{rotate=90}{Method 7} &
\adjustbox{rotate=90}{Method 8} &
\adjustbox{rotate=90}{Method 9} &
\adjustbox{rotate=90}{Method 10} \\
\midrule
1. & Description A & VS & S & VS & S & VS & S & C & VC & C & VC \\
\addlinespace
2. & Description B & S & C & S & C & S & C & S & C & S & C \\
\addlinespace
3. & Description C & N & N & N & N & N & N & Y & VC & Y & VC \\
\bottomrule
\end{tblr}
\end{table}
\end{document}
答案3
我不确定为什么要使用tabularx
。这似乎更像是一份工作tabular*
。
\documentclass{book}
\usepackage[a4paper]{geometry}
\usepackage{ragged2e}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{adjustbox}
\geometry{
twoside=true,
showframe=false,
% Horizontal
inner=10mm,
outer=10mm,
includemp=true,
bindingoffset=5mm,
marginparsep=3.5mm,
marginparwidth=15mm,
% Vertical
top=20mm,
vmarginratio=3:5,
includehead=true,
includefoot=true,
headheight=8pt,
headsep=14pt,
footskip=5mm,
}
\begin{document}
\begin{table*}[htp]
\caption{Caption}
\centering
\begin{tabular*}{\textwidth}{r l @{\extracolsep{\fill}} *{10}{c}}
\toprule
&& \multicolumn{10}{c}{Complexity}
\\
\cmidrule(r){3-12}
&
Description &
\adjustbox{rotate=90}{Method 1} &
\adjustbox{rotate=90}{Method 2} &
\adjustbox{rotate=90}{Method 3} &
\adjustbox{rotate=90}{Method 4} &
\adjustbox{rotate=90}{Method 5} &
\adjustbox{rotate=90}{Method 6} &
\adjustbox{rotate=90}{Method 7} &
\adjustbox{rotate=90}{Method 8} &
\adjustbox{rotate=90}{Method 9} &
\adjustbox{rotate=90}{Method 10}
\\
\cmidrule(l){2-2} \cmidrule(r){3-12}
1. & Description A & VS & S & VS & S & VS & S & C & VC & C & VC \\
\addlinespace[0.1cm]
2. & Description B & S & C & S & C & S & C & S & C & S & C \\
\addlinespace[0.1cm]
3. & Description C & N & N & N & N & N & N & Y & VC & Y & VC \\
\addlinespace[0.1cm]
\bottomrule
\end{tabular*}
\end{table*}
\end{document}
不过,我会避免使用旋转的标题。
\begin{tabular*}{\textwidth}{r l @{\extracolsep{\fill}} *{10}{c}}
\toprule
& Description & \multicolumn{10}{c}{Complexity methods} \\
\cmidrule(r){3-12}
&& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\
\midrule
1. & Description A & VS & S & VS & S & VS & S & C & VC & C & VC \\
\addlinespace[0.1cm]
2. & Description B & S & C & S & C & S & C & S & C & S & C \\
\addlinespace[0.1cm]
3. & Description C & N & N & N & N & N & N & Y & VC & Y & VC \\
\addlinespace[0.1cm]
\bottomrule
\end{tabular*}