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.bie...@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@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. > ______________________________________________ R-help@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.