Way to automatically escape characters ('_','\' etc) using knitr

ぃ、小莉子 提交于 2019-11-29 15:35:20

问题


I m using Rstudio under Win7. Is there a way that knitr automatically escape a character? Please see below for a simpler example:

\documentclass[a4]{article}
\title{Example}
\author{Stat-R}

\begin{document}

\maketitle

<<nothing,echo=FALSE>>=
my_name <- 'hari'
my_number <- 100

df1 <- data.frame(my_name,my_number)
df1
# names(df1)
@

\section{Testing only}

Now I will print the columnnames of my data frame df1
$\Sexpr{names(df1)}$

\end{document}

I get the following output...

But I want the following

I will have to change all '_' into '_' in my tex file. Is there a way to directly do it in sweave file. I will appreciate any suggestion on this regard...


回答1:


I think you want to put the name in an verbatim environment rather than a math environment:

\verb|\Sexpr{names(df1)}|


来源:https://stackoverflow.com/questions/9896173/way-to-automatically-escape-characters-etc-using-knitr

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