Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 783
Stat-R Avatar asked Dec 14 '25 15:12

Stat-R


1 Answers

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

\verb|\Sexpr{names(df1)}|
like image 199
Sacha Epskamp Avatar answered Dec 16 '25 10:12

Sacha Epskamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!