编译RStudio生成的乳胶文件 [英] compiling latex file generated by RStudio
问题描述
我正在尝试编辑 RStudio
生成的 latex
文件,但是似乎 RStudio
生成的文件没有开箱即用地编译.
I am trying to edit the latex
file generated by RStudio
but it seems the file generated by RStudio
does not compile out of the box.
这是模板 rmarkdown
(已编辑过一些,以便在幻灯片中显示代码)
Here is the template rmarkdown
(edited a little as I want to show code in my slides)
---
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
文件粘贴在下面:
Note, that I added keep_tex: true
to get the intermediate tex
file, the generated tex
file is pasted below:
\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
打包以突出显示代码),以便编译
I noticed that the generated tex
file does not compile out of the box. So, I added the necessary bits (i.e., package listings
for code highlighting) so that it compiles
\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}
\usepackage{listings}
\usetheme{CambridgeUS}
\begin{document}
...
\end{document}
我仍然无法编译此 tex
文件,因为 begin {Shaded}
位未编译,但出现以下错误:
I am still not able to compile this tex
file as the bit begin{Shaded}
does not compile, I get the following error:
LaTeX错误:环境阴影未定义
我的问题是,如何从 RStudio
生成 tex
文件,该文件可以直接进行编译.如果没有,我应该使用哪些包来编译如下所示的 tex
代码(尤其是带有突出显示的代码)
My question is, how to generate tex
file from RStudio
which compiles out of the box. If not, which packages should I use to compile the tex
code (especially to show code with highlighting) shown below
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
仅提供一些上下文,我正在尝试编译 tex
文件,以便可以在 tex
代码中添加节和小节,我不确定如何在 rmarkdown
文件中执行此操作.
Just to give some context, I am trying to compile the tex
file so that I can add sections and subsections in the tex
code, I am not sure how to do that in the rmarkdown
file.
非常感谢您的帮助.
SN248
推荐答案
我可以看到您正在使用RMarkdown,并且想要生成一个独立的.tex文件,该文件可以不使用knitr进行编译.我用SWeave做同样的事情.由于SWeave是带有R代码的乳胶文档,因此在运行SWeave和创建图形后,您可以轻松地对其进行编译.
I can see that you are using RMarkdown and want to generate a stand alone .tex file that can be compiled without knitr. I do the same thing with SWeave. As SWeave is a latex document with R code, you can easily compile it once SWeave is run and figures are created.
例如,我已经在RStudio中创建了此文档,并在Rstudio中对其进行了编译.
For Example, I have created this document in RStudio and compile it in 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}
我的文件夹看起来像这样
My folder looks like this
figure test.log test.Rnw test.tex
test-concordance.tex test.pdf test.synctex.gz test.toc
现在,如果您运行
$pdflatex test.tex
它按预期工作.但是,命令"latex"将无法使用,因为它无法与数字pdf图形一起使用.
It works as you intended. However command "latex" will not work as it can not work with pdf graphics in figures.
优势:我可以非常高效地使用R和Latex,并摆脱了RStudio和Sweav等中间软件/客户端.
Advantage: I can work with R and latex very efficiently and get rid of intermediate software/clients like RStudio and Sweav.
这篇关于编译RStudio生成的乳胶文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!