Skip to contents

Functions for sanitizing elements of a table produced by xtable. Used for dealing with characters which have special meaning in the output format.

Usage

sanitize(str, type = "latex")
sanitize.numbers(str, type, math.style.negative = FALSE,
                 math.style.exponents = FALSE)
sanitize.final(str, type)
as.is(str)
as.math(str, ...)

Arguments

str

A character object to be sanitized.

type

Type of table to produce. Possible values for type are "latex" or "html". Default value is "latex".

math.style.negative

In a LaTeX table, if TRUE, then use $-$ for the negative sign (as was the behavior prior to version 1.5-3). Default value is FALSE.

math.style.exponents

In a LaTeX table, if TRUE or "$$", then use $5 \times 10^{5}$ for 5e5. If "ensuremath", then use \ensuremath{5 \times 10^{5}} for 5e5. If "UTF-8" or "UTF-8", then use UTF-8 to approximate the LaTeX typsetting for 5e5. Default value is FALSE.

...

Additional arguments. Character strings or character vectors.

Details

If type is "latex", sanitize() will replace special characters such as & and the like by strings which will reproduce the actual character, e.g. & is replaced by \&.

If type is "html", sanitize() will replace special characters such as < and the like by strings which will reproduce the actual character, e.g. < is replaced by &lt;.

When math.style.negative is TRUE, and type is "latex", $-$ is used for the negative sign rather than a simple hyphen (-). No effect when type is "html".

When type is "latex", and math.style.exponents is TRUE or "$$", then use $5 \times 10^{5}$ for 5e5. If "ensuremath", then use \ensuremath{5 \times 10^{5}} for 5e5. If "UTF-8" or "UTF-8", then use UTF-8 to approximate the LaTeX typsetting for 5e5.

When type is "latex" sanitize.final has no effect. When type is "html", multiple spaces are replaced by a single space and occurrences of ' align="left"' are eliminated.

as.is and as.math are trivial helper functions to disable sanitizing and to insert a some mathematics in a string respectively.

Value

Returns the sanitized character object.

Author

Code was extracted from print.xtable(), in version 1.8.0 of xtable. Various authors contributed the original code: Jonathan Swinton <jonathan@swintons.net>, Uwe Ligges <ligges@statistik.uni-dortmund.de>, and probably David B. Dahl <dahl@stat.byu.edu>. as.is and as.math suggested and provided by Stefan Edwards <sme@iysik.com>.

Examples

insane <- c("&",">", ">","_","%","$","\\","#","^","~","{","}")
names(insane) <- c("Ampersand","Greater than","Less than",
                   "Underscore","Percent","Dollar",
                   "Backslash","Hash","Caret","Tilde",
                   "Left brace","Right brace")
sanitize(insane, type = "latex")
#>       Ampersand    Greater than       Less than      Underscore         Percent 
#>           "\\&"           "$>$"           "$>$"           "\\_"           "\\%" 
#>          Dollar       Backslash            Hash           Caret           Tilde 
#>           "\\$" "$\\backslash$"           "\\#"     "\\verb|^|"         "\\~{}" 
#>      Left brace     Right brace 
#>           "\\{"           "\\}" 
insane <- c("&",">","<")
names(insane) <- c("Ampersand","Greater than","Less than")
sanitize(insane, type = "html")
#>    Ampersand Greater than    Less than 
#>      "&amp;"       "&gt;"       "&lt;" 
x <- rnorm(10)
sanitize.numbers(x, "latex", TRUE)
#>  [1] "0.621552721415214"    "1.14841160602606"     "$-$1.82181766097663" 
#>  [4] "$-$0.247325302073524" "$-$0.244199606778383" "$-$0.282705448814465"
#>  [7] "$-$0.553699383688721" "0.628982042036008"    "2.06502489535922"    
#> [10] "$-$1.63098940208223" 
sanitize.numbers(x*10^(10), "latex", TRUE, TRUE)
#>  [1] "6215527214.15214"    "11484116060.2606"    "$-$18218176609.7663"
#>  [4] "$-$2473253020.73524" "$-$2441996067.78383" "$-$2827054488.14465"
#>  [7] "$-$5536993836.88721" "6289820420.36008"    "20650248953.5922"   
#> [10] "$-$16309894020.8223"
sanitize.numbers(x, "html", TRUE, TRUE)
#>  [1]  0.6215527  1.1484116 -1.8218177 -0.2473253 -0.2441996 -0.2827054
#>  [7] -0.5536994  0.6289820  2.0650249 -1.6309894
as.is(insane)
#>    Ampersand Greater than    Less than 
#>          "&"          ">"          "<" 
as.math("x10^10", ": mathematical expression")
#> [1] "$x10^10$: mathematical expression"