Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

2. siunitx for bolded numbers

3. siunitx manual

share|improve this question
3  
You are not using siunitx here to typeset your numbers, as they are all inside braces. To get things working properly, load the etoolbox package and \robustify\cellcolor in your preamble. Then remove all of the extra braces. You should also set table-format = -1.2, as with the settings you use the signs will not have any space reserved. –  Joseph Wright Mar 9 '12 at 16:56
add comment

2 Answers

up vote 6 down vote accepted

Taking your code, removing the bad braces and adding in braces around 'non-numerical' input for the dashes leads to

\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}

This will correctly align the decimals, and retains the cell colouration. Note that it is necessary to make \cellcolor 'engine robust' for this to work.

share|improve this answer
    
Thanks very much! It looks great now, but the numbers are in a different font. Is that intentional? –  Ariel Mar 9 '12 at 17:14
1  
See the manual on font settings. You probably want to set mode = text, detect-all. –  Joseph Wright Mar 9 '12 at 18:50
add comment

I don't think they are really minus signs, but just a place holder? If I change your table to use

&\textemdash&

instead of

 &-&

in all places, it works for me.

share|improve this answer
    
This works as well! Thanks for the alternative! Any thoughts on how to make the real minus sign behave with the decimal alignment? I thought the + before 1.2 in table-format=+1.2 took care of it... but apparently, that isn't helping. –  Ariel Mar 9 '12 at 16:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.