rsos_article.R 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #' Royal Society Open Science journal format.
  2. #'
  3. #' Format for creating submissions to Royal Society Open Science journals.
  4. #'
  5. #' @inheritParams rmarkdown::pdf_document
  6. #' @param ... Additional arguments to \code{rmarkdown::pdf_document}
  7. #'
  8. #' @return R Markdown output format to pass to
  9. #' \code{\link[rmarkdown:render]{render}}
  10. #'
  11. #' @export
  12. #' @importFrom rmarkdown output_format knitr_options pandoc_options pandoc_variable_arg includes_to_pandoc_args
  13. #' @author Thierry Onkelinx, \email{thierry.onkelinx@@inbo.be}
  14. rsos_article <- function(
  15. ...,
  16. keep_tex = TRUE,
  17. pandoc_args = NULL,
  18. includes = NULL,
  19. fig_crop = TRUE
  20. ) {
  21. extra <- list(...)
  22. template <- system.file(
  23. "rmarkdown/templates/rsos_article/resources/template.tex",
  24. package = "rticles"
  25. )
  26. args <- c(
  27. "--template", template,
  28. "--pdf-engine", "xelatex",
  29. pandoc_variable_arg("documentclass", "article"),
  30. pandoc_args,
  31. "--natbib",
  32. includes_to_pandoc_args(includes)
  33. )
  34. if (length(extra) > 0) {
  35. args <- c(
  36. args,
  37. sapply(
  38. names(extra),
  39. function(x){
  40. pandoc_variable_arg(x, extra[[x]])
  41. }
  42. )
  43. )
  44. }
  45. opts_chunk <- list(
  46. latex.options = "{}",
  47. dev = "pdf",
  48. fig.align = "center",
  49. dpi = 300,
  50. fig.width = 4.5,
  51. fig.height = 2.9,
  52. highlight = FALSE,
  53. echo = FALSE
  54. )
  55. crop <- fig_crop &&
  56. !identical(.Platform$OS.type, "windows") &&
  57. nzchar(Sys.which("pdfcrop"))
  58. if (crop) {
  59. knit_hooks <- list(crop = knitr::hook_pdfcrop)
  60. opts_chunk$crop <- TRUE
  61. } else {
  62. knit_hooks <- NULL
  63. }
  64. post_processor <- function(
  65. metadata, input_file, output_file, clean, verbose
  66. ) {
  67. text <- readLines(output_file, warn = FALSE)
  68. # set correct text in fmtext environment
  69. end_first_page <- grep("^\\\\EndFirstPage", text) #nolint
  70. if (length(end_first_page)) {
  71. maketitle <- grep("\\\\maketitle", text) #nolint
  72. text <- c(
  73. text[1:(maketitle - 1)],
  74. "\\begin{fmtext}",
  75. text[(maketitle + 1):(end_first_page - 1)],
  76. "\\end{fmtext}",
  77. "\\maketitle",
  78. text[(end_first_page + 1):length(text)]
  79. )
  80. }
  81. writeLines(enc2utf8(text), output_file, useBytes = TRUE)
  82. output_file
  83. }
  84. output_format(
  85. knitr = knitr_options(
  86. opts_knit = list(
  87. width = 75,
  88. concordance = TRUE
  89. ),
  90. opts_chunk = opts_chunk,
  91. knit_hooks = knit_hooks
  92. ),
  93. pandoc = pandoc_options(
  94. to = "latex",
  95. latex_engine = "xelatex",
  96. args = args,
  97. keep_tex = keep_tex
  98. ),
  99. post_processor = post_processor,
  100. clean_supporting = !keep_tex
  101. )
  102. }