File: from_knitr.R

package info (click to toggle)
r-cran-kableextra 1.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,400 kB
  • sloc: javascript: 579; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,096 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# These functions are imported from knitr/highr as `:::` is not recommended by
# CRAN

# escape special LaTeX characters
# @author Yihui Xie
escape_latex <- function(x, newlines = FALSE, spaces = FALSE) {
  x = gsub('\\\\', '\\\\textbackslash', x)
  x = gsub('([#$%&_{}])', '\\\\\\1', x)
  x = gsub('\\\\textbackslash', '\\\\textbackslash{}', x)
  x = gsub('~', '\\\\textasciitilde{}', x)
  x = gsub('\\^', '\\\\textasciicircum{}', x)
  if (newlines) x = gsub('(?<!\n)\n(?!\n)', '\\\\\\\\', x, perl = TRUE)
  if (spaces) x = gsub('  ', '\\\\ \\\\ ', x)
  x
}

escape_latex2 <- function(x) {
  x = gsub('\\\\', '\\\\\\\\textbackslash', x)
  x = gsub('([#$%&_{}])', '\\\\\\\\\\1', x)
  x = gsub('\\\\textbackslash', '\\\\\\\\textbackslash{}', x)
  x = gsub('~', '\\\\\\\\textasciitilde{}', x)
  x = gsub('\\^', '\\\\\\\\textasciicircum{}', x)
  x
}

# escape special HTML characters
# @author Yihui Xie
# Added conversion
escape_html <- function(x) {
  x = gsub('&', '&amp;', x)
  x = gsub('<', '&lt;', x)
  x = gsub('>', '&gt;', x)
  x = gsub('"', '&quot;', x)
  x = gsub('\n', '<br />', x)
  x
}