【问题标题】:How to convert Rmarkdown file to working latex file如何将 Rmarkdown 文件转换为工作乳胶文件
【发布时间】:2020-07-26 06:56:11
【问题描述】:

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

我已经阅读了一些暗示这是可能的帖子(例如,How to generate LaTeX file without preamble in R markdown?),这让我有所了解,但我仍然遇到问题。

例如,使用上面帖子中提到的方法,我可以将测试.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.

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

knitr::knit("test.Rmd")

在我的工作目录中获得一个降价文件test.md,然后我显然可以将此.md文件转换为.tex

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

这会生成一个.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.

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

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

【问题讨论】:

  • 使用 pdf_document 选项 keep_tex 保留文档的完整 LaTeX 源代码。它可能在序言中包含比您真正想要的更多内容;您可以使用template 选项指定一个更简单的选项。
  • 谢谢,这对了解也很有帮助!

标签: latex r-markdown pandoc


【解决方案1】:

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

rmarkdown::pandoc_convert(
  "test.md",
  to = "latex",
  output = "out.tex",
  options = "--standalone"
)

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

render("test.Rmd", output_format = "latex_document")

感兴趣的项目可能是rticles

【讨论】:

  • 哇,谢谢,这似乎很好用!我知道文章并且以前玩过一些模板,但也许我错过了模板的重点?以我的经验,提交一个相当精简的符合期刊要求的 word 或 latex 文件,如果稿件被接受,期刊的人会负责最终布局。以期刊的最终布局风格提交文章有什么好处?
  • 我认为这可能因期刊而异。 rticles 的一个优点是可以轻松设置必要的元数据信息。
猜你喜欢
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 2011-07-28
  • 2011-01-29
  • 2021-06-11
  • 1970-01-01
  • 2023-03-15
  • 2011-10-31
相关资源
最近更新 更多