R/remove_escape_latex.R

Defines functions remove_escape_latex

Documented in remove_escape_latex

#' Remove Escaped LaTeX Formulas from Text
#'
#' This function removes escaped LaTeX formulas and formatting commands from LaTeX-formatted text.
#'
#' @param x A character vector or string containing LaTeX-formatted text.
#'
#' @return A character vector with escaped LaTeX formulas and formatting commands removed.
#'
#' @examples
#' # Remove escaped LaTeX formulas from a string
#' cleaned_text <- remove_escape_latex("\\\\textbf\\{Hello\\}, $\\\\alpha$ formula")
#' print(cleaned_text)
#'
#' @importFrom magrittr %>%
#'
#' @export
remove_escape_latex <- function(x) {
  rm_esc <- gsub(
    "\\\\textbackslash([[:space:]])?",
    "\\\\",
    x,
    fixed = FALSE,
    ignore.case = TRUE
  )
  rm_esc <- gsub("\\\\([&%$#_{}])",
         "\\1",
         rm_esc,
         fixed = FALSE,
         ignore.case = TRUE
    )
    gsub("\\{\\}", "\\\\", rm_esc)
}

Try the RTLknitr package in your browser

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

RTLknitr documentation built on Aug. 25, 2023, 9:06 a.m.