Skip to content

Commit

Permalink
Merge pull request #549 from rstudio/fix/new-citeproc-support
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv committed Nov 2, 2023
2 parents 8dd188a + eddb999 commit 058e404
Show file tree
Hide file tree
Showing 78 changed files with 10,666 additions and 1,194 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ jobs:
matrix:
config:
# testing R release with last shipped pandoc version in RStudio IDE and new pandoc
- {os: windows-latest, pandoc: '3.1.1', r: 'release'}
- {os: macOS-latest, pandoc: '3.1.1', r: 'release'}
- {os: windows-latest, pandoc: '3.1.9', r: 'release'}
- {os: macOS-latest, pandoc: '3.1.9', r: 'release'}
- {os: ubuntu-latest, pandoc: 'devel', r: 'release'}
# testing older pandoc versions
- {os: ubuntu-latest, pandoc: '3.1.7', r: 'release'} # special citeproc change
- {os: ubuntu-latest, pandoc: '2.19.2', r: 'release'}
- {os: ubuntu-latest, pandoc: '2.18', r: 'release'}
- {os: ubuntu-latest, pandoc: '2.17.1.1', r: 'release'}
Expand Down Expand Up @@ -74,8 +75,7 @@ jobs:
shell: Rscript {0}


- uses: r-lib/actions/setup-tinytex@v2-branch
# special branch required for TeX Live 2023 fix
- uses: r-lib/actions/setup-tinytex@v2
env:
# install full prebuilt version
TINYTEX_INSTALLER: TinyTeX
Expand All @@ -89,7 +89,7 @@ jobs:
with:
extra-packages: any::rcmdcheck
needs: check

