You need to do two things:
- Alter your regex pattern for finding negative numbers. The pattern from that other question only finds negative numbers if they are greater than -10, and only if they have a decimal point. We will make it more general.
- Use
capture.output(print(xtable(testMatrix)))
instead of capture.output(xtable(testMatrix))
. Then you can use the functionality of print.xtable()
.
Observe:
library(xtable)
set.seed(123)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
xt <- capture.output(print(xtable(testMatrix))) # Notice use of print()
negative_number_pattern <- "(\\s|^)(-\\d+\\.*\\d*)" # Notice changed pattern
cat(gsub(negative_number_pattern, "\\1\\\\textcolor{red}{\\2}", xt), sep="\n")
% latex table generated in R 3.6.0 by xtable 1.8-4 package
% Mon May 13 08:02:53 2019
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
& 1 & 2 \\
\hline
1 & 4 & \textcolor{red}{-9} \\
2 & 8 & \textcolor{red}{-5} \\
3 & 3 & 0 \\
4 & \textcolor{red}{-8} & \textcolor{red}{-6} \\
5 & \textcolor{red}{-1} & \textcolor{red}{-7} \\
\hline
\end{tabular}
\end{table}
xt <- capture.output(print(xtable(testMatrix), include.rownames = FALSE))
cat(gsub(negative_number_pattern, "\\1\\\\textcolor{red}{\\2}", xt), sep="\n")
% latex table generated in R 3.6.0 by xtable 1.8-4 package
% Mon May 13 08:02:53 2019
\begin{table}[ht]
\centering
\begin{tabular}{rr}
\hline
1 & 2 \\
\hline
4 & \textcolor{red}{-9} \\
8 & \textcolor{red}{-5} \\
3 & 0 \\
\textcolor{red}{-8} & \textcolor{red}{-6} \\
\textcolor{red}{-1} & \textcolor{red}{-7} \\
\hline
\end{tabular}
\end{table}
As you can see, by using print()
, we're able to use the options you're looking for (here I demonstrate with changing include.rownames
to FALSE
).
Update: "Exporting"
If you want to print the table to filename.tex
directly from the R command, you can use cat()
's file
argument. From help("cat")
:
Usage
cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
Arguments
... R objects (see ‘Details’ for the types of objects allowed).
file A connection, or a character string naming the file to print to. If "" (the default), cat prints to the standard output connection, the console unless redirected by sink. If it is "|cmd", the output is piped to the command given by ‘cmd’, by opening a pipe connection.
So, this:
library(xtable)
set.seed(123)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
xt <- capture.output(print(xtable(testMatrix))) # Notice use of print()
negative_number_pattern <- "(\\s|^)(-\\d+\\.*\\d*)" # Notice changed pattern
cat(gsub(negative_number_pattern, "\\1\\\\textcolor{red}{\\2}", xt), sep="\n",
file = "filename.tex")
results in the following content in filename.tex
:
% latex table generated in R 3.6.0 by xtable 1.8-4 package
% Mon May 13 08:53:44 2019
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
& 1 & 2 \\
\hline
1 & 4 & \textcolor{red}{-9} \\
2 & 8 & \textcolor{red}{-5} \\
3 & 3 & 0 \\
4 & \textcolor{red}{-8} & \textcolor{red}{-6} \\
5 & \textcolor{red}{-1} & \textcolor{red}{-7} \\
\hline
\end{tabular}
\end{table}