为 LaTeX 编译清理字符串的功能? [英] Function to sanitize strings for LaTeX compilation?
问题描述
虽然 xtable()
有一个 sanitize.text.function
参数,它允许清理带有特殊字符的字符串以阻止 LaTeX 编译破坏 Sweave/knitr 文档,包不会将功能导出到用户空间.
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.
如何在 xtable
上下文之外对 asdf_text
之类的字符串进行清理,以便将其转换为 asdf\_text
之类的内容?(如果可能的话,我更喜欢小型、独立的解决方案.)
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.)
推荐答案
除非我误解了你的问题,否则我认为你忽略了 latexTranslate
,它也在 Hmisc
包(并记录在与 ?latex
相同的帮助页面上):
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’翻译字符串中的特定项目转换为 LaTeX 格式,例如,使'a^2 = a$^2$' 为上标在变量标签内.希腊字母的 LaTeX 名称(例如,如果‘greek==TRUE’,‘"alpha"’) 将添加反斜杠.数学根据需要插入模式.latexTranslate"假设输入文本总是有匹配的,例如‘[)[](]()’,以及周围的通过‘$$’就可以了.
‘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}$"
这篇关于为 LaTeX 编译清理字符串的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!