0

My question here is very similar to this one

I am trying to export a table with negative number to latex. I want to do it with print(xtable()) to access all the print option.

for the moment, the only solution I found is: (taking the previous example)

library(xtable)
testMatrix <- matrix(c(sample(-10:10,10)), ncol = 2)
xt <- capture.output(xtable(testMatrix))
sink(paste(file.name, ".tex", sep = "", collapse = NULL))
cat(gsub("(\\s|^)(-\\d\\.\\d*)", "\\1\\\\textcolor{red}{\\2}", xt), sep="\n")
sink()

It works, but I need to modify some info that are not accessible directly in xtable such as: include.rownames , math.style.exponents or caption.placement. The best would be a simple way to export negative decimal numbers directly with print and xtable. Any idea?

duckmayr
  • 16,303
  • 3
  • 35
  • 53
TeYaP
  • 303
  • 6
  • 21
  • 2
    The [kableExtra](http://haozhu233.github.io/kableExtra/) package allows to alter to color of table cells conditionally on their value (e.g., being negative). The package is great, but it's obviously not on option if you're married to xtable. – hplieninger May 15 '19 at 12:48
  • Yes, `huxtable` is another option (my package...). In general, modern table packages should allow a lot of control over output. – dash2 May 22 '19 at 12:57

1 Answers1

4

You need to do two things:

  1. 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.
  2. 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}
duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • ok but, how do you export it in `file.name.tex` ? What I would like is to export the result with `print()` – TeYaP May 13 '19 at 13:48