问题
I have a LaTeX document that is split to multiple .tex files.
I'm using R markdown to generate figures and tables.
Is it possible to generate .tex file from .Rmd without preamble, so that I will be able to just use output in my document? Currently, I need to manually copy part of the output to my .tex file
回答1:
Let's suppose your child document is named child.Rmd
and has the following contents.
```{r pressure, echo=FALSE, dev='pdf'}
plot(pressure)
```
Run
knitr::knit("child.Rmd")
and you get figure/pressure-1.pdf
and child.md
. Then run
rmarkdown::pandoc_convert("child.md", to = "latex", output = "child.tex")
Resulting child.tex
file only contains \begin{figure} ~~ \end{figure}
block inside. I hope this is the desired result.
If you don't have a strong reason to use RMarkdown, I'd recommend R Sweave; knitr supports child documents
UPDATE You didn't have to remove YAML header from your Rmd file; the above knitr::knit
and rmarkdown::pandoc_convert
combination ignores the YAML header. Take a look at this gist. Run run_this.R
script and child.Rmd
will be converted to child.tex
. You can of course render this Rmd file to html normally.
回答2:
You should be able to do this by creating a template containing just as much as you want. Instructions for templates are here: http://rmarkdown.rstudio.com/developer_document_templates.html
You could also look at bookdown
, which by design is for documents with
many chapters.
来源:https://stackoverflow.com/questions/42876148/how-to-generate-latex-file-without-preamble-in-r-markdown