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.
On the Home tab, click Format > Format Cells. In the Format Cells box, in the Category list, click Custom. In the Type box, enter the following format: 0.00%;[Red]-0.00%. Now, the negative percentage will appear in red highlighting.
Select the group of cells that contain or may contain negative numbers. Go to the Format tab, move down to Number, and pick “Custom Number Format.” In the window that appears, type “red” into the box at the top. You'll then see a few number formats to choose from.
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.)
\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}
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With