Search code examples
rr-markdownbookdown

Issue inserting image in header


I'm writing a report using multiple R Markdown files, and to facilitate combining them I decided to use the bookdown package. I face a problem with inserting a logo as header using fancyhdr LaTeX package, that worked fine in a regular .Rmd file.

index.Rmd file:

---
documentclass: book
classoption: openany
site: bookdown::bookdown_site
subparagraph: true
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
    toc_depth: 4
    latex_engine: xelatex # to receive Arial as font --> install.packages("xelatex") works for R users
link-citations: yes
fontsize: 12pt
linestretch: 1.25
---

The preamble.tex is:

\usepackage[german]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{fancyhdr}

\usepackage{fontspec}
\setmainfont{Arial}

\pagestyle{fancy}\setlength\headheight{100pt}
\fancyhead[L]{\includegraphics[width=456px]{INS_logo.png}}
\fancyhead[R]{\includegraphics[width=4.1cm]{shurp-2018-logo-blue-transparent.png}}
\renewcommand{\headrulewidth}{0pt}
\rfoot{Seite \thepage \hspace{1pt} von \pageref{LastPage}}

\geometry{a4paper, total={170mm,257mm}, left=20mm, top=10mm, }

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

Everything works fine except that the necessary logos don't appear in the header. Using the same code in a plain pdf_document format works fine.

I guess it has something to do with the book-option - does anyone know more here? I consulted https://bookdown.org/yihui/bookdown/, their github-repo as well as StackOverflow and the documentation on fancyhdr.

my sessioninfo (R Studio 1.2.1070):

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.5.1  backports_1.1.2 bookdown_0.7    rprojroot_1.3-2 htmltools_0.3.6 tools_3.5.1     yaml_2.2.0     
 [8] Rcpp_0.12.19.3  rmarkdown_1.10  knitr_1.20      xfun_0.4        digest_0.6.18   evaluate_0.12 

Thank you for any suggestions.


Solution

  • Are you sure that the images are not being pushed off the top off the page? Without your full directory, I am not able to reproduce your problem but it looks like you haven't specified a large enough top value within the geometry argument. Looking at the documentation to the geometry LaTeX function, you can see that this specifies the distance between the top of the body and the edge of the paper, leaving no space for your header.

    enter image description here

    Here is my reproducible example. It generates a dummy plot plot.png which is used for a logo placeholder

    index.Rmd:

    ---
    documentclass: book
    classoption: openany
    fontsize: 12pt
    linestretch: 1.25
    link-citations: yes
    output:
      bookdown::pdf_book:
        includes:
          in_header: preamble.tex
        latex_engine: xelatex
        toc_depth: 4
    subparagraph: yes
    ---
    
    ```{r setup, include=FALSE}
    # Create an image to attach
    png(filename = "logo.png", width = 200, height = 150, units = "px")
    plot(cars)
    box('figure', col = 'red')
    dev.off()
    ```
    
    # Title 1
    
    Text
    
    \newpage
    
    ## Section
    
    Text
    
    \newpage
    

    Within the same directory is the preamble.tex. Note the adjusted geometry argument top=50mm:

    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}
    \usepackage{geometry}
    \usepackage{titlesec}
    \usepackage{fancyhdr}
    \usepackage{fontspec}
    \setmainfont{Arial}
    
    \pagestyle{fancy}\setlength\headheight{100pt}
    \fancyhead[L]{\includegraphics[width=4cm]{logo.png}}
    \fancyhead[R]{\includegraphics[width=4.1cm]{logo.png}}
    \renewcommand{\headrulewidth}{0pt}
    \rfoot{Seite \thepage \hspace{1pt} von \pageref{LastPage}}
    
    \geometry{a4paper, total={170mm,257mm}, left=20mm, top=50mm}
    
    \titleformat{\chapter}
      {\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
    \titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
    

    enter image description here