Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R Markdown, create header/footer on every page regardless of output type (pdf, html, docx)

I'd like to add to the question Creating a footer for every page (including first!) using R markdown. The code there (also, below) works perfectly fine for me when knitting to pdf. But I won't get header/footers for html or docx output.

In R Markdown, what can I do to generate header/footers for every page of an output doc regardless of the type of output doc?

---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
\lipsum[1-30]
like image 214
lowndrul Avatar asked Sep 03 '17 19:09

lowndrul


People also ask

How do you add a header and footer in Rmarkdown?

6.9 Add custom headers and footers (*) The syntax for the formatting is \fancyhead[selectors]{output text} , whereby the selectors state the part of the header that we wish to customize. We can use the following selectors for the page locators: E for even pages. O for odd pages.

How do you add a header in R markdown?

Creating Headings and Subheadings We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.

How do I change R markdown from HTML to PDF?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do you markdown a PDF in R?

Click File, New File, and choose R Markdown. A window will open and you will need to input a title (you can change it later so don't worry you're stuck with it), you can choose what kind of document to make as well as the type of output file (PDF, Word or html). For this class, we will create Documents of PDF or Word.


1 Answers

You can add YAML instructions for headers and footers in html and Word versions of the document. Below is what the YAML looks like. Explanations follow.

---
title: "Test"
author: "Author Name"
output:
  html_document:
    include:
      before_body: header.html
      after_body: footer.html
  pdf_document: 
  word_document: 
   reference_docx: template.docx
header-includes:
  - \usepackage{fancyhdr}
  - \usepackage{lipsum}
  - \pagestyle{fancy}
  - \fancyhead[CO,CE]{This is fancy header}
  - \fancyfoot[CO,CE]{And this is a fancy footer}
  - \fancyfoot[LE,RO]{\thepage}
---

html header and footer

As shown in the YAML above, for html output you can specify the header and footer in separate html files using the before_body: and after_body: tags. For example, to get a header followed by a rule line, the header.html file could look like this:

This is a header
<hr>

Word header and footer

Yihui Xie, author of knitr, explains how to do this here (also see this SO answer). You create a Word file with the styles you want and then save that file in the local directory (or you can provide a path to the file if it's in another directory). Then you use the reference_docx: YAML tag to point knitr to that document. I just opened a new Word file and added a header and footer and then saved the file as template.docx in the local directory.

like image 127
eipi10 Avatar answered Sep 19 '22 15:09

eipi10