问题
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