如何修复 siunitx 表中的对齐?

如何修复 siunitx 表中的对齐?

我创建了下表: 在此处输入图片描述

我正在使用 siunitx,table-format=3.2因为我有 2dp 的数字。

不同数量级的数字组合产生了非常丑陋的效果。

我想要某种方法将第 1-2 行对齐在一起,然后“重置”表格并将第 3-5 行对齐在一起,然后再次重置表格并将第 6-9 行对齐在一起。

我怎样才能实现这个目标?


上述代码:

 % these define the type of document and the look of the page
\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=3.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabular}

\end{document}

答案1

将表拆分为三个环境,并对每个环境tabular使用不同的:table-formattabular

附注:请注意,生成的表格将比文本宽度宽 2pt。您可能需要更正这一点。

在此处输入图片描述

\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\end{tabular}
\sisetup{table-format=5, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\end{tabular}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabular}

\end{document}

使用tabularx和 其X类型列,您可以防止表格比文本宽度更宽,同时避免手动计算所需的宽度。我定义了一种新的Y列类型,其内容左对齐。\renewcommand\tabularxcolumn[1]{m{#1}}我已确保XY列内容垂直居中,以匹配L您的示例中类型列的原始定义:

\documentclass[]{article}
\usepackage[margin=0.8in, top = 1.3in, headheight = 0.6in]{geometry}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\setlength{\parindent}{0pt} % space at start of paragraph
\setlength{\parskip}{0.14in} % space between paragraphs

% colors
\usepackage[table]{xcolor}
\definecolor{bg3}{HTML}{ECECEC} % t gray = gray!15!white

% table-related
\usepackage{array}
\usepackage{booktabs}
\usepackage{multicol, makecell}
\usepackage{multirow, hhline}
\usepackage{siunitx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Y}{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}X}
% math
\usepackage{amsmath}

\begin{document}

\pagecolor{bg3}
\newcommand{\colcel}{\cellcolor{white}}
\def\arraystretch{1.5}

\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\multicolumn{6}{l}{Title} \\
\Xhline{1.2pt}
& \multicolumn{4}{c|}{2019} & { 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & { 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 &  11.1 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 &  1.3 \\
\end{tabularx}
\sisetup{table-format=5, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 1} \\ \hhline{~-----}
Row 3 & 12438 & 12009 & 12701 & 13159 &  19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 &  6665 \\
Row 5 & 164 & 322 & 820 & 0 &  2174 \\
\end{tabularx}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabularx}{\textwidth}{Y|S|*{3}{S|}S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 &  -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 &  -0.04 \\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 &  1 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 &  -30.2 \\ \Xhline{1.2pt}
\end{tabularx}

\end{document}

相关内容