[R] Sweave: \Sexpr and variables with special chars

Sven Garbade sfgarbade at googlemail.com
Wed Nov 17 10:00:41 CET 2010


try to quote the special LaTeX symbols, eg

## add more here if needed
sani <- function(x) gsub("_", "\\\\_", x)

so your example:

\documentclass[a4paper]{article}
<<echo=false, results=hide>>=
#mytitlevar <- "Stuff" # case 1, everything is find
mytitlevar <- "Stuff_first" # case 2, f is turned into sub-text
sani <- function(x) gsub("_", "\\\\_", x)
@
\title{MyTitle: \\ \Sexpr{sani(mytitlevar)} }
\begin{document}
\maketitle
\end{document}

Regards, Sven

2010/11/16 Ralf B <ralf.bierig at gmail.com>:
> I am using \Sexpr to include a variable in a title of a Sweave document:
>
> \documentclass[a4paper]{article}
> <<echo=false, results=hide>>=
> #mytitlevar <- "Stuff" # case 1, everything is find
> mytitlevar <- "Stuff_first" # case 2, f is turned into sub-text
> @
> \title{MyTitle: \\ \Sexpr{mytitlevar} }
> \begin{document}
> \maketitle
> \end{document}
>
> When doing this, the variable seems to be subject to interpretation by
> LaTeX. The variable in case #2 causes the 'f' of 'Stuff_first' to be
> printed as sub-text because of the leading underscore. Is there a way
> to turn the variable value (the text) into a form so that it is not
> interpreted and/or Sweave? I understand that this perhaps more of a
> LaTeX question than an R question...
>
> Thanks,
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list