jss_article.R 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #' Journal of Statistical Software (JSS) format.
  2. #'
  3. #' Format for creating a Journal of Statistical Software (JSS) articles. Adapted
  4. #' from
  5. #' \href{http://www.jstatsoft.org/about/submissions}{http://www.jstatsoft.org/about/submissions}.
  6. #'
  7. #' @inheritParams rmarkdown::pdf_document
  8. #' @param ... Arguments to \code{rmarkdown::pdf_document}
  9. #'
  10. #' @return R Markdown output format to pass to
  11. #' \code{\link[rmarkdown:render]{render}}
  12. #'
  13. #' @examples
  14. #'
  15. #' \dontrun{
  16. #' library(rmarkdown)
  17. #' draft("MyArticle.Rmd", template = "jss_article", package = "rticles")
  18. #' }
  19. #'
  20. #' @export
  21. jss_article <- function(..., keep_tex = TRUE, citation_package = 'natbib') {
  22. rmarkdown::pandoc_available('2.2', TRUE)
  23. template <- find_resource("jss_article", "template.tex")
  24. base <- inherit_pdf_document(
  25. ..., template = template, keep_tex = keep_tex, citation_package = citation_package
  26. )
  27. # Mostly copied from knitr::render_sweave
  28. base$knitr$opts_knit$out.format <- "sweave"
  29. base$knitr$opts_chunk <- merge_list(base$knitr$opts_chunk, list(
  30. prompt = TRUE, comment = NA, highlight = FALSE, tidy = FALSE,
  31. dev.args = list(pointsize = 11), fig.align = "center",
  32. fig.width = 4.9, # 6.125" * 0.8, as in template
  33. fig.height = 3.675 # 4.9 * 3:4
  34. ))
  35. base$pandoc$ext <- '.tex'
  36. post <- base$post_processor
  37. # a hack for https://github.com/rstudio/rticles/issues/100 to add \AND to the author list
  38. base$post_processor <- function(metadata, input, output, clean, verbose) {
  39. if (is.function(post)) output = post(metadata, input, output, clean, verbose)
  40. f <- xfun::with_ext(output, '.tex')
  41. x <- xfun::read_utf8(f)
  42. x <- gsub('( \\\\AND )\\\\And ', '\\1', x)
  43. x <- gsub(' \\\\AND(\\\\\\\\)$', '\\1', x)
  44. xfun::write_utf8(x, f)
  45. tinytex::latexmk(
  46. f, base$pandoc$latex_engine,
  47. if ('--biblatex' %in% base$pandoc$args) 'biber' else 'bibtex'
  48. )
  49. }
  50. hook_chunk <- function(x, options) {
  51. if (output_asis(x, options)) return(x)
  52. paste0('```{=latex}\n\\begin{CodeChunk}\n', x, '\\end{CodeChunk}\n```')
  53. }
  54. hook_input <- function(x, options) {
  55. if (options$prompt && length(x)) {
  56. x <- gsub("\\n", paste0("\n", "R+ "), x)
  57. x <- paste0("R> ", x)
  58. }
  59. paste0(c('\n\\begin{CodeInput}', x, '\\end{CodeInput}', ''),
  60. collapse = '\n')
  61. }
  62. hook_output <- function(x, options) {
  63. paste0('\n\\begin{CodeOutput}\n', x, '\\end{CodeOutput}\n')
  64. }
  65. base$knitr$knit_hooks <- merge_list(base$knitr$knit_hooks, list(
  66. chunk = hook_chunk,
  67. source = hook_input,
  68. output = hook_output,
  69. message = hook_output,
  70. warning = hook_output,
  71. plot = knitr::hook_plot_tex
  72. ))
  73. base
  74. }