Pass underscore as inline code to knitR

别等时光非礼了梦想. 提交于 2020-01-03 03:13:09

问题


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?

Basic example:

\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}

Tkanks in advance for your assistance.


回答1:


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}

Resulting in what we want!



来源:https://stackoverflow.com/questions/33550225/pass-underscore-as-inline-code-to-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!