- name: Remove default installed Pandoc
if: runner.os == 'Linux'
run: sudo dpkg -r pandoc
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rticles
Title: Article Formats for R Markdown
Version: 0.25.2
Version: 0.25.3
Authors@R: c(
person("JJ", "Allaire", , "jj@posit.co", role = "aut"),
person("Yihui", "Xie", , "xie@yihui.name", role = "aut",
Expand Down Expand Up @@ -99,7 +99,7 @@ Suggests:
withr,
covr,
testit,
testthat (>= 3.0.0),
testthat (>= 3.2.0),
xtable
Config/Needs/website: magick, pdftools, gifski, tidyverse/tidytemplate,
rstudio/quillt
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

- `elsevier_article()` Rmd skeleton is now updated to show different available bibliostyle file (i.e `*.bst` for _natbib_) with `elsearticle` document class. `biblio-style: elsarticle-harv` is set by default to correctly work with the `authoryear` classoption already set (thanks, @pepijn-devries, #545).

- Update `ajs_article()` resources for the template.

- Update `frontiers_article()` to version 3.4 from 2022.
- New document class file (.cls) and new bibliography style file (.bst)
- LaTeX template is also updated
- The format is now only compatible with `natbib` citation processor when Pandoc 3.1.7+ is used, because Frontiers documentclass requires usage of `natbib` and it conflicts with pandoc Citeproc.

- Update `mnras_article()` and `lncs_article()` to work with the Pandoc 3.1.7+ citeproc change.
# rticles 0.25

## BREAKING CHANGES
Expand Down
8 changes: 6 additions & 2 deletions R/article.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ elsevier_article <- function(..., keep_tex = TRUE,
#' <https://www.frontiersin.org/about/author-guidelines>.
#' @export
#' @rdname article
frontiers_article <- function(..., keep_tex = TRUE) {
pdf_document_format("frontiers", keep_tex = keep_tex, ...)
frontiers_article <- function(..., keep_tex = TRUE, citation_package = "natbib") {
# check all arguments for format's default
if (citation_package != "natbib" && rmarkdown::pandoc_available("3.1.7")) {
stop("Frontiers template only supports 'natbib' for citation processing. Pandoc citeproc is no more compatible with the Frontier documentclass.")
}
pdf_document_format("frontiers", keep_tex = keep_tex, citation_package = citation_package,...)
}


Expand Down
21 changes: 20 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ render_draft <- function(journal, output_options = NULL, quiet = FALSE) {

# Use to create variables command for Pandoc from a named vector
list_to_pandoc_variable_args <- function(v_args) {
# v_args must be named
# stopifnot(length(names(v_args) > 0)

truthy <- which(sapply(v_args, isTRUE))
truthy_arg <- NULL
if (length(truthy) > 0) {
Expand All @@ -152,6 +155,12 @@ list_to_pandoc_variable_args <- function(v_args) {
v_args <- v_args[-truthy]
}

# remove named args which are false
falsy <- sapply(v_args, isFALSE)
if (any(falsy)) {
v_args <- v_args[-which(falsy)]
}

# Convert to pandoc arguments
pandoc_arg_list <- c(
mapply(
Expand Down Expand Up @@ -211,13 +220,23 @@ string_to_table <- function(x, n, split_regex = ", ?") {
pdf_document_format <- function(format,
template = find_resource(format, "template.tex"),
...) {

fmt <- rmarkdown::pdf_document(..., template = template)
fmt$inherits <- "pdf_document"

## Set some variables to adapt template based on Pandoc version
args <- list_to_pandoc_variable_args(list(
pandoc3 = rmarkdown::pandoc_available("3")
pandoc3 = rmarkdown::pandoc_available("3"),
pandoc317 = rmarkdown::pandoc_available("3.1.7"), # new citeproc command
pandoc318 = rmarkdown::pandoc_available("3.1.8") # revised citeproc command
))
fmt$pandoc$args <- c(fmt$pandoc$args, args)
fmt
}


is_citeproc_pandoc_args <- function(args) {
"--citeproc" %in% args || "--natbib" %notin% args || "--biblatex" %notin% args
}

`%notin%` <- Negate(`%in%`)
55 changes: 55 additions & 0 deletions inst/rmarkdown/templates/acm/resources/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,60 @@

$if(csl-refs)$
% Pandoc citation processing
$if(pandoc318)$
%From Pandoc 3.1.8
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
$else$$if(pandoc317)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
% avoid brackets around text for \cite:
\makeatletter
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newlength{\cslentryspacing}
\setlength{\cslentryspacing}{0em}
\usepackage{enumitem}
\newlist{CSLReferences}{itemize}{1}
\setlist[CSLReferences]{label={},
leftmargin=\cslhangindent,
itemindent=-1\cslhangindent,
parsep=\parskip,
itemsep=\cslentryspacing}
$else$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
Expand All @@ -109,6 +163,7 @@
\setlength{\parskip}{#2\cslentryspacingunit}
}%
{}
$endif$$endif$
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
Expand Down
55 changes: 55 additions & 0 deletions inst/rmarkdown/templates/acs/resources/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,60 @@
$if(csl-refs)$
% Pandoc citation processing
$if(pandoc318)$
%From Pandoc 3.1.8
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
$else$$if(pandoc317)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
% avoid brackets around text for \cite:
\makeatletter
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newlength{\cslentryspacing}
\setlength{\cslentryspacing}{0em}
\usepackage{enumitem}
\newlist{CSLReferences}{itemize}{1}
\setlist[CSLReferences]{label={},
leftmargin=\cslhangindent,
itemindent=-1\cslhangindent,
parsep=\parskip,
itemsep=\cslentryspacing}
$else$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
Expand All @@ -121,6 +175,7 @@
\setlength{\parskip}{#2\cslentryspacingunit}
}%
{}
$endif$$endif$
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
Expand Down
55 changes: 55 additions & 0 deletions inst/rmarkdown/templates/aea/resources/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@

$if(csl-refs)$
% Pandoc citation processing
$if(pandoc318)$
%From Pandoc 3.1.8
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
$else$$if(pandoc317)$
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
% avoid brackets around text for \cite:
\makeatletter
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newlength{\cslentryspacing}
\setlength{\cslentryspacing}{0em}
\usepackage{enumitem}
\newlist{CSLReferences}{itemize}{1}
\setlist[CSLReferences]{label={},
leftmargin=\cslhangindent,
itemindent=-1\cslhangindent,
parsep=\parskip,
itemsep=\cslentryspacing}
$else$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
Expand All @@ -75,6 +129,7 @@
\setlength{\parskip}{#2\cslentryspacingunit}
}%
{}
$endif$$endif$
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
Expand Down
Loading

0 comments on commit 058e404

Please sign in to comment.