在以下机构的帮助下这个答案,我创建了下表:
问题在于使用\cellcolor
会导致siunitx
对齐被忽略,因为我将该单元格的内容括在里面{ }
(如果我不这样做,颜色会溢出,事情就会变得疯狂)。
我该如何解决?
上述代码:
% 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{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.1, 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} & {\colcel 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & {\colcel 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 & {\colcel 11.3} \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 & {\colcel 1.3} \\
\end{tabular}
\vspace*{-0.175in}
\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 & {\colcel 19696} \\
Row 4 & 6601 & 6206 & 6745 & 7256 & {\colcel 6665} \\
Row 5 & 164 & 322 & 820 & 0 & {\colcel 2174} \\
\end{tabular}
\vspace*{-0.175in}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|S|S[table-format=-1.2]|S|S}
\hhline{~-----} & \multicolumn{5}{c}{Heading 2} \\ \hhline{~-----}
Row 6 & -0.8 & -0.3 & 0.2 & 0.3 & {\colcel -0.4} \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 & {\colcel -0.04}\\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 & {\colcel 1.0} \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 & {\colcel -30.2} \\ \Xhline{1.2pt}
\end{tabular}
\end{document}
答案1
稍微改变一下结尾的位置}
就可以了:例如:{\colcel} 11.3
改为 og {\colcel 11.3 }
。
背景:您可以将非数字内容放在一组中{}
以将其“隐藏” siunitx
。如果您将\colcell
和数字放在一组中,{}
则基本上会隐藏数字以siunitx
防止对齐正确进行。如果您只是将一组放在命令{}
周围\colcell
,那么您只会隐藏该命令,而不是隐藏命令和数字。
如果您打算为整个列着色,这\columncolor
可能是更好的选择。
\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{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.1, 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} & {\colcel 2020} \\
\hhline{~-----}
& {1Q} & {2Q} & {3Q} & {4Q} & {\colcel 1Q} \\
\hline
Row 1 & 11.5 & 13 & 12.9 & 13 & {\colcel} 11.3 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 & {\colcel} 1.3 \\
\end{tabularx}
\vspace*{-0.175in}
\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 & {\colcel} 19696 \\
Row 4 & 6601 & 6206 & 6745 & 7256 & {\colcel} 6665 \\
Row 5 & 164 & 322 & 820 & 0 & {\colcel} 2174 \\
\end{tabularx}
\vspace*{-0.175in}
\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 & {\colcel} -0.4 \\
Row 7 & -0.08 & -0.01 & 0.04 & 0.06 & {\colcel} -0.04\\
Row 8 & 0.8 & -9.9 & -5.5 & 4.3 & {\colcel} 1.0 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 & {\colcel} -30.2 \\ \Xhline{1.2pt}
\end{tabularx}
\end{document}
答案2
只需使用\columncolor
而不是对单个单元格进行着色。我不知道为什么会发生最初的问题,但这解决了它。
输出:
代码:
% 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{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.1, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|S|S|S|>{\columncolor{white}}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.3 \\
Row 2 & 1.3 & 1.5 & 1.5 & 1.5 & 1.3 \\
\end{tabular}
\vspace*{-0.175in}
\sisetup{table-format=5, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|S|S|S|>{\columncolor{white}}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}
\vspace*{-0.175in}
\sisetup{table-format=-2.2, table-number-alignment=center, table-column-width=1.6cm}
\begin{tabular}{L{7cm}|S|S|S[table-format=-1.2]|S|>{\columncolor{white}}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.0 \\
Row 9 & 9.7 & -8.3 & -8.1 & 16.7 & -30.2 \\ \Xhline{1.2pt}
\end{tabular}
\end{document}