R Markdown - remove left-sided header in pdf output - r

I am trying to create an automated report in rmarkdown for business partners and I am a bit stuck on how to set up the headers using the Latex package fancyhdr.
The report includes a table of contents set to level 1 headers, but there are sub-sections in the report that are created using level 2 headers.
When I generate the PDF the fancy header has the level 1 header on the right header (which is fine), the message I set in the central header (also fine), and the sub-section (level 2 header) in the left header.
I would like to remove this, but the documentation is very vague as to how to do so - I've just spent a considerable amount of fruitless time trying to get this to work.
Here are the YAML settings at the top of the R Markdown document:
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \setmainfont{Gotham Book}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
---

These are the "left-odd" and "right-even" headers. If you set them to nothing using \fancyhead[LO,RE]{} they will go away:
---
title: "Report Title"
author: "Authors"
date: 'Date'
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 1
header-includes:
- \usepackage{fontspec}
- \usepackage{booktabs}
- \usepackage[tocflat]{tocstyle}
- \usetocstyle{standard}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Center Message}
- \fancyfoot[CO,CE]{Footer Message}
- \fancyfoot[LE,RO]{\thepage}
- \fancyhead[LO,RE]{}
- \usepackage{blindtext}
---
\blinddocument
(I have removed your font and inserted some sample text to show the effect.)

Related

How to show the total number of pages in a PDF via the RMarkdown (i.e. display "Page 1 of 10")?

Given the code below, which currently outputs the page number on top of the page, I would like it to output the PDF page count as in Page 1 of 3. I have done a lot of searching but haven't found any way to include the total page number.
Question Is this possible to do w/ in R Markdown ? If not, are there any workarounds ?
I have googled this and haven't found anything obvious such as YAML or TeX solutions, however, I may be missing something or not searching for the correct things.
RMarkdown Code
---
title: "R Markdown Example With Numbered Sections"
output:
bookdown::pdf_document2:
toc: true
toc_depth: 6
number_sections: true
includes:
in_header: header.tex
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[RO,RE]{\thepage}
- \fancyhead[LO,LE]{Header Message}
- \fancyfoot[LE,LO]{Footer Messge on the Left}
- \fancyfoot[LE,RO]{Footer Messge on the Right}
---
\thispagestyle{fancy}
# Example R Rarkdown : Numbered Sections
## R Markdown
### Description
Some description text
\newpage
#### Details
Details go here.
\newpage
## Plots
Plots go here
\newpage
Last page
Current header
Desired Header
Make sure you have the lastpage package installed.
Then modify your header includes to look like this:
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \pagestyle{fancy}
- \fancyhead[RO,RE]{\thepage\ of \pageref{LastPage}}
- \fancyhead[LO,LE]{Header Message}
- \fancyfoot[LE,LO]{Footer Messge on the Left}
- \fancyfoot[LE,RO]{Footer Messge on the Right}

How to add coloring to PDF reports in rmarkdown?

How to add background colors to specific section of the text while creating PDF reports using rmarkdown. Something like below
i tried something like this but it is working in latex but not in rmarkdown
\titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-0.5ex]{\linewidth}{3ex}\vspace{-3ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}
this is how it looks in latex
this is in rmarkdown
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
- \usepackage[table, svgnames]{xcolor}
- \usepackage{titlesec}
- \usepackage{sectsty}
- \usepackage{xcolor, soul}
- \sectionfont{\color{red}}
- \subsectionfont{\color{green}}
- \subsubsectionfont{\color{blue}}
- \titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-0.5ex]{\linewidth}{3ex}\vspace{-3ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}
---
\section{Highlights}
# Section
Two problems:
rmarkdown already loads xcolor without paying attention to the options you need. As a workaround you could trick markdown by passing the necessary options to all packages
sectsty will destroy all changes you do via titlesec. Don't load them both
---
title: "Untitled"
output:
pdf_document:
keep_tex: true
classoption: svgnames
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
- \usepackage{titlesec}
- \usepackage{soul}
- \titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-1.5ex]{\linewidth}{3ex}\vspace{-3.5ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}
---
\section{Highlights}
# Section

Reduce top margin when creating PDF from Rmarkdown using knitr

I'm creating a PDF document using knitr and Rmarkdown but the logo-and-title are far from the top of the document.
How to reduce the top margin on the first page ? Many thanks in advance.
This is my code:
---
title: "Title come here (there is a logo before the title)"
output:
pdf_document:
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}
- \includegraphics[width=1.5in,height=1.5in]{logo.png}\LARGE\\}
---
I found a solution that works (not sure if it's a "clean" solution, but it does the job): I added a negative value in - \setlength{\droptitle}{-1.1in}
---
title: "\\vspace{0.5in} Title here"
output:
pdf_document:
header-includes:
- \usepackage{titling}
- \setlength{\droptitle}{-1.1in}
- \pretitle{\begin{center}
- \includegraphics[height=1.5in]{logo.png}\LARGE\\}
---

How to change bibliography style in Rmarkdown?

I am trying to change the style of my bibliography in Rmarkdown. I followed this solution but nothing happens in my case.
This is my code:
---
title: "Tilte"
author:
- name: Rollo99
abstract: |
Abstract
keywords: whatever
date: "`r Sys.Date()`"
citation_package: natbib
bibliography: library.bib
biblio-style: abbrvdin
references:
link-citations: yes
linkcolor: blue
output:
rticles::elsevier_article:
number_sections: yes
header-includes:
- \setcitestyle{numbers}
- \usepackage{amsmath}
- \usepackage{hyperref}
- \usepackage{graphicx}
- \usepackage[section]{placeins}
- \usepackage{float}
- \usepackage[font=small, skip=1pt]{caption}
- \usepackage[labelfont=bf]{caption}
- \usepackage{amssymb}
- \usepackage{inputenc}
- \usepackage{placeins}
- \usepackage{blindtext}
- \usepackage{setspace}
- \usepackage[top=3cm, bottom=3cm, left=3cm, right=3cm]{geometry}
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{mathrsfs}
- \usepackage{longtable}
- \usepackage{graphicx}
- \usepackage{ragged2e}
- \usepackage{csquotes}
- \usepackage[bottom]{footmisc}
- \usepackage{xpatch}
- \setlength\parindent{0pt}
- \usepackage{appendix}
I downloaded and saved abbrvdin.bst in the same repository as the bibliography from here.
What am I doing wrong?
Thanks
The elsevier template does not support this, see the code on github.
You can just use plainmat or elsevier-harv.
A solution is to clone the repository and manipulate the file to your need. In addition to that you can raise a pull request.

2 Logo in R Markdown on PDF

I am using below code to add a company logo in r markdown in a pdf version and
the output is fine.
I need to add 2 logo's on each page - one for my client and other for our
company.
Is is possible to add 2 logo's in a pdf using r markdown.
---
title: "Complaints Text Model"
author: "ANALYTICS TEAM"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
output:
pdf_document:
toc: yes
header-includes:
- \usepackage{fancyhdr}
always_allow_html: yes
---
\addtolength{\headheight}{1.0cm}
\pagestyle{fancyplain}
\rhead{\includegraphics[height=1.2cm]{D:/D Drive Data/Shivi/R Project/my company.png}}
\renewcommand{\headrulewidth}{0pt}
One can be on one side of the page and other logo on the other side.
All you need to do is imitate your syntax for the left header:
\rhead{\includegraphics[height=1.2cm]{D:/D Drive Data/Shivi/R Project/my company.png}}
\lhead{\includegraphics[height=1.2cm]{D:/D Drive Data/Shivi/R Project/client.png}}

Resources