1

I'm very new to knitr. How do I write R code in knitr that composes LaTeX from scratch and injects it into the document at a particular point?

I'm thinking of something similar to what PHP or JSP does.

  • 1
    To answer the title's question, knitr is "simply" an R function that turns a quasi-LaTeX document into a .tex file. – Hugh Jun 7 '17 at 1:27
2

TeX is a text for knitr, so you can use any text-generating function, e.g. cat(). To include the text into the document, add echo=FALSE, results='asis' to the chunk options, e.g.

<<echo=FALSE,results="asis">>=
title="Introduction"
cat(paste("\\section{",title,"}",sep=""))
@ 

which adds to your document \section{Introduction}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.