17

I'm writing a report in R with knitr. I'm using xtable() to generate tables in the report. One of my tables includes both negative and positive numbers. I'd like to change the color of negative numbers into red. How can I do that? Obviously, one easy solution is to change the latex code that xtable generates BUT note that I have an auto-report that numbers can change with new datasets and I don't want to manually set the colors.

Here is a simple code:

\documentclass{article}
\begin{document}
<<simpleExamp, results=tex, echo=FALSE>>=
library(knitr)
library(xtable)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
xtable(testMatrix)
@
\end{document} 

How can I make the negative numbers Red? Thank you for your help.

Sam
  • 4,357
  • 6
  • 36
  • 60

2 Answers2

25

You can use capture.output() to capture the lines printed by the (implicit) call to print.xtable(). Then apply gsub() to the output, using a pattern and replacement that surround each negative number with \textcolor{red}{}. Finally, use cat() with sep="\n" to write the modified lines out to the *.tex file.

\documentclass{article}
\begin{document}
<<simpleExamp, results="asis", echo=FALSE>>=
library(knitr)
library(xtable)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
## I added the following three lines
xt <- capture.output(xtable(testMatrix))
xt_mod <- gsub("(\\s|^)(-\\d*)", "\\1\\\\textcolor{red}{\\2}", xt)
cat(xt_mod, sep="\n")
@
\end{document}

(Note also that I replaced results=tex with results="asis", which knitr 'prefers' and which it will more quickly process.)


Edit: Adding an image of the resulting table. (Getting it in an SO-ready form required a few tweaks to the code, which is also included below.)

enter image description here

\documentclass{standalone}
\renewenvironment{table}{}{}% Ignore `table` environment in standalone mode.
\begin{document}
<<simpleExamp, results="asis", echo=FALSE>>=
library(knitr)
library(xtable)
cat("\\Huge\n\n")
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
## I added the following three lines
xt <- capture.output(print.xtable(xtable(testMatrix), table.placement=NULL))
xt_mod <- gsub("(\\s|^)(-\\d*)", "\\1\\\\textcolor{red}{\\2}", xt)
cat(xt_mod, sep="\n")
@
\end{document}
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 3
    Wonderful. Very nice answer. Your answer gave me a good idea for many other problems I've been thinking about for a while. – Sam Aug 27 '12 at 20:22
  • @Sepehr -- Was using `capture.output()` the key bit? – Josh O'Brien Aug 27 '12 at 20:23
  • It was. I didn't know the function existed. Now I can customize my tables easily. Again, thank you very much for your help. – Sam Aug 27 '12 at 20:25
  • Is this supposed to work when 'knitr' is loaded in a console session and the above code pasted with enclosing quotes inside `knit()`? – IRTFM Aug 29 '12 at 23:32
  • @DWin -- Nope. The workflow is similar to `Sweave`'s, in that you: (1) Put the code in a file (calling it `"eg.Rnw"`, for example); (2) do `knit("eg.Rnw")` (perhaps from a console session); then (3) run LaTeX on `"eg.tex"`, the resulting `*.tex` file. – Josh O'Brien Aug 29 '12 at 23:42
  • Should `Hmisc::latex(knit(readLines("eg.Rnw"))` work? Assuming, of course, that "eg.Rnw" is in the working directory. – IRTFM Aug 30 '12 at 00:17
  • @Dwin -- According to `?knit`, that `input` argument has to be the "path of the input file", so `knit(readLines("eg.Rnw"))` won't work, but `knit("eg.Rnw")` will. Not sure what you're trying to do, but perhaps [this interesting script](https://github.com/yihui/knitr-examples/blob/master/021-ggplot2-geoms.Rnw) (linked to by Yihui in his answer to [this SO question](http://stackoverflow.com/questions/11653573/automated-ggplot2-example-gallery-in-knitr)) will give you some ideas. – Josh O'Brien Aug 30 '12 at 00:32
  • Thanks, I'll look at it but readers of this dialog should NOT expect Hmisc::latex(knit("eg.Rnw")) to "work", unless that is they are expecting a nice latex table that has a single entry "eg.Rnw". I'm guessing that `latex()` is given the file name as the value of the `knit()` call. – IRTFM Aug 30 '12 at 00:40
4

Copied Josh O'Brien's answer with a little tweak to color a table with decimals:

\documentclass{article}
\begin{document}
<<simpleExamp, results="asis", echo=FALSE>>=
library(knitr)
library(xtable)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)*1.1
## I added the following three lines
xt <- capture.output(xtable(testMatrix))
xt_mod <- gsub("(\\s|^)(-\\d\\.\\d*)", "\\1\\\\textcolor{red}{\\2}", xt)
cat(xt_mod, sep="\n")
@
\end{document}
jfreels
  • 123
  • 4