blob: 205148d8e57e32deb2a387a85bfe55163ec1c2a2 [file] [log] [blame]
Hao Zhuc5415632017-02-21 17:16:13 -05001# These functions are imported from knitr/highr as `:::` is not recommended by
2# CRAN
3
Hao Zhuf7994dd2017-02-27 16:58:42 -05004# escape special LaTeX characters
5# @author Yihui Xie
Hao Zhuc5415632017-02-21 17:16:13 -05006escape_latex <- function(x, newlines = FALSE, spaces = FALSE) {
7 x = gsub('\\\\', '\\\\textbackslash', x)
8 x = gsub('([#$%&_{}])', '\\\\\\1', x)
9 x = gsub('\\\\textbackslash', '\\\\textbackslash{}', x)
10 x = gsub('~', '\\\\textasciitilde{}', x)
11 x = gsub('\\^', '\\\\textasciicircum{}', x)
12 if (newlines) x = gsub('(?<!\n)\n(?!\n)', '\\\\\\\\', x, perl = TRUE)
13 if (spaces) x = gsub(' ', '\\\\ \\\\ ', x)
14 x
15}
16
Hao Zhud4630872018-03-26 11:26:36 -040017escape_latex2 <- function(x) {
18 x = gsub('\\\\', '\\\\\\\\textbackslash', x)
19 x = gsub('([#$%&_{}])', '\\\\\\\\\\1', x)
20 x = gsub('\\\\textbackslash', '\\\\\\\\textbackslash{}', x)
21 x = gsub('~', '\\\\\\\\textasciitilde{}', x)
22 x = gsub('\\^', '\\\\\\\\textasciicircum{}', x)
23 x
24}
25
Hao Zhuf7994dd2017-02-27 16:58:42 -050026# escape special HTML characters
27# @author Yihui Xie
Hao Zhuc5415632017-02-21 17:16:13 -050028escape_html <- function(x) {
29 x = gsub('&', '&amp;', x)
30 x = gsub('<', '&lt;', x)
31 x = gsub('>', '&gt;', x)
32 x = gsub('"', '&quot;', x)
33 x
34}