4

While xtable() has a sanitize.text.function argument which allows to sanitize strings with special charaters to stop LaTeX compilation from breaking in Sweave/knitr documents, the package does not export the function to the userspace.

How can I sanitize strings like asdf_text outside of the xtable context, so as to have it transformed to something like asdf\_text? (If possible I would prefer a small, self-contained solution.)

landroni
  • 2,902
  • 1
  • 32
  • 39
  • 1
    Who is voting to close, and why? – landroni Sep 30 '15 at 12:19
  • 1
    I voted to close (and answered, which is maybe a little schizophrenic). SO has a strong anti-recommendation policy; you could rephrase the question as "how do I solve this problem?", indicating that you'd like a simple/self-contained solution -- not "what package(s) has/have this functionality"?f – Ben Bolker Sep 30 '15 at 12:24
  • 1
    @BenBolker Thank you for the explanation. I've now modified the question to hopefully better fit these expectations. (It's really nicer to get recommendations on how to improve the question before getting downvotes/close votes. In my experience once a question gets a single close vote, no improvement however appropriate will save it from closure...) – landroni Sep 30 '15 at 12:29

2 Answers2

5

Unless I misunderstand your question, I think you've overlooked latexTranslate, which is also in the Hmisc package (and documented on the same help page as ?latex):

‘latexTranslate’ translates particular items in character strings to LaTeX format, e.g., makes ‘a^2 = a\$^2\$’ for superscript within variable labels. LaTeX names of greek letters (e.g., ‘"alpha"’) will have backslashes added if ‘greek==TRUE’. Math mode is inserted as needed. ‘latexTranslate’ assumes that input text always has matches, e.g. ‘[) [] (] ()’, and that surrounding by ‘\$\$’ is OK.

library("Hmisc")
latexTranslate("asdf_text")
## [1] "asdf\\_text"
latexTranslate("a^2")
## [1] "a$^{2}$"
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I definitely overlooked `latexTranslate()`! And it didn't even appear in my `sos` searches, as I was searching for the `sanitize` keyword... – landroni Sep 30 '15 at 12:18
  • An advantage of latexTranslate for my task is that it doesn't mess with existing latex code. `xtable` and `sanitizestr` will sanitize out the `\\textbf{}` such that in a string like "\\textbf{abc_def}" the textbf command no longer works. latexTranslate leaves the latex code alone but does sanitize the underscore. – Omar Wasow May 23 '18 at 02:26
2

Thus far I found package reportRx which provides sanitizestr():

Sanitizes strings to not break LaTeX

Strings with special charaters will break LaTeX if returned 'asis' by knitr. This happens every time we use one of the main reportRx functions. We first sanitize our strings with this function to stop LaTeX from breaking.

require(reportRx)
sanitizestr("asdf_text")
## [1] "asdf\\_text"

My gripe however is that it comes with quite a number of dependencies...


Another solution is tikzDevice which provides sanitizeTexString(), and has many fewer mandatory dependencies:

Replace LaTeX Special Characters in a String

This function is used by tikzDevice when sanitize=TRUE to replace special LaTeX characters [..]

require(tikzDevice)
sanitizeTexString("asdf_text")
## [1] "asdf{\\_{}}text"
Community
  • 1
  • 1
landroni
  • 2,902
  • 1
  • 32
  • 39