capture.output()
を使用して、print.xtable()
への(暗黙の)呼び出しによって印刷された行をキャプチャできます。次に、\textcolor{red}{}
で各負の数値を囲むパターンと置換を使用して、gsub()
を出力に適用します。最後にsep="\n"
とcat()
を使用して、変更された行を*.tex
ファイルに書き出します。
\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}
編集
(Iはknitr 'が好む' は、それがより迅速にプロセスれる、results="asis"
でresults=tex
を置き換えることにも注意してください。):得られた画像を追加表。 (SO対応形でそれを取得も以下に含まれるコードにいくつかの調整が必要であった。)
![enter image description here](https://i.stack.imgur.com/y2cad.png)
\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}
ワンダフル。非常に良い答え。あなたの答えは、私がしばらく考えてきた他の多くの問題に対して、私に良いアイデアを与えました。 – Sam
@Sepehr - 'capture.output()'をキービットで使用していましたか? –
でした。私はその機能が存在するのか知らなかった。これでテーブルを簡単にカスタマイズできます。再度、あなたの助けをありがとう。 – Sam