我有一张包含复杂标题的表格,使用了multirow
和multicol
包。我想将下表的第二列设置为包S
类型,siunitx
以便按小数点分隔符对齐数字,但我失败了。有什么帮助吗?
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{multirow,multicol}
\begin{document}
\begin{table}[htb]
\footnotesize
\centering
\caption{Example Table}
\begin{tabular}{ccSS}% <--- HERE
\midrule
\multirow{2}{*}{\textbf{Column 1}} &
\multirow{2}{*}{\textbf{%
\begin{tabular}[c]
{@{}c@{}}Column 2\\
(\si{\g/\mL})
\end{tabular}}} &
\multicolumn{2}{c}{\textbf{Column 3 (\si{\ug/\L})}} \\
\cmidrule{3-4}& &
\textbf{Column 3.1} & \textbf{Column 3.2}\\
\midrule
AE1 & 3.45 & \multicolumn{ 2}{c}{Some text} \\
AE2 & 20.65 & 50 & 4.5 \\
AE3 & 12.67 & 20 & 7.5 \\
AE4 & 0.78 & 75 & 12.09 \\
AE5 & 3.098 & 100 & 14 \\
\midrule
\end{tabular}
\end{table}
\end{document}
答案1
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs, multirow}
\usepackage{siunitx}
\begin{document}
\begin{table}[htb]
\centering
%\footnotesize
\renewcommand\multirowsetup{\centering}% for centering text in multirow cells
\caption{Example Table}
\begin{tabular}{cS[table-format=2.3]
S[table-format=3.0]
S[table-format=2.2]}% <--- HERE added S column type
\toprule
\multirow{2}{*}{\textbf{Column 1}}
& {\multirow{2}{12ex}{\textbf{Column 2}\\(\si{\g/\mL})}} % enclosed in {...}
% with defined width is possible write text in more lines
& \multicolumn{2}{c}{\textbf{Column 3 (\si{\ug/\L})}} \\
\cmidrule{3-4}& &
\textbf{Column 3.1} & \textbf{Column 3.2} \\
\midrule
AE1 & 3.45 & \multicolumn{2}{c}{Some text} \\
AE2 & 20.65 & 50 & 4.5 \\
AE3 & 12.67 & 20 & 7.5 \\
AE4 & 0.78 & 75 & 12.09 \\
AE5 & 3.098 & 100 & 14 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
我认为您不应该降低“第 1 列”,因为它与第 2-4 列中的其他标题具有相同的等级。
这个\smash[b]{...}
技巧可以假装标题下方的单元不占用垂直空间。
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{booktabs}
\usepackage{siunitx}
\newcommand{\splitcell}[1]{%
\smash[b]{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}}%
}
\begin{document}
\begin{table}[htb]
\centering
\caption{Example Table}
\footnotesize % <-- are you sure?
\begin{tabular}{
@{}
l
S[table-format=2.3]
S[table-format=3.0]
S[table-format=2.2]
@{}
}
\toprule
\textbf{Column 1} &
{\splitcell{\bfseries Column 2 \\ (\si{\g/\mL})}} &
\multicolumn{2}{c}{\textbf{Column 3 (\si{\ug/\L})}} \\
\cmidrule(l){3-4}
&& \textbf{Column 3.1} & \textbf{Column 3.2}\\
\midrule
AE1 & 3.45 & \multicolumn{2}{c}{Some text} \\
AE2 & 20.65 & 50 & 4.5 \\
AE3 & 12.67 & 20 & 7.5 \\
AE4 & 0.78 & 75 & 12.09 \\
AE5 & 3.098 & 100 & 14 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
顶部和底部规则不应该是;如果您不喜欢和\midrule
的默认厚度,您可以通过添加来更改相关参数\toprule
\bottomrule
\setlength{\heavyrulewidth}{\lightrulewidth}
在文件序言中。