Print long lines of LaTeX / Sweave text

At the end of the survey I conducted, we provide the respondents with an open box to tell us everything that we did not consider in the survey. These comments often span multiple pages. I am familiar with the longtableLaTeX package , and here is the solution I was mocking:

<<results = tex>>=
cat("\\begin{longtable}{p{14cm}}\n")
cat("\\hline\n")
write.table(toBePrinted, eol = "\\\\\n", col.names = FALSE)
cat("\\hline\n")
cat("\\end{longtable}")
@

Although this solution works technically, it does not look terribly polished and needs to be improved. I have two related questions:

  • Text Sanitation Tips for Exiting Sweave To Be Handled As tex. For example, if someone says reak havok Your survey is awesome & I would take more surveys for $$$ 100% of the time!special characters &, $, %when processing through LaTeX. Is there anything more effective than a call list gsubfor replacing offending characters with something benevolent?
  • Sweave & LaTeX.
+3
1

xtable , , , longtable. , Hmisc, "longtable" .

, , - :

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 , 

: [[: punct:]] , . . .

+2

Source: https://habr.com/ru/post/1762286/


All Articles