Creating a footer for every page using R markdown - r-markdown

I'm writing a document in R Markdown and I'd like it to include a footer on every page when I knit a PDF document. Does anyone have any idea on how to do this?

Yes, this question has been asked and answered here: Adding headers and footers using Pandoc. You just need to sneak a little LaTeX into the YAML header of your markdown document.
This markdown header does the trick:
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \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
---
Works for me with an Rmd file in RStudio Version 0.98.1030 for Windows.

Another option would be to use the argument includes provided by rmarkdown::pdf_document() (documentation). This allows you to keep the footer in a separate file. If your footer is defined in footer.tex, the header of your R Markdown file would look like this:
---
output:
pdf_document:
includes:
after_body: footer.tex
---
This also assumes that footer.tex is in the same directory as the R Markdown file.
Update: The file footer.tex can contain any valid LaTeX that you want to be inserted at the end of your PDF document. For example, footer.tex could contain the following:
This \textbf{text} will appear at the end of the document.

To manage the height of the footer, you can use the following:
date: '`r paste("Date:",Sys.Date())`'
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \setlength{\footskip}{-50pt} # set the footer size
Keep Coding!

Related

How do I knit to two formats simulateously with Quarto?

Is it possible to create multiple output formats from a Quarto/R Markdown document at once by specifying them in the YAML header?
My guess did not work:
---
title: Stacked Area chart with Annotations
format:
- html
- gfm
---
Try this way (and suppose file name is format_test.qmd)
format_test.qmd
---
title: "Untitled"
format:
html: default
gfm: default
---
## Quarto
Quarto enables you to weave together content and executable code
into a finished document. To learn more about Quarto
see <https://quarto.org>.
## Running Code
When you click the **Render** button a document will
be generated that includes both content and the output
of embedded code. You can embed code like this:
```{r}
1 + 1
```
Then from console run this following code to generate both html and github flavoured markdown (gfm) file at once.
# install.packages("quarto")
quarto::quarto_render("format_test.qmd", output_format = "all")
Or if you prefer using the terminal (or if you are a non R-useR) you can run the following command in the terminal,
quarto render format_test.qmd --to gfm,html
Be careful to specify multiple formats without putting any space in between i.e. type them as gfm,html.
And running the above command in the terminal does not require you to specify format yaml option in your document. So, for
format_test.qmd
---
title: "Untitled"
---
## Quarto
Quarto enables you to weave together content and executable code
into a finished document. To learn more about Quarto
see <https://quarto.org>.
quarto render format_test.qmd --to pdf,html,gfm
will generate three files in HTML, pdf, and markdown format.

Issues with different citation packages in bookdown

