如何将 Rmarkdown 文件转换为可用的 Latex 文件

hen*_*g87 5 latex pandoc r-markdown

我写了一篇手稿,我想提交给 Rmarkdown 的期刊。该杂志接受 word 和 latex 文件,所以我正在寻找一种方法来.tex从我的.Rmd文件中生成一个工作文件。

我已经阅读了一些暗示这是可能的帖子(例如,如何在 R markdown 中生成没有前导码的 LaTeX 文件?),这让我有所收获,但我仍然遇到问题。

例如,使用上面帖子中提到的方法,我可以将测试.Rmd转换为具有.tex文件类型的内容。这是测试 Rmarkdown(只是新文件的常用模板):

---
title: "Test document"
author: "Me"
date: "23 7 2020"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Run Code Online (Sandbox Code Playgroud)

这可以很好地编译为 PDF。然后,在控制台中我运行:

knitr::knit("test.Rmd")
Run Code Online (Sandbox Code Playgroud)

得到降价的文件test.md在我的工作目录,然后我可以在此显然转换.md文件.tex

rmarkdown::pandoc_convert("test.md", to = "latex", output = "test.tex")
Run Code Online (Sandbox Code Playgroud)

这会生成一个.tex文件,当我双击它时,会弹出一个看起来不错的文件的 PDF 视图。看看这个文件,它是不完整的,或者至少对我来说是陌生的:

\hypertarget{r-markdown}{%
\subsection{R Markdown}\label{r-markdown}}

This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.

When you click the \textbf{Knit} button a document will be generated
that includes both content as well as the output of any embedded R code
chunks within the document. You can embed an R code chunk like this:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(cars)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
\end{verbatim}

\hypertarget{including-plots}{%
\subsection{Including Plots}\label{including-plots}}

You can also embed plots, for example:

\begin{figure}
\centering
\includegraphics{figure/pressure-1.png}
\caption{plot of chunk pressure}
\end{figure}

Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
Run Code Online (Sandbox Code Playgroud)

据我所知,它缺少序言,\begin{document}, \end{document},而且我不知道hypertarget部分标题中发生了什么。不出所料,当我在 MiKTeX 中运行时它不会“重新编译”。不过verbatim,代码块的位看起来正是我所追求的。

那么,有没有办法生成一个从.tex文件编译出来的.Rmd文件?还是我必须手动编写序言之类的?如果我的问题的答案是“阅读 pandoc”,那么很公平,我将不得不咬紧牙关,最后看看它。但是我发现很难想象在 Rmarkdown 中没有好的(简单的)方法来准备可提交的手稿。

tar*_*leb 6

Pandoc 默认生成 LaTeX 片段,即不是完整的文档。这可以通过使用以下选项调用 pandoc 来更改--standalone

rmarkdown::pandoc_convert(
  "test.md",
  to = "latex",
  output = "out.tex",
  options = "--standalone"
)
Run Code Online (Sandbox Code Playgroud)

您可以让 R 完成工作并将命令缩短为

render("test.Rmd", output_format = "latex_document")
Run Code Online (Sandbox Code Playgroud)

感兴趣的项目可能是文章