I am writing my thesis as dynamic document using .Rnw, knitr and R. I ran my statistical analyses in an external R-script, which I sourced into my main .Rnw-file.
I also want to create some tables showing summary statistics and the output from my regression analyses. For some cells I type in the value by hand and for others I call the values with the knitr command \Sexpr{} from the sourced R files.
The following MWE provides an insight into what I am talking about:
\documentclass{scrartcl}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
\begin{table}
\caption{This is a table}
\begin{tabularx}{\textwidth}{XXXXX}
\toprule
&\multicolumn{2}{c}{First subset} & \multicolumn{2}{c}{Second subset}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Model1: WTP\_PR\_bin} & {Model2: WTP\_PR\_log} & {Model3: WTP\_RE\_bin} & {Model4: WTP\_RE\_log} \\
\midrule
Const & \Sexpr{sprintf("%.3f", round(244/33, digits=3))}*** & 22.300 & \Sexpr{round(exp(0.987), digits=3)} & 65.876\\
Variable 1 & \Sexpr{round(exp(0.873), digits=3)} &-34.445* & \Sexpr{round(sqrt(75688), digits=3)} & 0.348\\
Variable 2 & \Sexpr{78*87.343} & 222.873 & -0.874 & -1.234**\\
\bottomrule
\end{tabularx}
Note: Displayed are the coefficients; *\(p<0.1\), **\(p<0.05\), ***\(p<0.001\).
\end{table}
\end{document}
The result of the code above looks like this:

What I am aiming for right now is that the values in every column are aligned by decimal point. See the alignment of values in the table above. The values are left aligned, but not aligned to the decimal point.
Anybody any clue, how to solve that issue? I expect something like that:

Thanks for your support.
Magnus

tabularxfor numerical tables. Just use a normaltabularand usedcolumnorsiunitxpackages which provide decimal alignment column types. – David Carlisle Oct 28 '14 at 19:56tabularx, how can I solve the issue that I want my tables to have the same width as the text? – Magnus Metz Oct 28 '14 at 19:58tabularxis absolutely rubbish at doing that for numerical tables, it varies column widths by adjusting the length used for line breaking within columns but there is no line breaking of these numbers. You could usetabular*which is designed for that but really stretching the table full width should be a non-aim, it just pulls the data apart and makes it harder to read, better to let it be set to its natural width centred. But if you must, usetabular*. – David Carlisle Oct 28 '14 at 20:00