I am creating a pdf-book using bookdown, with a .bib file that holds my citations. I call the citations with the standard [#citation] used in bookdown. Citing works correctly, but the bibliography doesn't seem to be how I wanted it.
Here are the things that I need to work:
The bibliography should be included in the toc, at the end of \mainmatter, but before the \backmatter, which I use to attach pdf's as an appendix, where the appendix header is listed in the toc
The in-text citations should be in number format, with possible nested citations, like this: [1,2]
Bibliography should be in the order they appear in the text, with the possibility of removing DOI/URLs etc as I see fit
For these things to work so far, I've tried the natbib package, which seem to work fine. However, I do not seem to be able to remove DOI/URLs from specific bibliography entries, such as journal articles. Therefore, I tried using biblatex or the built-in pandoc, but neither seem to work properly (With pandoc the bibliography is gone from the toc, and the bibliography entries are messy. With biblatex, i get multiple errors that the program can't find specific entries in the .bib file, which are there)
This is my YAML with natbib:
site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
bookdown::pdf_book:
latex_engine: xelatex
fig_caption: yes
toc: false
citation_package: natbib
includes:
before_body: frontpage.tex
after_body: after_body.tex
in_header: preamble.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib, libraryzotero.bib]
link-citations: yes
---
And this is in my preamble.tex:
\usepackage{titlesec}
\usepackage{pdfpages}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{} % set title format
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\AtBeginDocument{\let\maketitle\relax}
\usepackage{makeidx}
\makeindex
\setcitestyle{numbers,square,comma}
\usepackage{url}
\usepackage[nottoc]{tocbibind}
\usepackage{caption}
\captionsetup[figure]{textfont={small,it}, labelfont={normalsize,bf,it}} % set figure caption font size and style
\usepackage{graphicx}
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
\raggedbottom
\usepackage{fancyhdr,blindtext}
\fancyhf{}
\fancyhead[LO]{\slshape \rightmark} %section
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\slshape \leftmark} % chapter
\fancyhead[LE]{\thepage}
\setlength{\headheight}{27.7pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{% % change settings for including headers
\if#mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
\makeatother
\usepackage{epigraph}
\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}
Where \setcitestyle{numbers, square, comma} is for the in-text references.
Does anyone have any suggestions how to get these things to work with bookdown?
For the natbib based solution you could patch the used bibliography style:
You are not setting any bibliography style. Therefore plainnat.bst is used.
Copy plainnat.bst from your TeX installation (typically TEXMF/bibtex/bst/natbib/) to your working directory with a new name.
Add biblio-style: <new-name> to your YAML header.
Edit your BST file:
Find FUNCTION {article}.
Within that block remove format.doi output and format.url output.
Repeat for other types.

How to pass dynamic parameters from YAML to the header which appears in every page of word document?

I want to have a dynamic header to appear on every page of word document after knitting an r markdown file. The header includes the document title and ID that are the user inputs passed to r markdown via r shiny app.
I followed the link https://vimeo.com/110804387 to create header for word template Template.docx. The header appears on every page but the parameters from YAML can't be passed to the header.
The header in the Template.docx:
Document title: `r params$title`
Document ID: `r params$id`
The YAML in r markdown:
output:
word_document:
reference_docx: Template.docx
params:
title: title
id: id
The word template image:
Image of the Template.docx
Do you know how I can get the parameters passed to the header? I really appreciate your help. I'm learning R and working on this first project at the same time. I'm using RStudio.
try adding quotes to `r ` --> "`r `"

Self-contained rmarkdown file contains only some embedded images

I'm seeing different behaviour in an R Markdown (Rmd) file depending on how I reference the image I'd like to embed in an HTML document. In the simple example below, the second image is embedded in the document, but the first (using the R chunk) is not.
---
title: "title"
output:
html_document:
mode: selfcontained
theme: null
---
```{r packages, echo=FALSE}
library(htmltools)
```
```{r imgTest, echo=FALSE}
img(src = "http://placehold.it/350x150")
```
<img src="http://placehold.it/350x150">
This is the output in the HTML (for the relevant bit):
<p><img src="http://placehold.it/350x150"/></p>
<p><img src="data:image/png;base64,iVBORw0KG<SNIPPED>"></p>
In summary, using the htmltools function img() within a code chunk does not embed the image but instead it remains a link.
For various reasons, I need the document to be truly self-contained (no links) and to also avoid raw HTML.
Can anyone explain why this is happening and offer a solution? I've tried a variety of chunk options without success so far.

get rid of captions when using texreg in Rmarkdown

How do you suppress captions for a texreg table? I'm using Rmarkdown to generate a LaTeX file. Here's a simple example:
```{r, echo=FALSE, message=FALSE, results="asis"}
library(texreg)
data <- data.frame(a=c(1,2,3,4), b=c(6,3,4,4))
texreg(lm(a~b, data=data), caption="", custom.note="", float.pos="h!")
```
The table I get has a caption on the bottom that says "Table 1:". How do I get rid of it? Thanks.
In the YAML section where LaTeX packages can be included, add the caption package:
header-includes:
- \usepackage{caption}
Then at the beginning of the RMarkdown document body add:
\captionsetup[table]{labelformat=empty}
This removes the caption labels for all tables.
In case you have an external .tex file for additional formatting (i. e. I made a file for including a logo in the header of each page as described here) you should include \usepackage{caption} (just this line) in the external .tex file, not in the yaml header of the RMarkdown document.