Repository navigation
Description
Despite the recommendation to only preview to HTML, I sometimes like to preview a bookdown chapter to PDF, so I can print or share with collaborators. I believe the preview_chapter() function is failing to incorporate data from the includes: YAML option in _output.yml when previewing to PDF.
Steps to Reproduce:
-
Create a new bookdown project (just the minimal project created by
File -> New Project… -> Book Project using bookdownis sufficient for a reprex). -
Add the following (example) line to `preamble.tex':
\DeclareMathOperator*{\argmax}{arg\,max} -
Add the following line to a book chapter, for example
03-methods.Rmd:$$\argmax_f f(x)$$ -
Compile the full book, confirming that the added LaTeX math line is rendered correctly (
fcentered belowargmax) -
Preview the chapter to PDF:
preview_chapter('03-method.Rmd', output_format = "pdf_book")This function call will fail, because LaTeX does not know the command
\argmax, sincepreamble.txtwas not added to the document. Inspection of the log file and.texfile remaining after the error confirms the absence of the preamble contents.
Previewing to HTML as normal does not have the issue. Files specified via includes: are incorporated appropriately in this case.
One other observation I made, though I'm not sure it rises to the level of its own bug report: when previewing to PDF via preview_chapter('03-method.Rmd', output_format = "pdf_book"), the resulting PDF contains the entire book contents, rather than just the specified chapter, as I would expect. Is this the intended behavior?
Session Info:
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.2.5033
Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
Package version:
base64enc_0.1.3 bookdown_0.19 digest_0.6.25 evaluate_0.14 glue_1.4.0 graphics_3.6.3 grDevices_3.6.3 highr_0.8 htmltools_0.4.0
jsonlite_1.6.1 knitr_1.28 magrittr_1.5 markdown_1.1 methods_3.6.3 mime_0.9 Rcpp_1.0.4 rlang_0.4.6 rmarkdown_2.1
stats_3.6.3 stringi_1.4.6 stringr_1.4.0 tinytex_0.22 tools_3.6.3 utils_3.6.3 xfun_0.13 yaml_2.2.1
By filing an issue to this repo, I promise that
- I have fully read the issue guide at https://yihui.org/issue/.
- I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included
xfun::session_info('bookdown'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/bookdown'). - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
Activity
cderv commentedon Mar 29, 2021
TL;DR: to preview with another format than default one (first in
_output.yaml) use the full format namepackage::function()Preview will be correct, even with PDF output
Here is what happens. I classified that as a bug but it is a weird behavior because of how rmarkdown currently works with output formats.
You PDF output fortmat is defined in
_output.yamlwhere you added thepreamble.texin header.When you call
preview_chapter('03-method.Rmd', output_format = "pdf_book")after having loading bookdown (library(bookdown)), the call will work but not using the format you defined in_output.yaml. It will use a freshbookdown::pdf_book()format with default option (hence no preamble).This is because
output_formatargument should be the full format"bookdown::pdf_book()", so that the key is match in the_output.yamlfile.For example, try restart R in your project, do no load bookdown and try running
You'll get an error
because
pdf_bookis not a key in your YAML for a format and the functionpdf_book()does not work.This is not a specificity to bookdown - this will happen with any format defined in
_output.yamlUsually when you use
output_format=argument and you pass a character to match the name of a format, it is best to use the full namebookdown::pdf_book.I recognized this is pretty weird not a desired behavior because it can be very tricky to understand.
Changing this would be hard but if it happens, it should be in rmarkdown.
We'll try to better document this.