Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get sanitize text function to work in xtable when not in math mode

Tags:

r

latex

xtable

EDIT I found the answer and posted below. The only reason I had thought it was working in math mode was because I was running an example and never saw the sanitize-text-function argument was being passed to the print method. I'll accept this answer once it becomes available.

I am typesetting a manuscript and doing a data analysis for it. In this analysis, I'm generating a table 1 and looking to indent some row names in the table to give it a cascading feel.

An example of the data I have is:

require(xtable)
data <- data.frame(
  'case'=sample(c('case', 'control'), 100, replace=TRUE),
  'age'=sample(c('40-50,', '50-60', '60-70'), 100, replace=TRUE),
  'sex'=sample(c('male', 'female'), 100, replace=TRUE),
  'income'=sample(c('under 50,000', '50-100,000', 'over 10000'), 100, replace=TRUE)
)

tables <- lapply(data[, -1], table, data[, 1])
tables <- lapply(tables, function(x) {
  rownames(x) <- paste('\\hspace{5mm}', rownames(x))
  x
})
tablenames <- names(tables)
tables <- Reduce(rbind, mapply(rbind, '', tables))
rownames(tables)[rownames(tables) == ''] <- tablenames

xtable(tables)

xtable(tables, type='latex', sanitize.text.function=identity)

I understand the last two xtable commands should return different tables. I'm using the most recent version of R and xtable.

like image 257
AdamO Avatar asked Sep 19 '13 17:09

AdamO


1 Answers

Welp... Apparently, sanitize.text.function is an argument to print.xtable and not to xtable itself. Doing

print(xtable(tables), type='latex', sanitize.text.function=identity)

solves the problem.

like image 96
AdamO Avatar answered Oct 05 '22 22:10

AdamO