Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer bib engine from file contents #19

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
.Rhistory
.RData
.Ruserdata

*.aux
*.bbl
*.blg
*.bcf
*.log
*.run.xml

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: r
sudo: false
latex: false
cache: packages

env:
Expand Down
30 changes: 28 additions & 2 deletions R/latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
#' @param file A LaTeX file path.
#' @param engine A LaTeX engine (can be set in the global option
#' \code{tinytex.engine}, e.g., \code{options(tinytex.engine = 'xelatex')}).
#' @param bib_engine A bibliography engine (can be set in the global option
#' @param bib_engine A bibliography engine, one of \code{'bibtex'} and
#' \code{'biber'} (can be set in the global option
#' \code{tinytex.bib_engine}).
#' If missing, the engine is inferred from the contents of \code{file}: if
#' the file contains \verb{\addbibresource} then the engine is assumed to
#' be \code{'biber'} unless \verb{backend=biber} occurs in the optional argument
#' to \code{biblatex}, provided the package occurs on one line. Otherwise,
#' the engine is assumed to be \code{'bibtex'} if \verb{\bibliography}
#' is present in \code{file}.
#' @param engine_args Command-line arguments to be passed to \code{engine} (can
#' be set in the global option \code{tinytex.engine_args}, e.g.,
#' \code{options(tinytex.engine_args = '-shell-escape'}).
Expand Down Expand Up @@ -57,7 +64,26 @@ latexmk = function(
}
}
if (missing(max_times)) max_times = getOption('tinytex.compile.max_times', max_times)
if (missing(bib_engine)) bib_engine = getOption('tinytex.bib_engine', bib_engine)
if (missing(bib_engine)) {
if (is.null(getOption('tinytex.bib_engine'))) {
file_lines = sub('(?<!(\\\\))[%].*$', '%', readLines(file), perl = TRUE)
if (any(grepl('\\addbibresource{', file_lines, fixed = TRUE))) {
if (any(grepl('\\\\usepackage\\[[^\\]]*backend\\s*[=]\\s*bibtex.*\\{biblatex\\}',
file_lines,
perl = TRUE))) {
bib_engine = 'bibtex'
} else {
bib_engine = 'biber'
}
} else if (any(grepl('\\bibliography{', file_lines, fixed = TRUE))) {
bib_engine = 'bibtex'
} else {
bib_engine = NULL
}
} else {
bib_engine = getOption('tinytex.bib_engine')
}
}
if (missing(engine_args)) engine_args = getOption('tinytex.engine_args', engine_args)
owd = setwd(dirname(file))
on.exit(setwd(owd), add = TRUE)
Expand Down
11 changes: 9 additions & 2 deletions man/latexmk.Rd

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

8 changes: 8 additions & 0 deletions tests/test-travis/bib-inference/backend-bibtex-a.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@Manual{R-base,
title = {R: A Language and Environment for Statistical Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2016},
url = {https://www.R-project.org/},
}
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test-travis/bib-inference/backend-bibtex-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
R Core Team [1].


References
[1] R Core Team. R: A Language and Environment for Statistical Comput-
ing. R Foundation for Statistical Computing. Vienna, Austria, 2016. url:
https://www.R-project.org/.




1

9 changes: 9 additions & 0 deletions tests/test-travis/bib-inference/backend-bibtex.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}
\usepackage[backend=bibtex]{biblatex}
\addbibresource{backend-bibtex-a}

\begin{document}
\textcite{R-base}.
\printbibliography
\end{document}

9 changes: 9 additions & 0 deletions tests/test-travis/bib-inference/the-biber.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}
\usepackage{biblatex}

\addbibresource{backend-bibtex-a.bib}

\begin{document}
\textcite{R-base}.
\printbibliography
\end{document}
43 changes: 43 additions & 0 deletions tests/test-travis/test-bib_inference.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
library(testit)

if (Sys.which("pdflatex") == "") {
tinytex::install_tinytex()
}

assert("Bibtex inference", {
current_wd = getwd()
on.exit(setwd(current_wd), add = TRUE)
setwd("bib-inference")


first_res = is.null(pdflatex("backend-bibtex.tex")) &&
if (nzchar(Sys.which('pdftotext'))) {
system('pdftotext -layout backend-bibtex.pdf')
backend_bibtex_text = readLines('backend-bibtex.txt', n = 8L, warn = FALSE)
backend_bibtex_expected = readLines('backend-bibtex-expected.txt', n = 8L, warn = FALSE)
setwd(current_wd)
identical(backend_bibtex_text,
backend_bibtex_expected)
} else {
setwd(current_wd)
TRUE # skip test
}
})

assert("Biber inference", {
current_wd = getwd()
on.exit(setwd(current_wd), add = TRUE)
setwd("bib-inference")
first_res = is.null(pdflatex("the-biber.tex")) &&
if (nzchar(Sys.which('pdftotext'))) {
system('pdftotext -layout the-biber.pdf')
backend_bibtex_text = readLines('the-biber.txt', n = 8L, warn = FALSE)
backend_bibtex_expected = readLines('backend-bibtex-expected.txt', n = 8L, warn = FALSE)
setwd(current_wd)
identical(backend_bibtex_text,
backend_bibtex_expected)
} else {
setwd(current_wd)
TRUE # skip test
}
})