Thanks to you all, I found siunitx
for decimal alignment. However, some of the columns in my correlation matrix don't have any numbers and have "-" instead. siunitx gives me an error about invalid numerical input. How can I get around this?
This is my code:
\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}
As you can see, I am getting an error about the ! ! Invalid numerical input '-'. I looked at the siunitx manual and I am finding it hard to resolve this error. Any help would be most welcome! Thanks for your time.
References:
1. Question about 'dcolumn' and siunitx
on tex.stackexchange
siunitx
here to typeset your numbers, as they are all inside braces. To get things working properly, load theetoolbox
package and\robustify\cellcolor
in your preamble. Then remove all of the extra braces. You should also settable-format = -1.2
, as with the settings you use the signs will not have any space reserved.