1

in my masterthesis I want to create regression tables the numbers in the table are aligned by the decimal point.

Here is a MWE to get an impression what I'm talking about:

\documentclass[10pt,a4paper]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{siunitx}
\begin{document}
\begin{table}
\caption{MWE without multicolumn elements.}
\begin{tabularx}{\textwidth}{XSSSS} % S: align by decimal point (part of the siunitx package)  
\toprule
1&2&3&4&5\\
\midrule  
1 & 94.90 & 5.10 & 0.00 & 0.00 \\  
2 & 4.08 & 88.78 & 6.80 & 0.34 \\  
3 & 0.00 & 4.08 & 91.84 & 4.08 \\  
4 & 1.02 & 0.51 & 5.61 & 92.86 \\  
\bottomrule
\end{tabularx}
Note: bla bla bla bla.
\end{table}
\end{document}

The code above leads to following results: enter image description here

I am quite satisfied with this result, but if I add \mulitcolumn areas like in the following MWE the compilation process doesn't work:

\documentclass[10pt,a4paper]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{siunitx}
\author{Name of the author}
\begin{document}
\begin{table}
\caption{MWE without multicolumn elements.}
\begin{tabularx}{\textwidth}{XSSSS} % S: align by decimal point (part of the siunitx package)  
\toprule
&\multicolumn{2}{c}{Subset 1}& \multicolumn{2}{c}{Subset 2}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& 1\_PR\_bin & 2\_PR\_log & 3\_RE\_bin & 4\_RE\_log \\ 
\midrule  
1 & 94.90 & 5.10 & 0.00 & 0.00 \\  
2 & 4.08 & 88.78 & 6.80 & 0.34 \\  
3 & 0.00 & 4.08 & 91.84 & 4.08 \\  
4 & 1.02 & 0.51 & 5.61 & 92.86 \\  
\bottomrule
\end{tabularx}
Note: bla bla bla bla.
\end{table}
\end{document}

Who can help me to fix this issue? Thank you very much in advance for your help!!

  • 1
    why would you want tabularx and X in the first column. I wrote tabularx and I'd never use it in such a way, it just looks horrible with the first column disconnected from the table. Why not just use tabular and an l column for the first column? – David Carlisle Oct 28 '14 at 15:16
  • @DavidCarlisle Thank you for your hint, but the X in the first column was just in the MWE. I never intended to use it in that way. – Magnus Metz Oct 28 '14 at 15:32
3

You have table head values starting with numbers but which presumably should not be part of the general column alignment. You'll see odd results even without the \multicolumn if you don't escape these:

& {1\_PR\_bin} & {2\_PR\_log} & {3\_RE\_bin} & {4\_RE\_log} \\ 

I'd also recommend telling siunitx how many digits are in each column using the optional argument to S

\begin{tabularx}{\textwidth}{X*4{S[table-format = 2.2]}} % S: align by decimal point (part of the siunitx package)  
\toprule
&\multicolumn{2}{c}{Subset 1}& \multicolumn{2}{c}{Subset 2}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {1\_PR\_bin} & {2\_PR\_log} & {3\_RE\_bin} & {4\_RE\_log} \\ 
\midrule  
1 & 94.90 & 5.10 & 0.00 & 0.00 \\  
2 & 4.08 & 88.78 & 6.80 & 0.34 \\  
3 & 0.00 & 4.08 & 91.84 & 4.08 \\  
4 & 1.02 & 0.51 & 5.61 & 92.86 \\  
\bottomrule
\end{tabularx}
  • Although your answer is correct and I also accepted it as right, I am still not satisfied with the results, as another problem occurred. Instead of numerics, like in the examples above, I use values computed with \Sexpr{} from an external R-Script. Unfortunately with your solution the document compiles, but I have a strange alignment of the numbers. The positive numbers are aligned to the right, whereas the negative numbers are aligned to the left. At least one thing is right. Both, negative and positive numbers are aligned by the deciamal point :-/... Any further hints? – Magnus Metz Oct 28 '14 at 16:07
  • @MagnusMetz Please either update this question or post a new one showing your problem. The whole point of the S column is to align on the decimal, so that should work. Calculated values will fail if they result in something non-expandable. – Joseph Wright Oct 28 '14 at 16:24
  • Here is the new question: tex.stackexchange.com/questions/209536/… Thanks for further help!! – Magnus Metz Oct 28 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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