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

  • Just out of curiosity: Why wouldn't you use xtable or Hmisc to create the entire table in R?
    – krlmlr
    Oct 18, 2012 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, 2012 at 21:51

1 Answer 1


Take a look at the dcolumn package. It allows defining a column that is centered at the decimal point. For example:

    \Sexpr{1/4} \\
    \Sexpr{1/8} \\

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!
    – Chernoff
    Oct 18, 2012 at 21:16
  • This work well with numeric values, but not numeric values in math mode.
    – Chernoff
    Oct 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 \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, 2012 at 21:27
  • Values from \Sexpr line as expected in the table, but fail to do so when bracketed by $s.
    – Chernoff
    Oct 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?
    – krlmlr
    Oct 18, 2012 at 21:38

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.