R Markdown Knitting header with JPG issue - r

I am new to markdown and have to generate a weekly report with the proper header including a jpg image. I have the following yaml
---
title: "My Title"
author: "Author Name"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \pretitle{\begin{center}
\includegraphics[width=2in]{picture.jpg}}
- \posttitle{\end{center}}
mainfont: Calibri
output:
pdf_document:
latex_engine: xelatex
---
Company policy states that all documents have to be in Calibri(I know) which was the main issue prior to this.
When I go to knit I get this error
Error in yaml::yaml.load(..., eval.expr = TRUE) :
Scanner error: while scanning a simple key at line 11, column 5 could not find expected ':' at line 12, column 5
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted
Any help on a resolution is much appreciated.

The first error Error in yaml::yaml.load(..., eval.expr = TRUE) : Scanner error: while scanning a simple key at line 11, column 5 could not find expected ':' at line 12, column 5 (as described in the question) originates from the fact that you are missing a - in line 11, so that line is considered as a key of YAML and since this key is missing a :, hence the error.
And after adding -, you are having another error ! Undefined control sequence. l.68 \pretitle {\begin{center} Error: LaTeX failed to compile (as said in the comment), because the command \pretitle is not recognised here. The command \pretitle (and also \postitle) comes from the latex package titling which is not specified here, hence the error.
Solution
Simply add the package titling using \usepackage in header-includes.
---
title: "My Title"
author: "Author Name"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \usepackage{titling}
- \pagestyle{fancy}
- \pretitle{\begin{center}
- \includegraphics[width=2in]{picture.jpg}}}
- \posttitle{\end{center}}
mainfont: Calibri
output:
pdf_document:
latex_engine: xelatex
---

Related

How to add logo to top right of PDF document

I am making a pdf document in which I want two logos in the top right of the page. I found great examples of using the latex 'fancy' package. When I replicate the example I receive this error ! LaTeX Error: File 'fancyhdr.sty' not found. I am new to latex and have only used in Rstudio when knitting documents. How do I add this file or how do I install the latex package 'fancy'. I am using a 2017 macbook pro and making this document in R Studio.
---
title: "Test"
date: "Updated: `r format(Sys.time(), '%Y-%m-%d')`"
output: pdf_document
urlcolor: blue
indent: true
header-includes:
- \usepackage{indentfirst}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[R]{\includegraphics[width=2cm]{ribbitr.png}}
- \fancyhead[L]{\includegraphics[width=2cm]{nsf_logo.png}}
- \renewcommand{\headrulewidth}{0.2pt}
- \renewcommand{\footrulewidth}{0.2pt}
---

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

Using YAML set parameters IN YAML header Knitr with parameters

I declare my parameter Report_Date in my YAML header in R Markdown. I want to use the Report_Date parameter as a forced header in header-includes the line after it. No matter which characters I use I can't seem to reference that parameter.
I've tried any variation I could think of and followed all the other SO I've found. Even though some have almost identical questions, the answers don't work for me, or I'm doing something else wrong.
- \fancyhead[LO,LE]{"'r params$Report_Date'"}
- \fancyhead[LO,LE]{r params$Report_Date}
- \fancyhead[LO,LE]{\"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\r params$Report_Date'}
- \fancyhead[LO,LE]{'`r params$Report_Date`'}
- \fancyhead[LO,LE]{'r params$Report_Date'}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\$r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$'r params$Report_Date'$}
- \fancyhead[LO,LE]{"r params$Report_Date"}
even tried:
includes:
in_header:'`r params$Report_Date`'
as told in: YAML current date in rmarkdown
This is my current YAML R Markdown code (it is NOT in a separate template file, just in my regular .rmd file that I want to create)
---
title: "Monthly Diagnostic Report"
author: "Morgan :)"
date: "July 12, 2019"
toc: true
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Monthly Diagnostic Report}
- \fancyhead[LO,LE]{"'r params$Report_Date'"}
- \fancyfoot[RE,RO]{\thepage}
output: pdf_document
---
Preferably I'd have a left header that evaluates paste0("Report Created for: ", params$Report_Date) and a footer that evaluates paste0("Report Created on: ", format(Sys.time(), "%d %B, %Y")).
But for now I'd settle for just a header contains the Report_Date parameter.
Error messages include:
! Missing $ inserted.
<inserted text>
You may need to add $ $ around a certain inline R expression `r `
the only syntax that would make sense to me is
---
output: pdf_document
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[LO,LE]{`r params$Report_Date`}
---
If I use that I get a different error message about params being unknown. This does make sense, since params is defined only after the YAML header has been parsed. Fortunately one can use \fancyhead and \fancyfoot also in the body of your document (after \begin{document} in LaTeX speak). Hence the following should give you the desired output:
---
output: pdf_document
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Monthly Diagnostic Report}
- \fancyfoot[RE,RO]{\thepage}
---
\fancyhead[LO,LE]{Report created for: `r params$Report_Date`}
\fancyfoot[CO,CE]{Report created on: `r format(Sys.time(), '%d %B, %Y')`}
<actual content>
Note: I am setting the creation date in the body as well, since it is difficult to get a literal colon into the YAML header.

R Markdown - remove left-sided header in pdf output

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.)

Rmarkdown knitting YAML LaTeX error

I'm trying to have a script knit an Rmd file to a PDF document and faced some errors.
I must say, document knits fine if I click on the knit button in Rstudio but it fails to knit if I use the command:
rmarkdown::render('diagnostic.Rmd',output_format = "pdf_document",
output_file = "diag.pdf")
The first error I get comes from this part of my code:
---
author: Zigo_datateam
date: "`r format(Sys.time(), '%d %B, %Y')`"
geometry: margin=1.6in
header-includes: "\\usepackage{fancyhdr} \\fancyfoot[CO,CE]{My footer}"
output:
pdf_document:
toc: false
title: "Graphic Layout(Report)"
---
and is:
Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning a simple key at line 4, column 1could
not find expected ':' at line 5, column 1
So, if I remove the hole header-includesexpression it seems to start knitting but then I get a second error from this part (which comes after the second "---"):
\addtolength{\headheight}{1.0cm}
\pagestyle{fancyplain}
\rhead{\includegraphics[height=1.2cm]{logo.png}}
\renewcommand{\headrulewidth}{0pt}
ERROR:
! Undefined control sequence.
<argument> \undefinedpagestyle
l.130 ...headheight}{1.0cm} \pagestyle{fancyplain}
How can I get rid of the first error without getting the second one?
Update: I decided to move all footer/header styles to a separate file and my code looks like this:
---
author: Zigo_datateam
date: "`r format(Sys.time(), '%d %B, %Y')`"
geometry: margin=1.6in
output:
pdf_document:
includes:
in_header: mystyles.sty
title: "Graphic Layout(Report)"
---
The errors I talked about earlier stoped showing but I still get this:
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
In your YAML, use
header-includes:
- \usepackage{fancyhdr}
- \fancyfoot[CO,CE]{My footer}
And it should work.

Resources