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, 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. Aug 5, 2015 at 21:42
  • I asked for a way for knitr to compose LaTeX code (i.e. markup) and add it to the document. Aug 5, 2015 at 21:49
  • 2
    Sorry, did not understand your quesiton. Please see update.
    – Boris
    Aug 5, 2015 at 22:02

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.