用于清理LaTeX编译字符串的函数吗? [英] Function to sanitize strings for LaTeX compilation?

查看:87
本文介绍了用于清理LaTeX编译字符串的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然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",则将添加反斜杠.数学 模式根据需要插入. "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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