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.
Related
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
---
I am new to Rmarkdown, Beamer presentations and the like and have been having some trouble inserting my logo to the header. I don't exactly know how the code works, Here is the code I am trying to use for my header, but it is giving me a "Undefined Control Sequence" error.
---
title: "Lifewerks Reporting Numbers 2021-2022"
date: "`r Sys.Date()`"
header-includes:
- \usepackage{subfig}
- \logo{\incudegraphics[width = 3cm]{sfp-logo.png}}
output: beamer_presentation
---
Any help or resources that I could learn from would be greatly appreciated! Thank you!!
there is a small typo, it should be \includegraphics [the l is missing]
if you want the image just on the first page, I would use \titlegraphic instead of \logo (the later would add the image to all frames)
---
title: "Lifewerks Reporting Numbers 2021-2022"
date: "`r Sys.Date()`"
header-includes:
- \titlegraphic{\includegraphics[width = 3cm]{example-image-duck}}
output:
beamer_presentation:
keep_tex: true
---
test
I'm using R markdown to make a beamer presentation.
When I try to include an error message output from R on the slide it goes off the page.
Here is my code:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output: beamer_presentation
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```
And, this is what the slides look like:
How do I keep the code on the page, either by putting it on multiple lines or reducing font size?
You can use the same technique as in Change representation (background-color & frame) of chunks in RMarkdown (with Beamer-Presentations) to redefine the verbatim environment in such a way that it uses the listings package. The listings package then allows you to both add line breaks and adjust the font size to your liking:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output:
beamer_presentation:
keep_tex: true
header-includes:
- \let\verbatim\undefined
- \let\verbatimend\undefined
- \usepackage{listings}
- \lstnewenvironment{verbatim}{\lstset{breaklines=true,basicstyle=\ttfamily\footnotesize}}{}
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```
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.
Using knitr in RStudio, I have an rmarkdown file:
---
title: "PDF knit error"
output: pdf_document
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
#Content
In RStudio, there is a nice little dropdown option where I can change the output format:
However, when I use this to change between output formats, the YAML header is changed in an undesirable way (it is also changed appropriately, as the output value changes). That is, the date field, which has an R snippet to dynamically assign the value to date, gets munged-up because the double quotes are changed to single quotes, and the single quotes are changed to a pair of single quotes.
For example, when I use this button to change the out put format to HTML, the markdown file is altered to:
---
title: "PDF knit error"
output: html_document
date: '`r format(Sys.time(), ''%d %B, %Y'')`'
---
#Content
The date changes compared:
"`r format(Sys.time(), '%d %B, %Y')`" # before another output option selected
'`r format(Sys.time(), ''%d %B, %Y'')`' # after another output option selected
This change causes the Knit to fail:
processing file: yaml-header-test.Rmd
Quitting from lines 2-7 (yaml-header-test.Rmd)
Error in base::parse(text = code, keep.source = FALSE) :
<text>:1:29: unexpected input
1: format(Sys.time(), ''%d %B, %Y'')
^
Calls: <Anonymous> ... inline_exec -> withVisible -> eval -> parse_only -> <Anonymous>
Execution halted
Is there away to prevent the switching of formats via the dropdown menu from causing edits to the date field? And, pre-emptively, yes, I recognize I can accomplish the change in format by editing the output field directly, but my question is about consequences of the GUI button.
System details:
Ubuntu 16.04
pandoc 1.17.2
R 3.3.1
RStudio 0.99.489