Skip to content

Conversation

@rich-iannone
Copy link
Member

Certain replacements for LaTeX escapes that supply a command will render better (i.e., not include unwanted space characters) if they use {} (e.g., \textasciitilde{} instead of \textasciitilde ). To that end, this PR enhances the escape_latex() function to make the replacements render LaTeX text more accurately.

@jcheng5
Copy link
Member

jcheng5 commented Mar 8, 2022

latex_special_chars <- c(
  "\\" = "\\textbackslash{}",
  "&" = "\\&",
  "~" = "\\textasciitilde{}",
  "^" = "\\textasciicircum{}"
)

escape_latex <- function(text) {
  m <- gregexec("[\\\\&%$#_{}~^]", text)
  special_chars <- regmatches(text, m)
  escaped_chars <- lapply(special_chars, function(x) {
    latex_special_chars[x]
  })
  regmatches(text, m) <- escaped_chars
  text
}

jcheng5
jcheng5 previously approved these changes Mar 9, 2022
@rich-iannone rich-iannone merged commit a6736d3 into master Mar 10, 2022
@rich-iannone rich-iannone deleted the escape-latex-fixes branch March 10, 2022 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants