How can I align numeric values brought in from the knitr
command \Sexpr{}
by decimal in a table?
1 Answer
Take a look at the dcolumn
package. It allows defining a column that is centered at the decimal point. For example:
\begin{tabular}{D{.}{.}{3}}
\Sexpr{1/4} \\
\Sexpr{1/8} \\
\end{tabular}
Does this work for you? See the package's documentation for further options.
Add the \usepackage{dcolumn}
command so that it appears in the preamble of the knitr-generated LaTeX file.
-
This is much simpler than hacking it up in the code chunk and recalling the individual pieces!– ChernoffOct 18, 2012 at 21:16
-
This work well with numeric values, but not numeric values in math mode.– ChernoffOct 18, 2012 at 21:22
-
@Chernoff: Might be due to "... that dcolumn always uses math mode for the digits as well as the separator." Does it work if you surround the
\Sexpr
s by$
characters, to cancel the math mode that is created by\Sexpr
? (See mail-archive.com/[email protected]/msg27407.html for using a similar trick in a similar context but an entirely different environment.)– krlmlrOct 18, 2012 at 21:27 -
Values from
\Sexpr
line as expected in the table, but fail to do so when bracketed by$
s.– ChernoffOct 18, 2012 at 21:36 -
@Chernoff: Then I don't understand your comment "This work well with numeric values, but not numeric values in math mode." Could you provide an example?– krlmlrOct 18, 2012 at 21:38
xtable
orHmisc
to create the entire table in R?