1 sanitize <- function(str, type = "latex") {
4 result <- gsub("\\\\", "SANITIZE.BACKSLASH", result)
5 result <- gsub("$", "\\$", result, fixed = TRUE)
6 result <- gsub(">", "$>$", result, fixed = TRUE)
7 result <- gsub("<", "$<$", result, fixed = TRUE)
8 result <- gsub("|", "$|$", result, fixed = TRUE)
9 result <- gsub("{", "\\{", result, fixed = TRUE)
10 result <- gsub("}", "\\}", result, fixed = TRUE)
11 result <- gsub("%", "\\%", result, fixed = TRUE)
12 result <- gsub("&", "\\&", result, fixed = TRUE)
13 result <- gsub("_", "\\_", result, fixed = TRUE)
14 result <- gsub("#", "\\#", result, fixed = TRUE)
15 result <- gsub("^", "\\verb|^|", result, fixed = TRUE)
16 result <- gsub("~", "\\~{}", result, fixed = TRUE)
17 result <- gsub("SANITIZE.BACKSLASH", "$\\backslash$", result, fixed = TRUE)
21 result <- gsub("&", "&", result, fixed = TRUE)
22 result <- gsub(">", ">", result, fixed = TRUE)
23 result <- gsub("<", "<", result, fixed = TRUE)
29 sanitize.numbers <- function(str, type,
30 math.style.negative = FALSE,
31 math.style.exponents = FALSE){
34 if ( math.style.negative ) {
35 for(i in 1:length(str)) {
36 result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)
39 if ( math.style.exponents ) {
40 if (is.logical(math.style.exponents) && ! math.style.exponents ) {
41 } else if (is.logical(math.style.exponents) && math.style.exponents ||
42 math.style.exponents == "$$"
44 for(i in 1:length(str)) {
46 gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
47 "$\\1\\2 \\\\times 10^{\\3\\4}$", result[i])
49 } else if (math.style.exponents == "ensuremath") {
50 for(i in 1:length(str)) {
52 gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
53 "\\\\ensuremath{\\1\\2 \\\\times 10^{\\3\\4}}",
56 } else if (math.style.exponents == "UTF8" ||
57 math.style.exponents == "UTF-8") {
58 for(i in 1:length(str)) {
59 ## this code turns 1e5 into a UTF-8 representation of 1\times10^5
60 if (all(grepl("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
62 temp <- strsplit(result[i],"eE",result[i])
67 "\u207b\u00b9\u00b2\u00b3\u2074\u2075\u20746\u20747\u20748\u20749\u2070",
80 sanitize.final <- function(str, type){
84 str$text <- gsub(" *", " ", str$text, fixed = TRUE)
85 str$text <- gsub(' align="left"', "", str$text,
91 ### Some trivial helper functions
92 ### Suggested by Stefan Edwards, sme@iysik.com
93 ### Helper function for disabling sanitizing
94 as.is <- function(str) {str}
96 ### Helper function for embedding names in a math environment
97 as.math <- function(str, ...) { paste0('$',str,'$', ...) }