compiling latex file generated by RStudio

浪子不回头ぞ 提交于 2019-12-13 02:22:55

问题


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.

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.

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}

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}

I am still not able to compile this tex file as the bit begin{Shaded} does not compile, I get the following error:

LaTeX Error: Environment Shaded undefined

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}

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.

Thank you very much for your help.

SN248


回答1:


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.

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

Now if you run

$pdflatex test.tex

It works as you intended. However command "latex" will not work as it can not work with pdf graphics in figures.

Advantage: I can work with R and latex very efficiently and get rid of intermediate software/clients like RStudio and Sweav.



来源:https://stackoverflow.com/questions/36683670/compiling-latex-file-generated-by-rstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!