Stat-R
Stat-R

Reputation: 5270

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

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...

enter image description here

But I want the following

enter image description here

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...

Upvotes: 3

Views: 2080

Answers (1)

Sacha Epskamp
Sacha Epskamp

Reputation: 47541

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

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

Upvotes: 7

Related Questions