How to print section name of latex code from R-code using knitr?

I am trying to create a latex simulation report with knitR. My R code has a cycle for products and generates a graph for each product. I want to include a section title for each iteration of the loop. I used resuls = 'asis' and tried to print the section header in a loop, as in the code snippet below:

<<looptest, echo=FALSE, results='asis', warning=FALSE>>= for (product in c("prod 1","prod 2")){ print(paste("\\section{",product,"}", sep="")) } @ 

The problem is that I get this when I exit latex:

 [1] "\\section{prod 1}" [1] "\\section{prod 2}" 
+4
source share
2 answers

Thomas invited me to post this as an answer. The solution is to use cat () instead of print ()

 <<looptest, echo=FALSE, results='asis', warning=FALSE>>= for (product in c("prod 1","prod 2")){ cat(paste("\\section{",product,"}", sep="")) } @ 

Has the correct latex output:

 \section{prod 1} \section{prod 2} 
+3
source

If you are hung up on something to dynamically generate sections / chapters, I suggest using the functionality of the knit_child loop. See code from examples of knitr examples on GitHub.

+2
source

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


All Articles