Produce LaTeX from Rmarkdown

What is the easiest way to produce simple LaTeX from an RMarkdown file, i.e. without the preamble?

In other words, if I have this R code in a code chunk in an Rmd document:

plot(sin(seq(-pi, pi, length.out = 100)))

I would like to produce a .tex file containing an \includegraphics statement with the generated figure.

Is this possible, or would I be better off simply using Sweave?

Not quite sure what you want to achieve. If I knit the following Rmd file in the RStudio environment the result is a pdf file and a tex file with preamble. You can remove the preamble from the tex file.

# title: "Produce LaTeX from Rmarkdown"
# author: "awellis"
# date: "4/23/2020"
graphics: yes
    keep_tex: true

## My plot

```{r sinus-plot, echo=FALSE}
plot(sin(seq(-pi, pi, length.out = 100)))

Regards Han

1 Like

Thanks. What I want is a tex file without, a preamble, not a tex file from I have to remove the preamble. I can do that using Sweave.

I want to inlcude the generated tex file in maste tex file, from which I produce my manuscript.

best wishes

In that case, just use knit and pandoc_convert directly from the console or a script. E.g.,


rmarkdown::pandoc_convert("", to = "latex", output = "test.tex")

The knit command will produce The pandoc_convert command will produce test.tex, which by default will be a tex file without any preamble or \begin{document} ... \end{document} paraphernalia.


ah, thanks, that's what I was looking for. Seems about the same as using Sweave on an Rnw file.

In effect it is, yes. The main difference is that knitr's target output format is Markdown rather than .tex.

I’ve been curious about using LaTeX in RStudio and this has helped a lot. Thank you.
Out of curiosity, how would the plot command be written in the body of the Rnw file?

Jason the #rstatsnewbie

you can include figures like so:

<<fig, echo = FALSE, fig = TRUE>>=
plot(sin(seq(-pi, pi, length.out = 100)))
    \caption{This is a caption}

The code chunk is:

<< >>=
your code goes here
1 Like

Thank you! I’ve use LaTeX for the last couple of years and am migrating to using R/RStudio for my analyses. This helps a lot with that process. :+1:

What do these mean/do? Do they signal a change in environment or something?

Is there a good resource available, especially for citations, bibtex, references etc.?

Jason the #rstatsnewbie

That is noweb syntax. Code chunks are demarcated by the << >>= and documentation chunks begin with @. See here

The documentation chunks contain LaTeX code, so citations, etc. work as usual.

1 Like

I have some examples of Rmd files that produce PDF files with none or more LaTeX.
See the github repository HanOostdijk/rmd_pdf_examples.

The bookdown package has a lot of functionality for 'citations, bibtex, references'. The names suggest that is useful only for books but it can also be used for articles. Chapter 2 of bookdown: Authoring Books and Technical Documents with R Markdown sections 6 and 8 describe how to use references and citations and the latexpdf part describes some specifics for the use of LaTeX.

1 Like

This is great information. Thank you. I will definitely be looking into those references. Hopefully this will help me merge my workflow into one arena.

Thanks again.

~ Jason the #rstatsnewbie

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.