-
Notifications
You must be signed in to change notification settings - Fork 53
/
_common.R
44 lines (38 loc) · 1.17 KB
/
_common.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# convert pdf to png
to_png <- function(fig_path) {
png_path <- sub("\\.pdf$", ".png", fig_path)
magick::image_write(magick::image_read_pdf(fig_path), format = "png", path = png_path,
density = 300, quality = 100)
return(png_path)
}
library(formatR)
is_latex <- identical(knitr::opts_knit$get("rmarkdown.pandoc.to"), "latex")
is_html <- identical(knitr::opts_knit$get("rmarkdown.pandoc.to"), "html")
knitr::knit_hooks$set(
optipng = knitr::hook_optipng,
pdfcrop = knitr::hook_pdfcrop,
small.mar = function(before, options, envir) {
if (before) par(mar = c(4, 4, .1, .1), cex.lab = .95, cex.axis = .9, mgp = c(2, .7, 0), tcl = -.3) # smaller margin on top and right
},
font.cn = function(before, options, envir) {
if (before) pdf.options(family = "GB1")
},
font.en = function(before, options, envir) {
if (before) pdf.options(family = "Times")
}
)
knitr::opts_chunk$set(
fig.align = "center",
cache = TRUE,
small.mar = TRUE,
fig.showtext = TRUE,
dpi = 300
)
if (is_latex) {
knitr::opts_chunk$set(
out.width = "70%"
)
}
append_ext <- function(filename) {
paste(filename, ifelse(is_latex, "pdf", "svg"), sep = ".")
}