2

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:

enter image description here

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:

enter image description here

Thanks for your support.

Magnus

3
  • don't use tabularx for numerical tables. Just use a normal tabular and use dcolumn or siunitx packages which provide decimal alignment column types. Oct 28, 2014 at 19:56
  • @David Without tabularx, how can I solve the issue that I want my tables to have the same width as the text? Oct 28, 2014 at 19:58
  • tabularx is 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 use tabular* 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, use tabular*. Oct 28, 2014 at 20:00

1 Answer 1

3

Using tabular* (or tabular) rather than tabularx for numerical data:

enter image description here

\documentclass{scrartcl}
\usepackage{dcolumn}
\usepackage{booktabs}
\newcommand\hd[1]{\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}#1\end{tabular}}}

% hack as no R on this machine
\def\Sexpr#1{12.123}
\catcode`\%=12

\begin{document}

\begin{table}
\caption{This is a table}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{4}{D{.}{.}{4.3}}@{}}
\toprule
&\multicolumn{2}{c}{First subset} & \multicolumn{2}{c}{Second subset}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& 
\hd{Model1:\\ WTP\_PR\_bin} &
\hd{Model2:\\ WTP\_PR\_log} &
\hd{Model3:\\ WTP\_RE\_bin} &
\hd{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{tabular*}
Note: Displayed are the coefficients; *\(p<0.1\), **\(p<0.05\), ***\(p<0.001\).
\end{table}

\end{document}
2
  • Thanks for this amazing fast answer. Now I have to include that into my thesis. Will see how it works. Just one more issue. In the graphic you posted the asterisks are not superscripted anymore... Why is that? Any fix for that? Oct 28, 2014 at 20:22
  • @MagnusMetz (I ought to be able to use (or not use:-) my own packages reasonably fast:-) * because dcolumn is set in math mode so you get $*$ not * you could use ^{***} or if it's inconvenient to change the markup, could probably insert a local redefinition of * in math mode to subscript itself Oct 28, 2014 at 21:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .