R, Sweave, Latex - Escape Variables to Be Printed in Latex

R, Sweave, LaTeX - escape variables to be printed in LaTeX?

You can either use \verb or you can insert a quadruple backslash, which is needed to get one in the final tex file because it goes through two printing operations which convert a double "\" to a single "\".

\documentclass{article}
\begin{document}
<<echo=FALSE, results=hide>>=
sanitize <- function(str) {
result <- str
result <- gsub("&", "\\\\&", result, fixed = TRUE)
result <- gsub("_", "\\\\_", result, fixed = TRUE)
result
}
@

<<>>=
(foo <- "test & _")
sanitize(foo)
@

My string is ``\verb+\Sexpr{foo}+''. When sanitized, it's ``\Sexpr{sanitize(foo)}''.

\end{document}

Print long strings of text LaTeX / Sweave

You could take a look at the package xtable for creating latex tables, but that doesn't work very well with longtable I guess. Alternatively, look at the function latex in the package Hmisc, that has an option "longtable" and allows more control over the output.

To add a slash for special characters as used in Latex, you could do something like this :

add.slash <- function(x){
where <- embed(c(1,gregexpr("[&#$%]",x)[[1]],nchar(x)+1),dim=2)
out <- paste(apply(where,1,function(y){substr(x,y[2],y[1]-1)}),collapse="\\")
return(out)
}

> x <- "I print $ and % and & and # and . and ! and ,"

> cat(add.slash(x),"\n")
I print \$ and \% and \& and \# and . and ! and ,

EDIT :
the use of [[:punct:]] is wrong, that also changes punctuations and so on. Code is corrected. Backslashes are really problematic.

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

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

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

First line at PDF doc is tabbed (LaTeX, R Sweave)

Using

\begin{flushleft}\underline{PDF-Document:}\end{flushleft}

soled the problem.

Are there better options than using "flushleft", some solution with any box-Class? Now I am using for all texts "flushleft".

how do you programatically create sub sections in latex/sweave

You are close, just put Sweave's

<<results=tex>>=

instead of (knitr's)

<<results='asis'>>=

Alternatively, use knitr. For that you might need to call Sweave2knitr() on the existing files.

For reference, see Sweave manual, page 13, or knitr chunk options.

How to print symbols with parenthesis in latex using xtable and Sweave

Ok, the following works for me. Your question didn't state whether you had the textcomp package loaded - you need that. Ditto the sideways package. I've also changed the sanitize.text.function to sanitize.text.function = function(x){x}.

The script below is saved as an .Rnw file.

\documentclass{article}
\usepackage{textcomp}
\usepackage{rotating}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<table, echo=FALSE, results=tex >>=
require(xtable)

tvPre <- as.data.frame(structure(list(`p\\\\_1` = c("\\textuparrow M", "\\textuparrow WH",
"\\textuparrow H", "\\textuparrow (WH)", "\\textuparrow W"),
`p\\\\_2` = c("", "", "", "", "\\textuparrow R"), `p\\\\_3` = c("\\textuparrow R",
"", "\\textuparrow (H)", "\\textuparrow (H)", "\\textdownarrow R"
)), .Names = c("p\\_1", "p\\_2", "p\\_3"), row.names = c("FV",
"a", "b", "c", "Oil"), class = "data.frame"))

print(xtable(tvPre), rotate.colnames = TRUE, sanitize.text.function = function(x){x})
@

\end{document}


Related Topics



Leave a reply



Submit