虽然 xtable()
有一个 sanitize.text.function
允许使用特殊字符清理字符串以阻止 LaTeX 编译破坏 Sweave/knitr 文档的参数,该包不会将该函数导出到用户空间。
我如何清理像 asdf_text
这样的字符串外xtable
上下文,以便将其转换为类似 asdf\_text
的内容? (如果可能的话,我更喜欢一个小的、独立的解决方案。)
最佳答案
除非我误解了您的问题,否则我认为您忽略了 latexTranslate
,这也在 Hmisc
中包(并记录在与 ?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}$"
关于r - 为 LaTeX 编译清理字符串的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32865384/