R/from_knitr.R

Defines functions escape_html escape_latex2 escape_latex

# 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
}

Try the kableExtra package in your browser

Any scripts or data that you put into this service are public.

kableExtra documentation built on June 22, 2024, 10:59 a.m.