Skip to content

Commit

Permalink
fix #954: also check if ghostscript is available before trying to cro…
Browse files Browse the repository at this point in the history
…p the PDF plots
  • Loading branch information
yihui committed Sep 9, 2020
1 parent 706e070 commit 0e7db03
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

- `knitr::write_bib()` uses the `URL` in the package `DESCRIPTION` if it is provided, instead of the canonical CRAN URL for the package.

- `hook_pdfcrop()` and `plot_crop()` will work only when both programs `pdfcrop` and `ghostscript` have been installed. Previously only `pdfcrop` was checked (thanks, @dalupus, #954).

# CHANGES IN knitr VERSION 1.29

## NEW FEATURES
Expand Down
11 changes: 2 additions & 9 deletions R/hooks-extra.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
#' provides a few useful hooks, which can also serve as examples of how to
#' define chunk hooks in \pkg{knitr}.
#'
#' The function \code{hook_pdfcrop()} can use the program \command{pdfcrop} to
#' crop the extra white margin when the plot format is PDF to make better use of
#' the space in the output document, otherwise we often have to struggle with
#' \code{graphics::\link{par}()} to set appropriate margins. Note
#' \command{pdfcrop} often comes with a LaTeX distribution such as MiKTeX or
#' TeXLive, and you may not need to install it separately (use
#' \code{Sys.which('pdfcrop')} to check it; if it not empty, you are able to use
#' it). Similarly, when the plot format is not PDF (e.g. PNG), the \pkg{magick}
#' package is used to crop the plot.
#' The function \code{hook_pdfcrop()} calls \code{\link{pdf_crop}()} to crop the
#' white margins of PDF plots.
#'
#' The function \code{hook_optipng()} calls the program \command{optipng} to
#' optimize PNG images. Note the chunk option \code{optipng} can be used to
Expand Down
14 changes: 12 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,20 @@ fig_process = function(FUN, path, options) {

#' Crop a plot (remove the edges) using PDFCrop or ImageMagick
#'
#' The command \command{pdfcrop} (often shipped with a LaTeX distribution) is
#' The program \command{pdfcrop} (often shipped with a LaTeX distribution) is
#' executed on a PDF plot file, and
#' \code{magick::\link[magick:transform]{image_trim}()} is executed for other
#' types of plot files.
#'
#' The program \command{pdfcrop} can crop the extra white margins when the plot
#' format is PDF, to make better use of the space in the output document,
#' otherwise we often have to struggle with \code{graphics::\link{par}()} to set
#' appropriate margins. Note \command{pdfcrop} often comes with a LaTeX
#' distribution such as TinyTeX, MiKTeX, or TeX Live, and you may not need to
#' install it separately (use \code{Sys.which('pdfcrop')} to check it; if it not
#' empty, you are able to use it). Note that \command{pdfcrop} depends on
#' GhostScript. You can check if GhostScript is installed via
#' \code{tools::find_gs_cmd()}.
#' @param x Filename of the plot.
#' @param quiet Whether to suppress standard output from the command.
#' @export
Expand All @@ -315,7 +325,7 @@ plot_crop = function(x, quiet = TRUE) {
is_pdf = grepl('[.]pdf$', x, ignore.case = TRUE)
x2 = x
x = path.expand(x)
if (is_pdf && !has_utility('pdfcrop')) return(x2)
if (is_pdf && !has_utility('pdfcrop') && !has_utility('ghostscript')) return(x2)

if (!quiet) message('cropping ', x)
if (is_pdf) {
Expand Down
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,8 @@ kpsewhich = function() {
has_utility = function(name, package = name) {
name2 = paste('util', name, sep = '_') # e.g. util_pdfcrop
if (is.logical(yes <- opts_knit$get(name2))) return(yes)
# a special case: use tools::find_gs_cmd() to find ghostscript
if (name == 'ghostscript') name = tools::find_gs_cmd()
yes = nzchar(Sys.which(name))
if (!yes) warning(package, ' not installed or not in PATH')
opts_knit$set(setNames(list(yes), name2))
Expand Down

0 comments on commit 0e7db03

Please sign in to comment.