rshinyr-markdown

How to insert a logo on left and right side of pdf title


I can insert the Rlogo.png on both sides of the page using ![](RLogo2.png) \hspace{3 in} ![](RLogo2.png) However, I can't find the way to insert the Rlogo.png right next to the pdf title. I would like the logo to be on both left and right sides of the title on the first page. Can someone help with this please. Code below:

---
title: "Shiny Installation here"
output:
  pdf_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Software

![](RLogo2.png) \hspace{3 in} ![](RLogo2.png)


Mr. Tony Moore  
West Coast Products  
5555 Club Drive  
Houston, Texas  

Subject:    Business meeting.

Dear Mr. Moore:  

This letter is in response to our meeting on July 31, 2015

## Download R packages

To download packages using R, type the following command:

## Run the shiny application

To run the application..........

See screenshot below:

enter image description here


Solution

  • Edit: I came up with a solution that handles all 3 file formats

    PDF

    If output is pdf a header.tex file is written and referenced. It uses \makeatletter and redefines the title with \def\@maketitle to include the RLogo2.png to the left and to the right. This assumes, that the file RLogo2.png lies in the same directory as your r-markdown file.

    Result pdf

    HTML

    If output is html a myheader.html file is written (Source) and referenced. It uses straight HTML to place the two images left and right from the title_ideal_text.

    Result html

    Word

    If output is docx a custom-template.docx as per this which has the images inside the header. I know this will then go over all pages but I could not find a better solution for this.

    Result word


    Code

    ---
    output:
      html_document:
        toc: yes
        includes:
           in_header: myheader.html
      pdf_document:
        toc: yes
        includes: 
          in_header: header.tex
      word_document:
        toc: yes
        reference_docx: custom-template.docx
    ---
    
    ```{r, echo=FALSE, results='asis'}
    library(htmltools)
    knitr::opts_chunk$set(echo = FALSE)
    
    output_format <- knitr::opts_knit$get("rmarkdown.pandoc.to")
    title_ideal_text <- "Shiny Installation here"
    tit <- ""
    
    if (output_format == "html") {
      fileConn <- file("myheader.html")
      writeLines(paste0('
      <div style="display: flex; align-items: center; justify-content: center; gap: 20px;">
        <img src="RLogo2.png" style="height:50px;">
        <h1>',title_ideal_text,'</h1>
        <img src="RLogo2.png" style="height:50px;">
      </div>
      '), fileConn)
      close(fileConn)
    } else if (output_format == "docx") {
      #cat('| ![](RLogo2.png){width=80px} | **Shiny Installation here** | ![](RLogo2.png){width=80px} |\n|:---:|:---:|:---:|\n\n')
      tit <- title_ideal_text
    } else {
      tit <- title_ideal_text
      fileConn <- file("header.tex")
      writeLines('
      \\makeatletter
      \\def\\@maketitle{%
        \\begin{center}%
          \\includegraphics[height=2cm]{RLogo2.png}
          \\hspace{2.5cm}  % adjust spacing between left logo and title
          {\\LARGE \\@title}
          \\hspace{2.5cm}  % adjust spacing between title and right logo
          \\includegraphics[height=2cm]{RLogo2.png}
        \\end{center}%
        \\par}%
      \\makeatother%
      ', fileConn)
      close(fileConn)
    }
    ```
    
    ---
    title: "`r tit`"
    ---
    
    ## Software
    
    Mr. Tony Moore  
    West Coast Products  
    5555 Club Drive  
    Houston, Texas  
    
    Subject:    Business meeting.
    
    Dear Mr. Moore:  
    
    This letter is in response to our meeting on July 31, 2015
    
    ## Download R packages
    
    To download packages using R, type the following command:
    
    ## Run the shiny application
    
    To run the application..........