rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.79k stars 1.27k forks source link

Keyboard character used is undefined - rlang traceback with PDF #710

Closed muschellij2 closed 5 years ago

muschellij2 commented 5 years ago

By filing an issue to this repo, I promise that

I'm trying to reproduce, which is a bit hard due to different nature from when using Rscript vs. using RStudio.

Problem

LaTeX failures when rlang-style traceback in PDF book in bookdown, when error = TRUE in code chunk.

This may be due to @r-lib rlang not recognizing PDF output (not sure if it should) or bookdown should set option if necessary.

Solution

Set options(rlang_backtrace_on_error = "none") in Rmd file.

Example failures

With xelatex

/usr/local/bin/pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --table-of-contents --toc-depth 2 --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --number-sections --highlight-style tango --pdf-engine xelatex --natbib --variable graphics=yes --wrap preserve --variable 'geometry:margin=1in' --variable 'compact-title:yes' --variable tables=yes --standalone 
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
! Text line contains an invalid character.
l.191 ^^[
         [90m  1. ^^[[39mbookdown::render_book(input = ".", output_format = ...

Error: Failed to compile test.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See test.log for more info.

With pdflatex:

! Package inputenc Error: Keyboard character used is undefined
(inputenc)                in inputencoding `utf8'.

Does not reproduce in reprex

I think this may be due to traceback options in rlang with non-UTF8 characters in traceback for PDF outputs for `bookdown.

Here is the uploaded test files: test.zip, but they are downloaded in the scrip below.

I have not found this to completely reproduce in @tidyverse reprex, but if you run this script locally (RStudio or Terminal R), it will error whenever rlang_backtrace_on_error seems to be set to anything but "none" or `"reminder"':

options(tinytex.verbose = TRUE); 
destfile = tempfile(fileext = ".zip")
download.file("https://github.com/rstudio/bookdown/files/3132337/test.zip",
              destfile = destfile)
outdir = tempfile()
dir.create(outdir, recursive = TRUE)
unz = unzip(zipfile = destfile, exdir = outdir)

test_dir = file.path(outdir, "test")
dir(test_dir)
dir.exists(test_dir)
setwd(test_dir)

#' ## pdflatex with no branching - works
options(rlang_backtrace_on_error = "none")
bookdown::render_book(input = ".", output_format = "bookdown::pdf_book",
                      output_options=list(latex_engine = "pdflatex"))

#' ## pdflatex with reminder only - works
options(rlang_backtrace_on_error = "reminder")
bookdown::render_book(input = ".", output_format = "bookdown::pdf_book",
                      output_options=list(latex_engine = "pdflatex"))

#' ## pdflatex with branching - fails
options(rlang_backtrace_on_error = "branch")
bookdown::render_book(input = ".", output_format = "bookdown::pdf_book",
                      output_options=list(latex_engine = "pdflatex"))

#' ## xelatex with no branching - works
options(rlang_backtrace_on_error = "none")
bookdown::render_book(input = ".", output_format = "bookdown::pdf_book",
                      output_options=list(latex_engine = "xelatex"))
#' ## xelatex with branching - fails
options(rlang_backtrace_on_error = "branch")
bookdown::render_book(input = ".", output_format = "bookdown::pdf_book",
                      output_options=list(latex_engine = "xelatex"))
yihui commented 5 years ago

I bet there must be certain ANSI escape codes. I feel this issue is not directly relevant to bookdown but essentially a LaTeX issue. You'll have to either disable ANSI escape codes from rlang, or figure out how to deal with them in LaTeX (e.g. https://tex.stackexchange.com/q/168460/9128).

muschellij2 commented 5 years ago

Ahh good to know. I will look into this a bit more, but wanted to present the problem and current solution in case anyone else ran into it. Will close.

yihui commented 5 years ago

Sounds good. Thank you!

github-actions[bot] commented 4 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.