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
    Commented Jun 7, 2017 at 1:27

1 Answer 1


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.


which adds to your document \section{Introduction}

  • 1
    Which example provides the answer to the question? I did not find it. Commented Aug 5, 2015 at 21:42
  • Try github.com/yihui/knitr/blob/master/inst/examples/…
    – Boris
    Commented Aug 5, 2015 at 21:43
  • I asked for a way for knitr to compose LaTeX code (i.e. markup) and add it to the document. Commented Aug 5, 2015 at 21:49
  • 2
    Sorry, did not understand your quesiton. Please see update.
    – Boris
    Commented Aug 5, 2015 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .