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

Build PDF through tinytex::latexmk() #1222

Merged
merged 10 commits into from
Dec 15, 2017
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Imports:
methods,
stringr (>= 1.2.0)
Suggests:
tinytex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about importing it and making it the default behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was about to ask you. My only concern is that tinytex is a new package on CRAN, and I'm not sure if users with R versions lower than 3.3 are able to install it since CRAN won't provide binaries. Well, they surely can, but they just have to install.packages('tinytex', type = 'source') (it is a pure R package and does not require compilation).

I was wondering if we need to wait a little longer before fully routing to tinytex.

shiny (>= 0.11),
tufte,
testthat,
Expand Down
28 changes: 13 additions & 15 deletions R/render.R
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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