Skip to content

Commit

Permalink
Merge pull request #48 from Pakillo/latex
Browse files Browse the repository at this point in the history
0.2.10 add LaTeX as output format
  • Loading branch information
Pakillo authored Jul 4, 2024
2 parents a9dc810 + 0943e6d commit 11e7c02
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: grateful
Title: Facilitate Citation of R Packages
Version: 0.2.9
Version: 0.2.10
Authors@R: c(
person("Francisco", "Rodriguez-Sanchez",
email = "[email protected]",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# grateful 0.2.10

* Add support for LaTeX output (thanks @patrick-weiss for the suggestion). Use `out.format = "tex-document"` to obtain a full LaTeX document, or `out.format = "tex-fragment"` to produce just a fragment citing packages to be copied into another existing LaTeX document.

# grateful 0.2.9

* Match package names with citation keys explicitly, rather than relying on sorting.
Expand Down
7 changes: 5 additions & 2 deletions R/cite_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
#' the references. It is recommended to set `out.dir = getwd()`.
#'
#' @param out.format Output format when `output = "file"`:
#' either "html" (the default), "docx" (Word), "pdf", "Rmd", or "md" (markdown).
#' either "html" (the default), "docx" (Word), "pdf",
#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document),
#' "tex-document" (full LaTeX document), "Rmd", or "md" (markdown).
#' (Note that choosing "pdf" requires a working installation of LaTeX,
#' see <https://yihui.org/tinytex/>).
#'
Expand Down Expand Up @@ -158,7 +160,8 @@

cite_packages <- function(output = c("file", "paragraph", "table", "citekeys"),
out.dir = NULL,
out.format = c("html", "docx", "pdf", "Rmd", "md"),
out.format = c("html", "docx", "pdf", "Rmd", "md",
"tex-fragment", "tex-document"),
citation.style = NULL,
pkgs = "All",
omit = c("grateful"),
Expand Down
7 changes: 5 additions & 2 deletions R/create_rmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#' as produced by [get_pkgs_info()].
#' @param csl Optional. Citation style to format references.
#' See <https://www.zotero.org/styles>.
#' @param out.format Output format. One of: "html", docx" (Word), "pdf", "Rmd", or "md" (markdown).
#' @param out.format Output format, either "html", docx" (Word), "pdf",
#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document),
#' "tex-document" (full LaTeX document), "Rmd"(Rmarkdown), or "md" (markdown).
#' @param Rmd.file Name of the Rmarkdown file to be created.
#' @param include.RStudio Include RStudio?
#' @param passive.voice Logical. If `TRUE`, uses passive voice in any paragraph
Expand All @@ -23,7 +25,8 @@ create_rmd <- function(pkgs.df = NULL,
bib.file = "grateful-refs",
csl = NULL,
Rmd.file = "grateful-report",
out.format = c("html", "docx", "pdf", "Rmd", "md"),
out.format = c("html", "docx", "pdf", "Rmd", "md",
"tex-fragment", "tex-document"),
include.RStudio = FALSE,
passive.voice = FALSE,
...) {
Expand Down
18 changes: 15 additions & 3 deletions R/render_citations.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#'
#' @param Rmd.file Path to Rmarkdown file generated by [create_rmd()].
#' @param out.dir Directory to save the output document.
#' @param out.format Output format, either "html", docx" (Word), "pdf", or "md" (markdown).
#' @param out.format Output format, either "html", docx" (Word), "pdf",
#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document),
#' "tex-document" (full LaTeX document), or "md" (markdown).
#'
#' @return A document in the format specified by `out.format`.
#'
Expand All @@ -11,7 +13,8 @@

render_citations <- function(Rmd.file = NULL,
out.dir = NULL,
out.format = c("html", "docx", "pdf", "md")
out.format = c("html", "docx", "pdf", "md",
"tex-fragment", "tex-document")
) {

if (is.null(Rmd.file)) {
Expand All @@ -28,14 +31,23 @@ render_citations <- function(Rmd.file = NULL,
if (out.format == "pdf") output <- "pdf_document"
if (out.format == "html") output <- "html_document"
if (out.format == "md") output <- "md_document"
if (out.format == "tex-fragment") output <- "latex_fragment"
if (out.format == "tex-document") output <- "latex_document"

output.options <- NULL
if (out.format == "tex-fragment" | out.format == "tex-document") {
out.format <- "tex"
output.options <- list(latex_engine = "xelatex", citation_package = "natbib")
}

out.name <- gsub(pattern = ".Rmd", replacement = "", basename(Rmd.file))

rmarkdown::render(
input = Rmd.file,
output_format = output,
output_file = paste0(out.name, ".", out.format),
output_dir = out.dir
output_dir = out.dir,
output_options = output.options
)

out.file <- file.path(out.dir, paste0(out.name, ".", out.format))
Expand Down
6 changes: 4 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ knitr::opts_chunk$set(
[![Codecov test coverage](https://codecov.io/gh/Pakillo/grateful/branch/master/graph/badge.svg)](https://app.codecov.io/gh/Pakillo/grateful?branch=master)
`r badger::badge_lifecycle("stable")`
`r badger::badge_repostatus("Active")`
[![HitCount since 2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square)](https://hits.dwyl.com/Pakillo/grateful)
[![HitCount since 2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square&show=unique)](https://hits.dwyl.com/Pakillo/grateful)
[![](https://cranlogs.r-pkg.org/badges/grand-total/grateful)](https://cran.r-project.org/package=grateful)
<!-- badges: end -->

The goal of **grateful** is to make it very easy to cite R and the R packages used in any analyses, so that package authors receive their deserved credit. By calling a single function, **grateful** will scan the project for R packages used and generate a BibTeX file containing all citations for those packages.

**grateful** can then generate a new document with citations in the desired output format (Word, PDF, HTML, Markdown). These references can be formatted for a specific journal, so that we can just paste them directly into our manuscript or report.
**grateful** can then generate a new document with citations in the desired output format (Word, PDF, LaTeX, HTML, Markdown). These references can be formatted for a specific journal, so that we can just paste them directly into our manuscript or report.

Alternatively, we can use **grateful** directly within an [Rmarkdown](https://rmarkdown.rstudio.com/) or [Quarto](https://quarto.org/) document. In this case, a paragraph containing in-text citations of all used R packages will (optionally) be inserted into the Rmarkdown/Quarto document, and these packages will be included in the reference list when rendering.

Expand Down Expand Up @@ -75,7 +77,7 @@ cite_packages(out.dir = ".") # save report to working directory

<br>

This document can also be a Word document, PDF file, markdown file, or left as the source Rmarkdown file using `out.format`:
This document can also be a Word or LaTeX document, PDF file, markdown file, or left as the source Rmarkdown file using `out.format`:

```{r eval = FALSE}
cite_packages(out.format = "docx", out.dir = ".")
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ coverage](https://codecov.io/gh/Pakillo/grateful/branch/master/graph/badge.svg)]
[![Project Status: Active - The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![HitCount since
2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square)](https://hits.dwyl.com/Pakillo/grateful)
[![HitCount since
2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square&show=unique)](https://hits.dwyl.com/Pakillo/grateful)
[![](https://cranlogs.r-pkg.org/badges/grand-total/grateful)](https://cran.r-project.org/package=grateful)
<!-- badges: end -->

Expand All @@ -25,9 +29,9 @@ the project for R packages used and generate a BibTeX file containing
all citations for those packages.

**grateful** can then generate a new document with citations in the
desired output format (Word, PDF, HTML, Markdown). These references can
be formatted for a specific journal, so that we can just paste them
directly into our manuscript or report.
desired output format (Word, PDF, LaTeX, HTML, Markdown). These
references can be formatted for a specific journal, so that we can just
paste them directly into our manuscript or report.

Alternatively, we can use **grateful** directly within an
[Rmarkdown](https://rmarkdown.rstudio.com/) or
Expand Down Expand Up @@ -85,8 +89,8 @@ cite_packages(out.dir = ".") # save report to working directory

<br>

This document can also be a Word document, PDF file, markdown file, or
left as the source Rmarkdown file using `out.format`:
This document can also be a Word or LaTeX document, PDF file, markdown
file, or left as the source Rmarkdown file using `out.format`:

``` r
cite_packages(out.format = "docx", out.dir = ".")
Expand Down
6 changes: 4 additions & 2 deletions man/cite_packages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions tests/testthat/test-create_rmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("NULL arguments return error", {
test_that("create_rmd produces correct Rmd", {

skip_on_cran()
skip_on_ci()
# skip_on_ci()

pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir())
rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "Rmd")
Expand All @@ -27,7 +27,7 @@ test_that("create_rmd produces correct Rmd", {
"",
"|Package |Version |Citation |",
"|:--------|:-------|:---------|",
paste0("|grateful |", utils::packageVersion("grateful")," |@grateful |"),
paste0("|grateful |", utils::packageVersion("grateful")," |@grateful |"),
"",
"**You can paste this paragraph directly in your report:**",
"",
Expand All @@ -39,3 +39,25 @@ test_that("create_rmd produces correct Rmd", {


})



test_that("csl is downloaded if needed", {
skip_on_cran()
pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir())
rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "Rmd",
csl = "peerj")
expect_true(file.exists(file.path(tempdir(), "peerj.csl")))

})


test_that("rendered report is produced", {
skip_on_cran()
skip_on_ci()
pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir())
rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "html")
expect_true(file.exists(file.path(tempdir(), "grateful-report.html")))
expect_true(!file.exists(file.path(tempdir(), "grateful-report.Rmd")))

})
10 changes: 10 additions & 0 deletions tests/testthat/test-render_citations.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ test_that("render_citations returns a report", {
expect_equal(rendcit, file.path(tempdir(), "grateful-report.md"))
expect_true(file.exists(file.path(tempdir(), "grateful-report.md")))

# LaTeX fragment
rendcit <- render_citations(Rmd.file = rmd, out.dir = tempdir(), out.format = "tex-fragment")
expect_equal(rendcit, file.path(tempdir(), "grateful-report.tex"))
expect_true(file.exists(file.path(tempdir(), "grateful-report.tex")))

# LaTeX document
rendcit <- render_citations(Rmd.file = rmd, out.dir = tempdir(), out.format = "tex-document")
expect_equal(rendcit, file.path(tempdir(), "grateful-report.tex"))
expect_true(file.exists(file.path(tempdir(), "grateful-report.tex")))

})

0 comments on commit 11e7c02

Please sign in to comment.