编译由RStudio生成的乳胶文件

时间:2016-04-18 00:19:41

标签: latex r-markdown beamer

我正在尝试编辑由latex生成的RStudio文件,但似乎RStudio生成的文件无法开箱即用。

以下是模板rmarkdown(因为我想在幻灯片中显示代码,所以进行了一些编辑)

---
title: "Untitled"
author: "SN248"
date: "April 17, 2016"
output:
  beamer_presentation:
    keep_tex: true
    theme: "CambridgeUS"
---

```{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>.

Some code
```{r eval=FALSE, echo=TRUE}
install.packages("mypackage")
library(mypackage)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=TRUE}
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.

注意,我添加了keep_tex: true来获取中间tex文件,生成的tex文件粘贴在下面:

\begin{frame}[fragile]{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}.

Some code

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}

\end{frame}

\begin{frame}[fragile]{Including Plots}

You can also embed plots, for example:

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(pressure)}
\end{Highlighting}
\end{Shaded}

\includegraphics{Untitled_files/figure-beamer/pressure-1.pdf}

Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.

\end{frame}

我注意到生成的tex文件没有开箱即用。所以,我添加了必要的位(即包listings用于代码突出显示),以便编译

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}
\usepackage{listings}
\usetheme{CambridgeUS}
\begin{document}
 ...
\end{document}

我仍然无法编译此tex文件,因为位begin{Shaded}无法编译,我收到以下错误:

LaTeX Error: Environment Shaded undefined

我的问题是,如何从tex生成RStudio文件,该文件开箱即用。如果没有,我应该使用哪些包来编译tex代码(特别是显示突出显示的代码),如下所示

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}

为了给出一些上下文,我正在尝试编译tex文件,以便我可以在tex代码中添加部分和子部分,我不知道如何在{{rmarkdown代码中执行此操作。 1}}文件。

非常感谢你的帮助。

SN248

1 个答案:

答案 0 :(得分:1)

我可以看到你正在使用RMarkdown并希望生成一个可以在没有knitr的情况下编译的独立的.tex文件。 我用SWeave做同样的事情。由于SWeave是带有R代码的乳胶文档,因此一旦运行SWeave并创建数字,您就可以轻松编译它。

例如,我在RStudio中创建了这个文档,并在Rstudio中编译它。

\documentclass{article}

\title{Best title ever}
\author{XTriMa, PhD}

\begin{document}
\maketitle
\tableofcontents
\section{Method} \label{intro}
Let me make two vectors with random numbers and plot them.

<<>>=
x<-runif(100)
y <- x^(sin(x))
plot(x, y)
@
\end{document}

我的文件夹看起来像这样

figure                test.log  test.Rnw         test.tex
test-concordance.tex  test.pdf  test.synctex.gz  test.toc

现在如果你运行

$pdflatex test.tex

它按预期工作。但是命令“latex”不起作用,因为它无法用于图中的pdf图形。

优势:我可以非常有效地使用R和乳胶,并摆脱像RStudio和Sweav这样的中间软件/客户。