【问题标题】:rstudio knitr not running pdfcroprstudio knitr没有运行pdfcrop
【发布时间】:2026-01-27 20:40:01
【问题描述】:

我正在尝试使用 rstudio (1.2.5019) 将下面的 mwe 编织成 pdf

---
title: "Test"
author: "Me"
output: 
  pdf_document: 
    fig_caption: yes
    keep_tex: yes
    fig_crop: yes
---
This should be cropped
```{r}
library(knitr)
```



```{r named-chunk}
plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
     main="Not cropped", xlab="Looks good", ylab="Nok")
```

输出的 pdf 看起来不错,只是图中没有使用 pdfcrop 裁剪。

这是日志输出。

处理文件:cropping.Rmd |.......
| 25% 没有 R 代码的普通文本

|.......................
| 50% 标签:unnamed-chunk-1
|……………………………………………………………………………………………………………………………… ...
| 75% 没有 R 代码的普通文本

|.................................................. ......................| 100% 标签:命名块

输出文件:cropping.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS cropping.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output cropping.tex --self-contained --highlight-style tango --pdf-engine pdflatex--可变图形--lua-filter “C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua” --lua-filter "C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua" --变量“几何:margin=1in”

创建的输出:cropping.pdf

pdfcrop 在我的命令行中可以正常工作,但不会被 rstudio 调用。它也位于rstudio。

Sys.which('pdfcrop') pdf裁剪 "C:\texlive\2019\bin\win32\pdfcrop.exe"

我错过了什么?

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

    这实际上解决了它。

    ``` {r crop-hook,cache=FALSE}
        knit_hooks$set(crop=hook_pdfcrop)
    ```
    
    
    ```{r named-chunk, crop=TRUE}
    plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
         main="Not cropped", xlab="Looks good", ylab="Nok")
    ```
    

    据我从文档中了解到,这应该没有必要?

    【讨论】:

    • 您在文档的什么地方读到了这个? bookdown.org 同意您的解决方案 - 您从哪里了解到您在问题中使用的语法?
    【解决方案2】:

    通过livetexmiktex 安装pdfcrop --- 用于编写 Latex 文档的那个。

    【讨论】: