• R Markdown Cookbook
  • Preface
    • How to read this book
    • Structure of the book
    • Software information and conventions
    • Acknowledgments
  • About the Authors
    • Yihui Xie
    • Christophe Dervieux
    • Emily Riederer
  • 1 Installation
    • 1.1 Use a Pandoc version not bundled with the RStudio IDE
    • 1.2 Install LaTeX (TinyTeX) for PDF reports
    • 1.3 Install missing LaTeX packages
  • 2 Conceptual Overview
    • 2.1 What happens when we render?
    • 2.2 R Markdown anatomy
      • 2.2.1 YAML metadata
      • 2.2.2 Narrative
      • 2.2.3 Code chunks
      • 2.2.4 Document body
    • 2.3 What can we change to change the results?
  • 3 Basics
    • 3.1 Code chunks and inline R code
    • 3.2 Write Markdown in the RStudio visual editor
    • 3.3 Render an R script to a report
    • 3.4 Convert R Markdown to R script
    • 3.5 R Markdown Notebooks
  • 4 Document Elements
    • 4.1 Insert page breaks
    • 4.2 Set the document title dynamically
    • 4.3 Access the document metadata in R code
    • 4.4 Unnumbered sections
    • 4.5 Bibliographies and citations
      • 4.5.1 Changing citation style
      • 4.5.2 Add an item to a bibliography without using it
      • 4.5.3 Add all items to the bibliography
      • 4.5.4 Include appendix after bibliography (*)
    • 4.6 Generate R package citations
    • 4.7 Cross-referencing within documents
    • 4.8 Update the date automatically
    • 4.9 Multiple authors in a document
    • 4.10 Numbered figure captions
    • 4.11 Combine words into a comma-separated phrase
    • 4.12 Preserve a large number of line breaks
    • 4.13 Convert models to equations
    • 4.14 Create an animation from multiple R plots
    • 4.15 Create diagrams
      • 4.15.1 Basic diagrams
      • 4.15.2 Adding parameters to plots
      • 4.15.3 Other packages for making diagrams
    • 4.16 Escape special characters
    • 4.17 Comment out text
    • 4.18 Omit a heading in the table of contents
    • 4.19 Put together all code in the appendix (*)
    • 4.20 Manipulate Markdown via Pandoc Lua filters (*)
  • 5 Formatting
    • 5.1 Font color
      • 5.1.1 Using an R function to write raw HTML or LaTeX code
      • 5.1.2 Using a Pandoc Lua filter (*)
    • 5.2 Indent text
    • 5.3 Control the width of text output
    • 5.4 Control the size of plots/images
    • 5.5 Figure alignment
    • 5.6 Verbatim code chunks
      • 5.6.1 Show a verbatim inline expression
    • 5.7 Line numbers for code blocks (*)
    • 5.8 Multi-column layout (*)
  • 6 LaTeX Output
    • 6.1 Add LaTeX code to the preamble
    • 6.2 Pandoc options for LaTeX output
    • 6.3 Add logo to title page
    • 6.4 Include additional LaTeX packages
      • 6.4.1 Loading LaTeX packages
      • 6.4.2 Example packages
    • 6.5 Control the placement of figures
      • 6.5.1 Floating environments
      • 6.5.2 Prevent figures from floating
      • 6.5.3 Force floats forward
      • 6.5.4 Adjust LaTeX placement rules (*)
    • 6.6 LaTeX sub-figures
    • 6.7 Render documents containing Unicode characters
    • 6.8 Generate a LaTeX fragment
    • 6.9 Add custom headers and footers (*)
    • 6.10 Use a custom Pandoc LaTeX template (*)
    • 6.11 Write raw LaTeX code
    • 6.12 For hardcore LaTeX users (*)
  • 7 HTML Output
    • 7.1 Apply custom CSS
    • 7.2 Center section headings
    • 7.3 Style code blocks and text output
    • 7.4 Scrollable code blocks (*)
    • 7.5 Fold all code blocks but show some initially
    • 7.6 Put content in tabs
    • 7.7 Embed the Rmd source file in the HTML output file
    • 7.8 Embed arbitrary files in the HTML output file
    • 7.9 Use a custom HTML template (*)
    • 7.10 Include the content of an existing HTML file (*)
    • 7.11 Add a custom browser icon
    • 7.12 Use the <details> disclosure element
    • 7.13 Sharing HTML output on the web
      • 7.13.1 R-specific services
      • 7.13.2 Static website services
    • 7.14 Improve accessibility of HTML pages
    • 7.15 For hardcore HTML users (*)
  • 8 Word
    • 8.1 Custom Word templates
    • 8.2 The two-way workflow between R Markdown and Word
    • 8.3 Style individual elements
  • 9 Multiple Output Formats
    • 9.1 LaTeX or HTML output
    • 9.2 Display HTML widgets
    • 9.3 Embed a web page
    • 9.4 Multiple figures side by side
    • 9.5 Write raw content (*)
    • 9.6 Custom blocks (*)
      • 9.6.1 Syntax
      • 9.6.2 Adding a shaded box
      • 9.6.3 Including icons
  • 10 Tables
    • 10.1 The function knitr::kable()
      • 10.1.1 Supported table formats
      • 10.1.2 Change column names
      • 10.1.3 Specify column alignment
      • 10.1.4 Add a table caption
      • 10.1.5 Format numeric columns
      • 10.1.6 Display missing values
      • 10.1.7 Escape special characters
      • 10.1.8 Multiple tables side by side
      • 10.1.9 Generate multiple tables from a for-loop (*)
      • 10.1.10 Customize LaTeX tables (*)
      • 10.1.11 Customize HTML tables (*)
    • 10.2 The kableExtra package
      • 10.2.1 Set the font size
      • 10.2.2 Style specific rows/columns
      • 10.2.3 Group rows/columns
      • 10.2.4 Scaling down wide tables in LaTeX
    • 10.3 Other packages for creating tables
  • 11 Chunk Options
    • 11.1 Use variables in chunk options
    • 11.2 Do not stop on error
    • 11.3 Multiple graphical output formats for the same plot
    • 11.4 Cache time-consuming code chunks
    • 11.5 Cache a code chunk for multiple output formats
    • 11.6 Cache large objects
    • 11.7 Hide code, text output, messages, or plots
    • 11.8 Hide everything from a chunk
    • 11.9 Collapse text output blocks into source blocks
    • 11.10 Reformat R source code
    • 11.11 Output text as raw Markdown content (*)
    • 11.12 Remove leading hashes in text output
    • 11.13 Add attributes to text output blocks (*)
    • 11.14 Post-process plots (*)
    • 11.15 High-quality graphics (*)
    • 11.16 Step-by-step plots with low-level plotting functions (*)
    • 11.17 Customize the printing of objects in chunks (*)
    • 11.18 Option hooks (*)
  • 12 Output Hooks (*)
    • 12.1 Redact source code
    • 12.2 Add line numbers to source code
    • 12.3 Scrollable text output
    • 12.4 Truncate text output
    • 12.5 Output figures in the HTML5 format
  • 13 Chunk Hooks (*)
    • 13.1 Crop plots
    • 13.2 Optimize PNG plots
    • 13.3 Report how much time each chunk takes to run
    • 13.4 Show the chunk header in the output
    • 13.5 Embed an interactive 3D plot with rgl
  • 14 Miscellaneous knitr Tricks
    • 14.1 Reuse code chunks
      • 14.1.1 Embed one chunk in another chunk (*)
      • 14.1.2 Use the same chunk label in another chunk
      • 14.1.3 Use reference labels (*)
    • 14.2 Use an object before it is created (*)
    • 14.3 Exit knitting early
    • 14.4 Generate a plot and display it elsewhere
    • 14.5 Modify a plot in a previous code chunk
    • 14.6 Save a group of chunk options and reuse them (*)
    • 14.7 Use knitr::knit_expand() to generate Rmd source
    • 14.8 Allow duplicate labels in code chunks (*)
    • 14.9 A more transparent caching mechanism
      • 14.9.1 Invalidate the cache by changing code in the expression
      • 14.9.2 Invalidate the cache by changes in global variables
      • 14.9.3 Keep multiple copies of the cache
      • 14.9.4 Comparison with knitr’s caching
  • 15 Other Languages
    • 15.1 Register a custom language engine (*)
    • 15.2 Run Python code and interact with Python
    • 15.3 Execute content conditionally via the asis engine
    • 15.4 Execute Shell scripts
    • 15.5 Visualization with D3
    • 15.6 Write the chunk content to a file via the cat engine
      • 15.6.1 Write to a CSS file
      • 15.6.2 Include LaTeX code in the preamble
      • 15.6.3 Write YAML data to a file and also display it
    • 15.7 Run SAS code
    • 15.8 Run Stata code
    • 15.9 Create graphics with Asymptote
      • 15.9.1 Generate data in R and read it in Asymptote
    • 15.10 Style HTML pages with Sass/SCSS
  • 16 Managing Projects
    • 16.1 Source external R scripts
    • 16.2 Read external scripts into a chunk
    • 16.3 Read multiple code chunks from an external script (*)
    • 16.4 Child documents (*)
    • 16.5 Keep the plot files
    • 16.6 The working directory for R code chunks
    • 16.7 R package vignettes
    • 16.8 R Markdown templates in R packages
      • 16.8.1 Template use-cases
      • 16.8.2 Template setup
    • 16.9 Write books and long-form reports with bookdown
    • 16.10 Build websites with blogdown
  • 17 Workflow
    • 17.1 Use RStudio keyboard shortcuts
    • 17.2 Spell-check R Markdown
    • 17.3 Render R Markdown with rmarkdown::render()
    • 17.4 Parameterized reports
    • 17.5 Customize the Knit button (*)
    • 17.6 Collaborate on Rmd documents through Google Drive with trackdown
      • 17.6.1 The trackdown Workflow
    • 17.7 Organize an R Markdown project into a research website with workflowr
    • 17.8 Send emails based on R Markdown
  • References
  • Published with bookdown

R Markdown Cookbook

6.3 Add logo to title page

We can use the titling LaTeX package to alter our title block to include an image. Below is a full example that shows how to add the R logo (logo.jpg) to the title page. The image can be of any format that LaTeX supports (e.g., jpg, png, or pdf).

---
title: Adding a Logo to LaTeX Title
author: Michael Harper
date: December 7th, 2018
output: pdf_document
header-includes:
  - \usepackage{titling}
  - \pretitle{\begin{center}
    \includegraphics[width=2in,height=2in]{logo.jpg}\LARGE\\}
  - \posttitle{\end{center}}
---

<!-- Optionally include a page break. This will force the start
of the document to the second page -->

\newpage

This is your report.

```{r, include=FALSE}
# copy the R logo to the current directory
file.copy(file.path(R.home("doc"), "html", "logo.jpg"), '.')
```

An example output is shown in Figure 6.1.

A logo on a LaTeX title page.

FIGURE 6.1: A logo on a LaTeX title page.

An alternative method that does not require a special LaTeX package (titling) is to just insert the image in the title field using the Markdown syntax. For example:

title: |
  ![](logo.jpg){width=1in}  
  Adding a Logo to LaTeX Title

In this case, you will not need the header-includes field in the YAML frontmatter in the first example. Please note that although you cannot see them, there are two trailing spaces after ![](logo.jpg){width=1in}, which means a line break in Markdown (see Section 4.12). Without the line break, the image and the title would be on the same line, which may not be what you desire.