我创建了一个多列表格,每列上方都有标题(简化表见下面的代码)。我尝试对齐小数点,我已使用 siunitx 包成功完成此操作。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx} %%aligns by decimal points: https://tex.stackexchange.com/questions/2746/aligning-numbers-by-decimal-points-in-table-columns
\usepackage[singlelinecheck=false]{caption}
\usepackage{array}
\usepackage[document]{ragged2e}
\sisetup{
input-symbols = {()},
group-digits = false,
explicit-sign
}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\setlength{\tabcolsep}{8pt}
\hspace*{-2.5cm}
\begin{tabular}{@{}l
*{6}{S[table-format=-3.1]}@{}}
\toprule
& \multicolumn{3}{c}{ROC} &
\multicolumn{3}{c}{Quebec} \\
\cmidrule(lr){2-4}\cmidrule(lr){5-7}
Variables & Married & Cohabiting & Total &
Married & Cohabiting & Total \\ \midrule
Age difference^{ab} & & & &
& & \\
\quad 0-1 years & 36.4 & 26.3 & 35.3 &
40.7 & 28.1 & 34.9 \\
\quad Woman older (2+ years) & 11.3 & 17.0 & 11.9
& 10.3 & 11.9 & 11.0 \\
\quad Man older (2-4 years) & 27.6 & 28.8 & 27.8
& 24.0 & 37.8 & 30.3 \\
\quad Man older (5+ years) & 24.7 & 27.9 & 25.1
& 25.1 & 22.2 & 23.8 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
但是,当我的代码如下所示时,表格的标题会变得不稳定。
当我尝试修复该问题时,我最终会破坏小数点的排列方式。我尝试通过以下方法修复该问题:1) 在 \begin{tabular} 后的行中添加回“cccccc”,或者在指定小数点对齐位数的行中使用“table-number-alignment=center”。
我已经阅读了不少关于这个问题的问题,但似乎没有一个解决方案有效。我承认我对 LaTeX 还很陌生,所以如果有简单的答案,我深表歉意!
非常感谢您的帮助!谢谢。
答案1
是不是如你所愿呢?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{booktabs}
\usepackage{siunitx} %%aligns by decimal points: https://tex.stackexchange.com/questions/2746/aligning-numbers-by-decimal-points-in-table-columns
\usepackage[singlelinecheck=false]{caption}
\usepackage{array}
\usepackage[document]{ragged2e}
\sisetup{
input-symbols = {()},
group-digits = false,
explicit-sign
}
\begin{document}
\begin{table}[!htb]
\centering\captionsetup{margin = 2.5cm}
\caption{My caption}
\label{my-label}
\setlength{\tabcolsep}{8pt}
\captionsetup{margin=2.5cm}
\begin{tabular}{@{}l
*{6}{S[table-format=2.1,table-number-alignment=center]}@{}}
\toprule
& \multicolumn{3}{c}{ROC} &
\multicolumn{3}{c}{Quebec} \\
\cmidrule(lr){2-4}\cmidrule(lr){5-7}
{Variables} & {Married} & {Cohabiting} & {Total} &
{Married} & {Cohabiting} & {Total} \\ \midrule
Age difference$ ^{ab} $ \\[0.5ex]
\quad 0--1 years & 36.4 & 26.3 & 35.3 &
40.7 & 28.1 & 34.9 \\
\quad Woman older (2+ years) & 11.3 & 17.0 & 11.9
& 10.3 & 11.9 & 11.0 \\
\quad Man older (2--4 years) & 27.6 & 28.8 & 27.8
& 24.0 & 37.8 & 30.3 \\
\quad Man older (5+ years) & 24.7 & 27.9 & 25.1
& 25.1 & 22.2 & 23.8 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}