rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.88k stars 979 forks source link

Interplay of header-includes, extra_dependencies, and in_header in pdf_document() #1359

Closed crsh closed 5 years ago

crsh commented 6 years ago

Hi,

I've noticed some unexpected interplay between the different methods of customizing the document preamble in PDF documents. header-includes and includes: in_header both work individually, but when I use both header-includes appears to be ignored:

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    includes:
      in_header: preamble.tex

header-includes:
  - \usepackage{booktabs}
---

preamble.tex could for example simply contain

\usepackage{tabulary}

However, once I include the additional extra_dependencies everything is included.

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
    extra_dependencies:
      - tabularx
    includes:
      in_header: preamble.tex

header-includes:
  - \usepackage{booktabs}
---

Is this intended behavior? To me it would be useful and less confusing if these three approaches could be used independently of one another.

crsh commented 6 years ago

Hi, sorry to bother again. In case this is not intended behavior, I'd be willing to contribute a pull request to fix this. Any pointers as to where this could originate would be appreciated.

mnazarov commented 5 years ago

This is actually an issue upstream in pandoc - see https://github.com/jgm/pandoc/issues/3139 and links from there. In short, in_header (which is transformed into command line option) overwrites YAML's header-includes, since command line options have 'higher precedence' (since they provide a way to overwrite YAML metadata), but the discussion is ongoing...

dmurdoch commented 5 years ago

One other observation: if you include extra_dependencies in the pdf_document call, then inline R code in the header-includes: field is not executed. For example, this works:

---
output: 
  pdf_document:
    keep_tex: true
header-includes: 
  - \usepackage{fancyhdr}
  - \pagestyle{fancy}
  - '`r paste0("\\fancyhead[CO,CE]{", params$figureno, "}")`'
params:
  figureno: "Fig. 1-1"
---

but this doesn't:

---
output: 
  pdf_document:
    keep_tex: true
    extra_dependencies: fancyhdr
header-includes: 
  - \pagestyle{fancy}
  - '`r paste0("\\fancyhead[CO,CE]{", params$figureno, "}")`'
params:
  figureno: "Fig. 1-1"
---

If you look at the generated .tex file in the second one, you can see that the R code was never executed, and the literal string including it was inserted into the header.

pauljohn32 commented 5 years ago

Problem still exists in pandoc-2.7. My document which has a template and an includes statement:

output:
  pdf_document:
    citation_package: natbib
    fig_caption: true
    highlight: haddock
    keep_tex: true
    latex_engine: pdflatex
    pandoc_args: [--listings]
    template: "theme/report-template.tex"
    includes:
      in_header: "theme/guidePreambleKnitr.tex"

was failing to run the "header-includes: " section. I solve problem in short term by following your fix to add an extra_dependencies flag.

This is a bad bug for me and I was having a hard time figuring it out until I found this post. Thanks very much to the original poster. I've grown frustrated with relying on pandoc because of problems like this.

yihui commented 5 years ago

As @mnazarov pointed out, this was by design of Pandoc. I agree it is confusing, so I just pushed a fix to always include the code from the header-includes field in the LaTeX preamble. Thanks for the report!

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.