Siunitx:如何处理无效的数字输入‘-’?

Siunitx:如何处理无效的数字输入‘-’?

感谢大家,我找到了siunitx小数对齐。但是,我的相关矩阵中的某些列没有任何数字,而是“-”。siunitx 给我一个关于无效数字输入的错误。我该如何解决这个问题?

这是我的代码:

\documentclass[6pt]{article}
\usepackage[landscape, total={8.5in, 5.45in}, top=1.5in, bottom=1.25in, right=1.
25in, left=1.25in, centering]{geometry}
\usepackage{longtable}
\usepackage{bm}
\usepackage[table]{xcolor}
\usepackage[none]{hyphenat}
\usepackage[T1]{fontenc}
\usepackage[default]{cantarell}
\usepackage{booktabs}
\usepackage{rotating}
\usepackage{siunitx}
\usepackage{array}
\definecolor{tableShade8}{HTML}{78C679}
\definecolor{tableShade9}{HTML}{ADDD8E}
\definecolor{tableShade10}{HTML}{D7E9C0}
\definecolor{tableShade7}{HTML}{F7FCB9}
\definecolor{tableShade2}{HTML}{F1F5FA}
\definecolor{tableShade3}{HTML}{dddddd}

\pagestyle{empty}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand{\arraystretch}{1.2}
\usepackage{arydshln}
\arrayrulecolor{tableShade3}

% this is the column for rotated headers
\newcolumntype{R}[1]{>{\begin{turn}{90}\begin{minipage}{#1}\footnotesize}l%
<{\end{minipage}\end{turn}}%
}
\begin{document}

\begin{center}
\textbf{Tricky longtable with rotated headers for a correlation matrix}\end{center}
\setlength\LTleft{0in}
\setlength\LTright{0in}
\setlength\LTpre{-0.3cm}
\setlength\LTpost{0in}
\rowcolors{1}{tableShade2}{white}
\setlength{\tabcolsep}{8pt}
\centering

\begin{longtable}{lS[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]
               S[table-format=+1.2]}

\hiderowcolors
&&\multicolumn{7}{c}{First Group}&\multicolumn{4}{c}{Second Group}\\\cmidrule(lr){3-9}\cmidrule(lr){10-13}
&\multicolumn{1}{R{2.4cm}}{A long heading}
&\multicolumn{1}{R{2.4cm}}{Another \\ long heading}
&\multicolumn{1}{R{2.4cm}}{Third super long \\long heading}
&\multicolumn{1}{R{2.4cm}}{Fourth heading}
&\multicolumn{1}{R{2.4cm}}{Fifth blah\\loooooong blah}
&\multicolumn{1}{R{2.4cm}}{And another \\ heading}
&\multicolumn{1}{R{2.4cm}}{As if that \\ was not enough}
&\multicolumn{1}{R{2.4cm}}{And so it \\ keeps on going}
&\multicolumn{1}{R{2.4cm}}{When exactly \\ will this end?}
&\multicolumn{1}{R{2.4cm}}{Not exactly \\minimal is it?}
&\multicolumn{1}{R{2.4cm}}{Welcome to my\\ longtable hell}
&\multicolumn{1}{R{2.4cm}}{And this is it\\ I promise}\\
\specialrule{0.01em}{0.0em}{0em}
\endhead
\specialrule{0.01em}{0.0em}{0em}
\noalign{\raggedright
\footnotesize
%Complicated legend begins
\begin{tabular}{llllllllll}
\addlinespace[0.2em]
\hiderowcolors
\multicolumn{6}{l}{\textbf{LEGEND}}\\
{\cellcolor{tableShade8}$\ge$0.8}&:Very high&
{\cellcolor{tableShade9}0.60-0.79}&:High&
{\cellcolor{tableShade10}0.40-0.59}&:Moderate&
{\cellcolor{tableShade7}0.20-0.39}&:Low&
{\cellcolor{tableShade2}$\le$0.19}&:Very low\\
\end{tabular}
}

\endfoot

\multicolumn{13}{l}{\textbf{A subheading for the data}}\\\hline
\showrowcolors
First variable&{\cellcolor{tableShade8}1}&{\cellcolor{tableShade9}-0.72}&{\cellcolor{tableShade2}0.07}&{\cellcolor{tableShade2}0.08}&{\cellcolor{tableShade2}0.04}&{\cellcolor{tableShade2}-0.05}&{\cellcolor{tableShade2}0.14}&{\cellcolor{tableShade2}-0.16}&{\cellcolor{tableShade2}0.13}&{\cellcolor{tableShade2}-0.05}&{\cellcolor{tableShade10}0.58}&{\cellcolor{tableShade10}0.55}\\

Second variable &{\cellcolor{tableShade9}-0.72}&{\cellcolor{tableShade8}1}&{\cellcolor{tableShade2}-0.07}&{\cellcolor{tableShade2}-0.07}&{\cellcolor{tableShade2}-0.01}&{\cellcolor{tableShade2}0.06}&{\cellcolor{tableShade2}-0.09}&{\cellcolor{tableShade2}0.1}&{\cellcolor{tableShade2}-0.09}&{\cellcolor{tableShade2}0.03}&{\cellcolor{tableShade7}-0.32}&{\cellcolor{tableShade7}-0.31}\\

Third variable &{\cellcolor{tableShade2}0.07}&{\cellcolor{tableShade2}-0.07}&{\cellcolor{tableShade8}1}&{\cellcolor{tableShade2}0.06}&{\cellcolor{tableShade2}0.06}&{\cellcolor{tableShade2}0.05}&{\cellcolor{tableShade2}0.07}&{\cellcolor{tableShade2}-0.08}&{\cellcolor{tableShade2}0.06}&{\cellcolor{tableShade2}-0.09}&{\cellcolor{tableShade2}-0.16}&{\cellcolor{tableShade2}-0.19}\\

Whoa what is this&{\cellcolor{tableShade2}0.08}&{\cellcolor{tableShade2}-0.07}&{\cellcolor{tableShade2}0.06}&{\cellcolor{tableShade8}1}&-&-&-&-&-&-&{\cellcolor{tableShade2}0.07}&{\cellcolor{tableShade2}-0.01}\\

Fourth Variable&{\cellcolor{tableShade2}0.04}&{\cellcolor{tableShade2}-0.01}&{\cellcolor{tableShade2}0.06}&-&{\cellcolor{tableShade8}1}&{\cellcolor{tableShade8}0.86}&{\cellcolor{tableShade7}-0.32}&{\cellcolor{tableShade2}-0.11}&{\cellcolor{tableShade7}-0.32}&{\cellcolor{tableShade2}-0.16}&{\cellcolor{tableShade2}0.07}&{\cellcolor{tableShade2}0.12}\\

\end{longtable}
\end{document}

如您所见,我收到一条关于 !! 无效数字输入“-”的错误。我查看了 siunitx 手册,发现很难解决此错误。任何帮助都非常感谢!感谢您的时间。

参考:

1. 关于 'dcolumn' 的问题以及siunitxtex.stackexchange

2.siunitx加粗数字

3.siunitx手册

答案1

拿出你的代码,删除错误的括号,并在破折号的“非数字”输入周围添加括号,会导致

\documentclass[6pt]{article}
\usepackage[landscape, total={8.5in, 5.45in}, top=1.5in, bottom=1.25in, right=1.
25in, left=1.25in, centering]{geometry}
\usepackage{longtable}
\usepackage{bm}
\usepackage[table]{xcolor}
\usepackage[none]{hyphenat}
\usepackage[T1]{fontenc}
\usepackage[default]{cantarell}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{rotating}
\usepackage{siunitx}
\usepackage{array}
\definecolor{tableShade8}{HTML}{78C679}
\definecolor{tableShade9}{HTML}{ADDD8E}
\definecolor{tableShade10}{HTML}{D7E9C0}
\definecolor{tableShade7}{HTML}{F7FCB9}
\definecolor{tableShade2}{HTML}{F1F5FA}
\definecolor{tableShade3}{HTML}{dddddd}

\pagestyle{empty}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand{\arraystretch}{1.2}
\usepackage{arydshln}
\arrayrulecolor{tableShade3}
\usepackage{etoolbox}
\robustify\cellcolor

% this is the column for rotated headers
\newcolumntype{R}[1]{>{\begin{turn}{90}\begin{minipage}{#1}\footnotesize}l%
<{\end{minipage}\end{turn}}%
}
\begin{document}

\begin{center}
\textbf{Tricky longtable with rotated headers for a correlation matrix}\end{center}
\setlength\LTleft{0in}
\setlength\LTright{0in}
\setlength\LTpre{-0.3cm}
\setlength\LTpost{0in}
\rowcolors{1}{tableShade2}{white}
\setlength{\tabcolsep}{8pt}
\centering

\begin{longtable}{lS[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]
               S[table-format=-1.2]}

\hiderowcolors
&&\multicolumn{7}{c}{First Group}&\multicolumn{4}{c}{Second Group}\\\cmidrule(lr){3-9}\cmidrule(lr){10-13}
&\multicolumn{1}{R{2.4cm}}{A long heading}
&\multicolumn{1}{R{2.4cm}}{Another \\ long heading}
&\multicolumn{1}{R{2.4cm}}{Third super long \\long heading}
&\multicolumn{1}{R{2.4cm}}{Fourth heading}
&\multicolumn{1}{R{2.4cm}}{Fifth blah\\loooooong blah}
&\multicolumn{1}{R{2.4cm}}{And another \\ heading}
&\multicolumn{1}{R{2.4cm}}{As if that \\ was not enough}
&\multicolumn{1}{R{2.4cm}}{And so it \\ keeps on going}
&\multicolumn{1}{R{2.4cm}}{When exactly \\ will this end?}
&\multicolumn{1}{R{2.4cm}}{Not exactly \\minimal is it?}
&\multicolumn{1}{R{2.4cm}}{Welcome to my\\ longtable hell}
&\multicolumn{1}{R{2.4cm}}{And this is it\\ I promise}\\
\specialrule{0.01em}{0.0em}{0em}
\endhead
\specialrule{0.01em}{0.0em}{0em}
\noalign{\raggedright
\footnotesize
%Complicated legend begins
\begin{tabular}{llllllllll}
\addlinespace[0.2em]
\hiderowcolors
\multicolumn{6}{l}{\textbf{LEGEND}}\\
{\cellcolor{tableShade8}$\ge$0.8}&:Very high&
{\cellcolor{tableShade9}0.60--0.79}&:High&
{\cellcolor{tableShade10}0.40--0.59}&:Moderate&
{\cellcolor{tableShade7}0.20--0.39}&:Low&
{\cellcolor{tableShade2}$\le$0.19}&:Very low\\
\end{tabular}
}

\endfoot

\multicolumn{13}{l}{\textbf{A subheading for the data}}\\\hline
\showrowcolors
First variable&\cellcolor{tableShade8}1&\cellcolor{tableShade9}-0.72&\cellcolor{tableShade2}0.07&\cellcolor{tableShade2}0.08&\cellcolor{tableShade2}0.04&\cellcolor{tableShade2}-0.05&\cellcolor{tableShade2}0.14&\cellcolor{tableShade2}-0.16&\cellcolor{tableShade2}0.13&\cellcolor{tableShade2}-0.05&\cellcolor{tableShade10}0.58&\cellcolor{tableShade10}0.55\\

Second variable &\cellcolor{tableShade9}-0.72&\cellcolor{tableShade8}1&\cellcolor{tableShade2}-0.07&\cellcolor{tableShade2}-0.07&\cellcolor{tableShade2}-0.01&\cellcolor{tableShade2}0.06&\cellcolor{tableShade2}-0.09&\cellcolor{tableShade2}0.1&\cellcolor{tableShade2}-0.09&\cellcolor{tableShade2}0.03&\cellcolor{tableShade7}-0.32&\cellcolor{tableShade7}-0.31\\

Third variable &\cellcolor{tableShade2}0.07&\cellcolor{tableShade2}-0.07&\cellcolor{tableShade8}1&\cellcolor{tableShade2}0.06&\cellcolor{tableShade2}0.06&\cellcolor{tableShade2}0.05&\cellcolor{tableShade2}0.07&\cellcolor{tableShade2}-0.08&\cellcolor{tableShade2}0.06&\cellcolor{tableShade2}-0.09&\cellcolor{tableShade2}-0.16&\cellcolor{tableShade2}-0.19\\

Whoa what is this&\cellcolor{tableShade2}0.08&\cellcolor{tableShade2}-0.07&\cellcolor{tableShade2}0.06&\cellcolor{tableShade8}1
&{--}&{--}&{--}&{--}&{--}&{--}&\cellcolor{tableShade2}0.07&\cellcolor{tableShade2}-0.01\\

Fourth Variable&\cellcolor{tableShade2}0.04&\cellcolor{tableShade2}-0.01&\cellcolor{tableShade2}0.06
&{--}&\cellcolor{tableShade8}1&\cellcolor{tableShade8}0.86&\cellcolor{tableShade7}-0.32&\cellcolor{tableShade2}-0.11&\cellcolor{tableShade7}-0.32&\cellcolor{tableShade2}-0.16&\cellcolor{tableShade2}0.07&\cellcolor{tableShade2}0.12\\
\end{longtable}
\end{document}

这将正确对齐小数,并保留单元格颜色。请注意,必须使\cellcolor“引擎强大”才能实现此功能。

答案2

我不认为它们真的是减号,而只是一个占位符?如果我把你的桌子改成使用

&\textemdash&

代替

 &-&

在所有地方,它对我都有效。

相关内容