From cbd0a0c3799154f00c89d31ff6e03c897281eee5 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Wed, 6 Sep 2017 10:42:16 +0200 Subject: [PATCH 1/6] use post_processor to place \begin{fmtext} and \end{fmtext} --- NAMESPACE | 1 - R/rsos_article.R | 110 +++++++++++++---- .../rsos_article/resources/template.tex | 8 -- .../rsos_article/skeleton/skeleton.Rmd | 116 +++++++++--------- man/rsos_article.Rd | 11 +- 5 files changed, 154 insertions(+), 92 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 39b12b81..22b1f1e9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -20,7 +20,6 @@ importFrom(rmarkdown,output_format) importFrom(rmarkdown,pandoc_options) importFrom(rmarkdown,pandoc_path_arg) importFrom(rmarkdown,pandoc_variable_arg) -importFrom(rmarkdown,pdf_document) importFrom(rmarkdown,render) importFrom(utils,file_test) importFrom(utils,install.packages) diff --git a/R/rsos_article.R b/R/rsos_article.R index 465d4cf9..ed8f7ee7 100644 --- a/R/rsos_article.R +++ b/R/rsos_article.R @@ -16,35 +16,97 @@ #' } #' #' @export -rsos_article <- function(..., keep_tex = TRUE, citation_package = "natbib") { - inherit_pdf_document( - ..., - keep_tex = keep_tex, - citation_package = citation_package, - template = find_resource("rsos_article", "template.tex") - ) -} - -find_file <- function(template, file) { +rsos_article <- function( + ..., + keep_tex = TRUE, + citation_package = "natbib", + pandoc_args = NULL, + includes = NULL, + fig_crop = TRUE +) { template <- system.file( - "rmarkdown", "templates", template, file, + "rmarkdown/templates/rsos_article/resources/template.tex", package = "INBOmd" ) - if (template == "") { - stop("Couldn't find template file ", template, "/", file, call. = FALSE) + + args <- c("--template", template, pandoc_args) + + # citations + citation_package <- match.arg(citation_package) + if (citation_package == "natbib") { + args <- c(args, paste0("--", citation_package)) } + # content includes + args <- c(args, includes_to_pandoc_args(includes)) - template -} + extra <- list(...) + if (length(extra) > 0) { + args <- c( + args, + sapply( + names(extra), + function(x){ + pandoc_variable_arg(x, extra[[x]]) + } + ) + ) + } + opts_chunk <- list( + latex.options = "{}", + dev = "pdf", + fig.align = "center", + dpi = 300, + fig.width = 4.5, + fig.height = 2.9 + ) + crop <- fig_crop && + !identical(.Platform$OS.type, "windows") && + nzchar(Sys.which("pdfcrop")) + if (crop) { + knit_hooks <- list(crop = knitr::hook_pdfcrop) + opts_chunk$crop <- TRUE + } else { + knit_hooks <- NULL + } -find_resource <- function(template, file) { - find_file(template, file.path("resources", file)) -} + post_processor <- function( + metadata, input_file, output_file, clean, verbose + ) { + text <- readLines(output_file, warn = FALSE) -#'@importFrom rmarkdown pdf_document -# Call rmarkdown::pdf_documet and mark the return value as inheriting pdf_document -inherit_pdf_document <- function(...) { - fmt <- rmarkdown::pdf_document(...) - fmt$inherits <- "pdf_document" - fmt + # set correct text in fmtext environment + end_first_page <- grep("\\\\EndFirstPage", text) #nolint + if (length(end_first_page) == 1) { + maketitle <- grep("\\\\maketitle", text) + text <- c( + text[1:(maketitle - 1)], + "\\begin{fmtext}", + text[(maketitle + 1):(end_first_page - 1)], + "\\end{fmtext}", + "\\maketitle", + text[(end_first_page + 1):length(text)] + ) + writeLines(enc2utf8(text), output_file, useBytes = TRUE) + } + output_file + } + + output_format( + knitr = knitr_options( + opts_knit = list( + width = 60, + concordance = TRUE + ), + opts_chunk = opts_chunk, + knit_hooks = knit_hooks + ), + pandoc = pandoc_options( + to = "latex", + latex_engine = "xelatex", + args = args, + keep_tex = keep_tex + ), + post_processor = post_processor, + clean_supporting = !keep_tex + ) } diff --git a/inst/rmarkdown/templates/rsos_article/resources/template.tex b/inst/rmarkdown/templates/rsos_article/resources/template.tex index 7684ebcc..82588fa6 100644 --- a/inst/rmarkdown/templates/rsos_article/resources/template.tex +++ b/inst/rmarkdown/templates/rsos_article/resources/template.tex @@ -73,14 +73,6 @@ \providecommand{\tightlist}{% \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} -%%%%%%%%%% Insert the texts which can accomdate on firstpage in the tag "fmtext" %%%%% - -\begin{fmtext} -$first_page_text$ -\end{fmtext} - -%%%%%%%%%%%%%%% End of first page %%%%%%%%%%%%%%%%%%%%% - \maketitle $body$ diff --git a/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd index 2d2ec361..7df88d32 100644 --- a/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd @@ -29,64 +29,6 @@ keywords: abstract: | The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. - -first_page_text: | - # Insert A head here - - This demo file is intended to serve as a "starter file"" for articles - submitted to the Royal Society Open Science journal using `RMarkdown`. - - The RMarkdown argument `first_page_text` should hold all plain text to be - displayed on the first page of the page. Warning: excess text will be hidden - behind the copyright box. The example below contains line 1 to 19 in the code. - Line 14 to 17 are hidden. - - ## Insert B head here - - Subsection text here. - - ### Insert C head here - - Subsubsection text here. - - Line 1 - - Line 2 - - Line 3 - - Line 4 - - Line 5 - - Line 6 - - Line 7 - - Line 8 - - Line 9 - - Line 10 - - Line 11 - - Line 12 - - Line 13 - - Line 14 - - Line 15 - - Line 16 - - Line 17 - - Line 18 - - Line 19 - ## Remove this if not required ethics: | Please provide details on the ethics. @@ -121,6 +63,64 @@ lineno: false output: INBOmd::rsos_article --- +# Insert A head here + +This demo file is intended to serve as a "starter file"" for articles +submitted to the Royal Society Open Science journal using `RMarkdown`. + +The RMarkdown argument `first_page_text` should hold all plain text to be +displayed on the first page of the page. Warning: excess text will be hidden +behind the copyright box. The example below contains line 1 to 19 in the code. +Line 14 to 17 are hidden. + +## Insert B head here + +Subsection text here. + +### Insert C head here + +Subsubsection text here. + +Line 1 + +Line 2 + +Line 3 + +Line 4 + +Line 5 + +Line 6 + +Line 7 + +Line 8 + +Line 9 + +Line 10 + +Line 11 + +Line 12 + +Line 13 + +Line 14 + +Line 15 + +Line 16 + +Line 17 + +Line 18 + +Line 19 + +\EndFirstPage + # Lists * one diff --git a/man/rsos_article.Rd b/man/rsos_article.Rd index 6c3eec72..3b8408a8 100644 --- a/man/rsos_article.Rd +++ b/man/rsos_article.Rd @@ -4,7 +4,8 @@ \alias{rsos_article} \title{Royal Society Open Science journal format.} \usage{ -rsos_article(..., keep_tex = TRUE, citation_package = "natbib") +rsos_article(..., keep_tex = TRUE, citation_package = "natbib", + pandoc_args = NULL, includes = NULL, fig_crop = TRUE) } \arguments{ \item{...}{Additional arguments to \code{rmarkdown::pdf_document}} @@ -13,6 +14,14 @@ rsos_article(..., keep_tex = TRUE, citation_package = "natbib") \item{citation_package}{The LaTeX package to process citations, \code{natbib} or \code{biblatex}. Use \code{none} if neither package is to be used.} + +\item{pandoc_args}{Additional command line options to pass to pandoc} + +\item{includes}{Named list of additional content to include within the +document (typically created using the \code{\link{includes}} function).} + +\item{fig_crop}{\code{TRUE} to automatically apply the \code{pdfcrop} utility +(if available) to pdf figures} } \value{ R Markdown output format to pass to From e0efc9b6b5b9af596aff7c68e346165f6e0f4d5c Mon Sep 17 00:00:00 2001 From: ThierryO Date: Wed, 6 Sep 2017 10:55:42 +0200 Subject: [PATCH 2/6] fix lintr --- R/rsos_article.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/rsos_article.R b/R/rsos_article.R index ed8f7ee7..efcac38c 100644 --- a/R/rsos_article.R +++ b/R/rsos_article.R @@ -77,7 +77,7 @@ rsos_article <- function( # set correct text in fmtext environment end_first_page <- grep("\\\\EndFirstPage", text) #nolint if (length(end_first_page) == 1) { - maketitle <- grep("\\\\maketitle", text) + maketitle <- grep("\\\\maketitle", text) #nolint text <- c( text[1:(maketitle - 1)], "\\begin{fmtext}", From adb9df3772022a689af8733d5a2d1aff721b9f2e Mon Sep 17 00:00:00 2001 From: ThierryO Date: Fri, 8 Sep 2017 16:27:38 +0200 Subject: [PATCH 3/6] define \EndFirstPage as a dummy command --- R/rsos_article.R | 6 +++--- .../rmarkdown/templates/rsos_article/resources/template.tex | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/R/rsos_article.R b/R/rsos_article.R index efcac38c..b84b8b57 100644 --- a/R/rsos_article.R +++ b/R/rsos_article.R @@ -75,8 +75,8 @@ rsos_article <- function( text <- readLines(output_file, warn = FALSE) # set correct text in fmtext environment - end_first_page <- grep("\\\\EndFirstPage", text) #nolint - if (length(end_first_page) == 1) { + end_first_page <- grep("^\\\\EndFirstPage", text) #nolint + if (length(end_first_page)) { maketitle <- grep("\\\\maketitle", text) #nolint text <- c( text[1:(maketitle - 1)], @@ -86,8 +86,8 @@ rsos_article <- function( "\\maketitle", text[(end_first_page + 1):length(text)] ) - writeLines(enc2utf8(text), output_file, useBytes = TRUE) } + writeLines(enc2utf8(text), output_file, useBytes = TRUE) output_file } diff --git a/inst/rmarkdown/templates/rsos_article/resources/template.tex b/inst/rmarkdown/templates/rsos_article/resources/template.tex index 82588fa6..2eb825fe 100644 --- a/inst/rmarkdown/templates/rsos_article/resources/template.tex +++ b/inst/rmarkdown/templates/rsos_article/resources/template.tex @@ -72,6 +72,8 @@ %% Some pieces required from the pandoc template \providecommand{\tightlist}{% \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} +\providecommand{\EndFirstPage}{% +} \maketitle From e5a1b07c02ac8deb073010ace98e8687e06594be Mon Sep 17 00:00:00 2001 From: ThierryO Date: Fri, 8 Sep 2017 17:02:03 +0200 Subject: [PATCH 4/6] force to keep tex --- R/rsos_article.R | 5 ++--- man/rsos_article.Rd | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/R/rsos_article.R b/R/rsos_article.R index b84b8b57..fee0819e 100644 --- a/R/rsos_article.R +++ b/R/rsos_article.R @@ -18,7 +18,6 @@ #' @export rsos_article <- function( ..., - keep_tex = TRUE, citation_package = "natbib", pandoc_args = NULL, includes = NULL, @@ -104,9 +103,9 @@ rsos_article <- function( to = "latex", latex_engine = "xelatex", args = args, - keep_tex = keep_tex + keep_tex = TRUE ), post_processor = post_processor, - clean_supporting = !keep_tex + clean_supporting = FALSE ) } diff --git a/man/rsos_article.Rd b/man/rsos_article.Rd index 3b8408a8..66db6b60 100644 --- a/man/rsos_article.Rd +++ b/man/rsos_article.Rd @@ -4,14 +4,12 @@ \alias{rsos_article} \title{Royal Society Open Science journal format.} \usage{ -rsos_article(..., keep_tex = TRUE, citation_package = "natbib", - pandoc_args = NULL, includes = NULL, fig_crop = TRUE) +rsos_article(..., citation_package = "natbib", pandoc_args = NULL, + includes = NULL, fig_crop = TRUE) } \arguments{ \item{...}{Additional arguments to \code{rmarkdown::pdf_document}} -\item{keep_tex}{Keep the intermediate tex file used in the conversion to PDF} - \item{citation_package}{The LaTeX package to process citations, \code{natbib} or \code{biblatex}. Use \code{none} if neither package is to be used.} From dab417ea21075a3d727dbd084e56551732090952 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Mon, 11 Sep 2017 14:38:51 +0200 Subject: [PATCH 5/6] post_processor() requires bookdown --- R/rsos_article.R | 32 ++++++++++--------- .../rsos_article/skeleton/skeleton.Rmd | 4 ++- man/rsos_article.Rd | 7 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/R/rsos_article.R b/R/rsos_article.R index fee0819e..40f8bdd8 100644 --- a/R/rsos_article.R +++ b/R/rsos_article.R @@ -16,29 +16,30 @@ #' } #' #' @export +#' @importFrom rmarkdown output_format knitr_options pandoc_options pandoc_variable_arg includes_to_pandoc_args rsos_article <- function( ..., - citation_package = "natbib", + keep_tex = TRUE, pandoc_args = NULL, includes = NULL, fig_crop = TRUE ) { + + extra <- list(...) + template <- system.file( "rmarkdown/templates/rsos_article/resources/template.tex", package = "INBOmd" ) + args <- c( + "--template", template, + "--latex-engine", "xelatex", + pandoc_variable_arg("documentclass", "article"), + pandoc_args, + "--natbib", + includes_to_pandoc_args(includes) + ) - args <- c("--template", template, pandoc_args) - - # citations - citation_package <- match.arg(citation_package) - if (citation_package == "natbib") { - args <- c(args, paste0("--", citation_package)) - } - # content includes - args <- c(args, includes_to_pandoc_args(includes)) - - extra <- list(...) if (length(extra) > 0) { args <- c( args, @@ -50,6 +51,7 @@ rsos_article <- function( ) ) } + opts_chunk <- list( latex.options = "{}", dev = "pdf", @@ -93,7 +95,7 @@ rsos_article <- function( output_format( knitr = knitr_options( opts_knit = list( - width = 60, + width = 96, concordance = TRUE ), opts_chunk = opts_chunk, @@ -103,9 +105,9 @@ rsos_article <- function( to = "latex", latex_engine = "xelatex", args = args, - keep_tex = TRUE + keep_tex = keep_tex ), post_processor = post_processor, - clean_supporting = FALSE + clean_supporting = !keep_tex ) } diff --git a/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd index 7df88d32..185ff024 100644 --- a/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd @@ -60,7 +60,9 @@ bibliography: sample.bib ## change to true to add optional line numbering lineno: false -output: INBOmd::rsos_article +output: + bookdown::pdf_book: + base_format: INBOmd::rsos_article --- # Insert A head here diff --git a/man/rsos_article.Rd b/man/rsos_article.Rd index 66db6b60..87f873e3 100644 --- a/man/rsos_article.Rd +++ b/man/rsos_article.Rd @@ -4,14 +4,13 @@ \alias{rsos_article} \title{Royal Society Open Science journal format.} \usage{ -rsos_article(..., citation_package = "natbib", pandoc_args = NULL, - includes = NULL, fig_crop = TRUE) +rsos_article(..., keep_tex = TRUE, pandoc_args = NULL, includes = NULL, + fig_crop = TRUE) } \arguments{ \item{...}{Additional arguments to \code{rmarkdown::pdf_document}} -\item{citation_package}{The LaTeX package to process citations, \code{natbib} -or \code{biblatex}. Use \code{none} if neither package is to be used.} +\item{keep_tex}{Keep the intermediate tex file used in the conversion to PDF} \item{pandoc_args}{Additional command line options to pass to pandoc} From d3b65e055af0cfe350b900b672b0062ec1e97a13 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Mon, 11 Sep 2017 14:39:45 +0200 Subject: [PATCH 6/6] ignore .zenodo.json --- .Rbuildignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.Rbuildignore b/.Rbuildignore index 400664d6..3ad202cd 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,4 @@ ^.*\.Rproj$ ^\.Rproj\.user$ ^wercker\.yml$ +^\.zenodo\.json$