Search code examples
rlatexr-markdown

Adding a banner to the PDF created using RMarkdown


I am trying to use an image as a banner at the top of the PDF created by RMarkdown. I have a Tex file which uses the code taken from here. The modied Tex file (Banner.Tex) looks like

\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{tikz}
\fancyhead{%
  \begin{tikzpicture}[remember picture, overlay, outer sep=0pt, inner sep=0pt]
   \node at (0,4) {\includegraphics{ImageFromForm.png}};
     \end{tikzpicture}
}
\fancypagestyle{plain}[fancy]{}
\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{4cm}

The RMarkdown script looks like-

---
title: Test Document
output: 
  pdf_document:
    keep_tex: true
header-includes:
   - \include{Banner} 
---


    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
```{r cars}
summary(cars)
```

The ImageFromForm.png (from the Tex file) prints but there are overlaps (below). It seems like instead of stretching it to the page width, it prints it again.I don't understad the nodes in the code. Could someone please point me in the right direction? Any help is greatly recommended. Thanks. Resulting Banner


Solution

  • Two problems:

    • You shouldn't use \include before the start of the document. Use \input instead
    ---
    title: Test Document
    output: 
      pdf_document:
        keep_tex: true
    header-includes:
       - \input{Banner} 
    ---
    
    
        ## R Markdown
        
        This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    ```{r cars}
    summary(cars)
    ```
    
    • With \fancyhead{...} you set the left, middle and right header at the same time. If you want to include your image only once, choose which of them you want to modify. For example for the header in the centre:
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \usepackage{tikz}
    \fancyhead[c]{%
      \begin{tikzpicture}[remember picture, overlay, outer sep=0pt, inner sep=0pt]
       \node at (0,4) {\includegraphics{example-image-duck}};
         \end{tikzpicture}
    }
    \fancypagestyle{plain}[fancy]{}
    \renewcommand{\headrulewidth}{0pt}
    \setlength{\headheight}{4cm}
    

    enter image description here