Skip to content

Commit

Permalink
Build PDF through tinytex::latexmk() (#1222)
Browse files Browse the repository at this point in the history
* use tinytex::latexmk() if available

* always use latexmk() to create PDF and never use Pandoc to create the PDF directly

this will make it faster to build PDF with keep_tex, because Pandoc does not need to convert Markdown to LaTeX twice (once for .tex, and once for .pdf)

* load quietly

* tinytex is on CRAN now, so no longer need to load it from the dark

* a news item for the PR #1222

* remove version requirement of tinytex

* always assume the output file is PDF unless it has the extension .tex

* see if _R_CHECK_TESTS_NLINES_ works (I want to seem more lines of output when the check fails)

* preinstall the LaTeX package units

* tweak comments
  • Loading branch information
yihui committed Dec 15, 2017
1 parent 08c7567 commit cf05cb2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Expand Up @@ -5,8 +5,12 @@ cache:
pandoc_version: 1.19.2.1

env:
- PANDOC_VERSION=default
- PANDOC_VERSION=latest PATH=$HOME/bin:$PATH
global:
- _R_CHECK_TESTS_NLINES_=0
matrix:
- PANDOC_VERSION=default
- PANDOC_VERSION=latest PATH=$HOME/bin:$PATH

before_install:
- "[[ ${PANDOC_VERSION} = latest ]] && ./tools/install-pandoc.sh || true"
- tlmgr install units
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -74,6 +74,7 @@ Imports:
methods,
stringr (>= 1.2.0)
Suggests:
tinytex,
shiny (>= 0.11),
tufte,
testthat,
Expand Down
28 changes: 13 additions & 15 deletions R/render.R
Expand Up @@ -647,25 +647,23 @@ render <- function(input,
status
}
texfile <- file_with_ext(output_file, "tex")
# compile Rmd to tex when we need to generate bibliography with natbib/biblatex
if (need_bibtex) {
convert(texfile)
# manually compile tex if PDF output is expected
if (grepl('[.]pdf$', output_file)) {
# determine whether we need to run citeproc (based on whether we have
# references in the input)
run_citeproc <- citeproc_required(yaml_front_matter, input_lines)
# if the output format is LaTeX, first convert .md to .tex, and then convert
# .tex to .pdf via latexmk() if PDF output is requested (in rmarkdown <=
# v1.8, we used to call Pandoc to convert .md to .tex and .pdf separately)
if (output_format$pandoc$keep_tex || knitr:::is_latex_output()) {
# do not use pandoc-citeproc if needs to build bibliography
convert(texfile, run_citeproc && !need_bibtex)
# unless the output file has the extension .tex, we assume it is PDF
if (!grepl('[.]tex$', output_file)) {
latexmk(texfile, output_format$pandoc$latex_engine, '--biblatex' %in% output_format$pandoc$args)
file.rename(file_with_ext(texfile, "pdf"), output_file)
# clean up the tex file if necessary
if (!output_format$pandoc$keep_tex) on.exit(unlink(texfile), add = TRUE)
}
# clean up the tex file if necessary
if ((texfile != output_file) && !output_format$pandoc$keep_tex)
on.exit(unlink(texfile), add = TRUE)
} else {
# determine whether we need to run citeproc (based on whether we
# have references in the input)
run_citeproc <- citeproc_required(yaml_front_matter, input_lines)
# generate .tex if we want to keep the tex source
if (texfile != output_file && output_format$pandoc$keep_tex)
convert(texfile, run_citeproc)
# run the main conversion if the output file is not .tex
convert(output_file, run_citeproc)
}

Expand Down
4 changes: 4 additions & 0 deletions R/util.R
Expand Up @@ -314,6 +314,10 @@ escape_regex_metas <- function(in_str) {

# call latexmk to compile tex to PDF; if not available, use a simple emulation
latexmk <- function(file, engine, biblatex = FALSE) {
if (requireNamespace('tinytex', quietly = TRUE)) {
return(tinytex::latexmk(file, engine, if (biblatex) 'biber' else 'bibtex'))
}
warning('You are recommended to install the tinytex package to build PDF.', call. = FALSE)
if (!grepl('[.]tex$', file))
stop("The input file '", file, "' does not appear to be a LaTeX document")
engine <- find_latex_engine(engine)
Expand Down
4 changes: 4 additions & 0 deletions inst/NEWS
@@ -1,6 +1,10 @@
rmarkdown 1.9 (unreleased)
--------------------------------------------------------------------------------

## MAJOR CHANGES

* If the **tinytex** package is installed, PDF output is built through `tinytex::latexmk()`, otherwise it is generated by `rmarkdown:::latexmk()`, which has been factored out and improved in the **tinytex** package, so it is recommended that you install the **tinytex** package (#1222).

## BUG FIXES

* Temporary files created in `render()` may be cleaned up prematurely, which can cause problems with Shiny R Markdown documents (#1184).
Expand Down

0 comments on commit cf05cb2

Please sign in to comment.