将下划线作为内联代码传递给knitR [英] Pass underscore as inline code to knitR
本文介绍了将下划线作为内联代码传递给knitR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用knitR生成文档.有些变量名带有下划线,我不能更改.无论如何仍要以普通文本的形式插入这些内容?
I'm using knitR to generate a document. Some of the variable names have underscores that I can't change. Anyway to still insert these as normal text?
基本示例:
\documentclass{article}
\begin{document}
<<>>=
data.df = data.frame(var_name_1=c(0,1),var_name_2=c(1,2))
@
I want to be able to print \Sexpr{(names(data.df)[1])} and \Sexpr{(names(data.df)[2])} normally.
\end{document}
提前感谢您的帮助.
推荐答案
一种合适的解决方法是在代码块中定义函数rep_
. rep_
只是寻找下划线,并在其前放置一个"\".
A suitable work around is to define the function rep_
in the code block. rep_
just looks for the underscore and places a "\" before it.
\documentclass{article}
\begin{document}
<<>>=
data.df = data.frame(var_name_1=c(0,1),var_name_2=c(1,2))
rep_ = function(test){gsub("([_])", '\\\\\\_', "test_var")}
@
I want to be able to print \Sexpr{rep_((names(data.df)[1]))} and \Sexpr{rep_((names(data.df)[2]))} normally.
\end{document}
产生我们想要的东西!
这篇关于将下划线作为内联代码传递给knitR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文