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

查看:32
本文介绍了“编译 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?

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

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}
egin{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天全站免登陆