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.

How can I align numeric values brought in from the knitr command \Sexpr{} by decimal in a table?

share|improve this question
    
Just out of curiosity: Why wouldn't you use xtable or Hmisc to create the entire table in R? –  krlmlr Oct 18 '12 at 21:32
    
Probably because of other bad habits. I need to include graphics and text as well as the R calculations in a table. I'm using knitr since my GRA position would otherwise have me copy and paste lots of output into a working document, piece-by-piece. –  Chernoff Oct 18 '12 at 21:51

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
This is much simpler than hacking it up in the code chunk and recalling the individual pieces! –  Chernoff Oct 18 '12 at 21:16
    
This work well with numeric values, but not numeric values in math mode. –  Chernoff Oct 18 '12 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 \Sexprs by $ characters, to cancel the math mode that is created by \Sexpr? (See mail-archive.com/lyx-users@lists.lyx.org/msg27407.html for using a similar trick in a similar context but an entirely different environment.) –  krlmlr Oct 18 '12 at 21:27
    
Values from \Sexpr line as expected in the table, but fail to do so when bracketed by $s. –  Chernoff Oct 18 '12 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? –  krlmlr Oct 18 '12 at 21:38

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.