]> git.donarmstrong.com Git - xtable.git/blob - pkg/R/sanitize.R
Fixed default of sanitize type argument to be 'latex' as specified in the docs
[xtable.git] / pkg / R / sanitize.R
1 sanitize <- function(str, type = "latex") {
2   if(type == "latex"){
3     result <- str
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)
18     return(result)
19   } else {
20     result <- str
21     result <- gsub("&", "&amp;", result, fixed = TRUE)
22     result <- gsub(">", "&gt;", result, fixed = TRUE)
23     result <- gsub("<", "&lt;", result, fixed = TRUE)
24     return(result)
25   }
26 }
27
28
29 sanitize.numbers <- function(str, type,
30                              math.style.negative = FALSE,
31                              math.style.exponents = FALSE){
32   if (type == "latex"){
33     result <- str
34     if ( math.style.negative ) {
35       for(i in 1:length(str)) {
36         result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)
37       }
38     }
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 == "$$"
43                  ) {
44         for(i in 1:length(str)) {
45           result[i] <-
46             gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
47                  "$\\1\\2 \\\\times 10^{\\3\\4}$", result[i])
48         }
49       } else if (math.style.exponents == "ensuremath") {
50         for(i in 1:length(str)) {
51           result[i] <-
52             gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
53                  "\\\\ensuremath{\\1\\2 \\\\times 10^{\\3\\4}}",
54                  result[i])
55         }
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+)$",
61                         result[i]))) {
62             temp <- strsplit(result[i],"eE",result[i])
63             result[i] <-
64               paste0(temp[1],
65                      "\u00d710",
66                      chartr("-1234567890",
67                             "\u207b\u00b9\u00b2\u00b3\u2074\u2075\u20746\u20747\u20748\u20749\u2070",
68                             temp[2]))
69           }
70         }
71       }
72     }
73     return(result)
74   } else {
75     return(str)
76   }
77 }
78
79
80 sanitize.final <- function(str, type){
81   if (type == "latex"){
82     return(str)
83   } else {
84     str$text <- gsub("  *", " ",  str$text, fixed = TRUE)
85     str$text <- gsub(' align="left"',  "", str$text,
86                      fixed = TRUE)
87     return(str)
88   }
89 }
90
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}
95
96 ### Helper function for embedding names in a math environment
97 as.math <- function(str, ...) { paste0('$',str,'$', ...) }