“编译PDF"和“编译PDF"之间的区别和knit2pdf [英] Difference between "Compile PDF" and knit2pdf

查看:94
本文介绍了“编译PDF"和“编译PDF"之间的区别和knit2pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Rnw文件,可以使用RStudio中的编译PDF"按钮(或Command + Shift + k)将其编译为PDF. 但是,当我使用knit2pdf时,不会创建图形,也不会创建完整的PDF.为什么会这样?您如何具体设置图像的存储位置,以便pdflatex可以找到它们?

I have a .Rnw file that I am able to compile into a PDF using the "Compile PDF" button in RStudio (or Command+Shift+k). However, when I use knit2pdf the graphics are not created and the complete PDF is not created. Why would this happen? How do you specifically set where the images will be stored so that pdflatex can find them?

这里是一个例子.我知道几天前我发布的这个问题一个类似的例子,但在我看来,这是两个不同的问题.

Here is an example. I am aware that this question that I posted a few days ago has a similar example, but in my mind these are two different questions.

如果我点击编译",此文件将运行良好并生成PDF.我没有任何错误,该图是在/figure目录中生成的,一切都很好.

This file will run just fine and produce a PDF if I hit "Compile". I don't get any errors, the figure is produced in the /figure directory, and all is well.

%test.Rnw
\documentclass{article}
\usepackage[margin=.5in, landscape]{geometry}
\begin{document}

This is some test text!

<<setup, include=FALSE, results='hide', cache=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE,
 cache = FALSE, error = FALSE)
library(ggplot2)
@

<<printplotscreen, results='asis'>>=
ggplot(diamonds) + 
  geom_bar(aes(x = color, stat = "bin"))
@

\end{document}

但是,当我运行此脚本的目的是与单击编译"(是吗?)完全相同时,将不会创建该图形,并且在下面出现了不令人惊讶的错误关于找不到它.

However, when I run this script that is intended to do exactly the same thing as hitting "Compile" (is it?) the figure is not created and I get the not-surprising error below about not being able to find it.

#test.R
library("knitr")

knit2pdf(input = "~/Desktop/thing/test.Rnw",
         output=paste0('~/Desktop/thing/test','.tex'))

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'test.tex' failed.
LaTeX errors:
! LaTeX Error: File `figure/printplotscreen-1' not found.

注意:如果您想重现此内容(并感谢!),请确保先运行knit2pdf脚本以查看它不会创建图形.如果您首先点击编译",则这些数字将在此处供knit2pdf使用,但不能准确表示情况.

NOTE: If you are trying to reproduce this (and thanks!) then make sure you run the knit2pdf script FIRST to see that it doesn't create the figures. If you hit "Compile" first then the figures will be there for knit2pdf to use, but it will not accurately represent the situation.

推荐答案

解决方案:在使用knit2pdf之前,请确保将工作目录设置为项目目录,然后将输入"路径缩短为只是.Rnw文件.因此...

The solution: Make sure to set the working directory to the project directory before using knit2pdf, then shorten the "input" path to just the .Rnw file. Thus...

test.R
library("knitr")
diamonds = diamonds[diamonds$cut != "Very Good",]

setwd("/Users/me/Desktop/thing")
knit2pdf(input = "test.Rnw", output = "test.tex")

这篇关于“编译PDF"和“编译PDF"之间的区别和knit2pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