Skip to content

PDF Chapter Preview does not incorporate "includes:" arguments #888

Open

Description

@connorp

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:

  1. Create a new bookdown project (just the minimal project created by
    File -> New Project… -> Book Project using bookdown is sufficient for a reprex).

  2. Add the following (example) line to `preamble.tex':

    \DeclareMathOperator*{\argmax}{arg\,max}

  3. Add the following line to a book chapter, for example 03-methods.Rmd:

    $$\argmax_f f(x)$$

  4. Compile the full book, confirming that the added LaTeX math line is rendered correctly (f centered below argmax)

  5. 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, since preamble.txt was not added to the document. Inspection of the log file and .tex file 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

  1. added
    bug
    on Mar 29, 2021
  2. cderv commented on Mar 29, 2021

    @cderv
    Collaborator

    TL;DR: to preview with another format than default one (first in _output.yaml) use the full format name package::function()

    bookdown::preview_chapter('03-method.Rmd', output_format = "bookdown::pdf_book")
    

    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.yaml where you added the preamble.tex in 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 fresh bookdown::pdf_book() format with default option (hence no preamble).

    This is because output_format argument should be the full format "bookdown::pdf_book()", so that the key is match in the _output.yaml file.

    For example, try restart R in your project, do no load bookdown and try running

    bookdown::preview_chapter('03-method.Rmd', output_format = "pdf_book")
    

    You'll get an error

    > bookdown::preview_chapter('03-method.Rmd', output_format = "pdf_book")
    Error in eval(parse(text = name)) : object 'pdf_book' not found
    

    because pdf_book is not a key in your YAML for a format and the function pdf_book() does not work.

    This is not a specificity to bookdown - this will happen with any format defined in _output.yaml

    Usually 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 name bookdown::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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdocumentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions