RMarkdown and YAML not able to load packages - r

I have a text that I want to convert from latex to Markdown for various reasons. But I'm not able to make it work. If I load all the packages and only type "yo" as text, it's returning me an error.
Error in yaml::yaml.load(enc2utf8(string), ...) :
Scanner error: while scanning a directive at line 5, column 1could not find expected directive name at line 5, column 2
Calls: <Anonymous> ... yaml_load_utf8 -> mark_utf8 -> <Anonymous> -> .Call
Execution halted
Many people had this error before but I haven't saw somebody encountering this error so far and a solution for packages like this.
Can we write comments "%" in the preamble?
How can I add my latex packages in RMarkdown?
Will it work if I export it in word?
Here is my code:
---
title: "rest"
author: "Me"
date: '2016-10-26'
header-includes:
% Titles
- \usepackage{titlesec}
% figure
- \usepackage{color}
- \usepackage{graphicx} % Enhanced support for graphics
- \usepackage{subfig} % subfigure environment
- \usepackage{float} % Improved interface for floating objects
- \usepackage{rotating} % Rotation tools, including rotated full-page floats
% \usepackage[labelfont=bf]{caption} % Customising captions in floating environments
% tables
- \usepackage[table]{xcolor} % Driver-independent color extensions
- \usepackage{array} % Extending the array and tabular environments
- \usepackage{longtable} % Allow tables to flow over page boundaries
- \usepackage{multirow} % Create tabular cells spanning multiple rows
% math
- \usepackage{amssymb} % some weird math symbols
- \usepackage{amsthm}
- \usepackage{amsmath} % AMS mathematical facilities for LATEX
% documents aspect
- \usepackage{lineno} % Line numbers on paragraphs
- \usepackage[top=2.2cm, left=2.2cm,right=2.2cm,bottom=4.2cm]{geometry} % Flexible and complete interface to document d=imensions
% see geometry.pdf on how to lay out the page. There's lots.
- \usepackage[utf8]{inputenc} % Accept different input encodings
- \usepackage[T1]{fontenc}
- \usepackage{lscape} % Place selected parts of a document in landscape
- \usepackage{babel}
% reference in text
- \usepackage[unicode=true, pdfusetitle, bookmarks=true, bookmarksnumbered=false, bookmarksopen=false, breaklinks=true, pdfborder={0 0 0}, backref=page,colorlinks=true]
{hyperref}
- \newcommand*{\fullref}[1]{\hyperref[{#1}]{\autoref*{#1} \nameref*{#1}}}
% bibliography
%\usepackage[style=authoryear, backend=bibtex,bibencoding=ascii]{biblatex} % Bibliographies in LATEX using BibTEX for sorting only
- \usepackage{natbib} % Flexible bibliography support
% \usepackage{translation-natbib-fr} %French translation of the documentation of natbib
%\renewbibmacro{in:}{}
%\bibliography{yo.bib}
% \ref{} makes reference to a \label{} set in a table or figure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
- \theoremstyle{plain}
title: "data"
author: "Me"
date: '2016-10-26'
output:
pdf_document: default
word_document: default
documentclass: article % - \documentclass[english]{scrartcl
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
YO

Your header contains invalid YAML, inline comments need #:
---
title: "rest"
author: "Me"
date: '2016-10-26'
header-includes:
# Titles
- \usepackage{titlesec}
# figure
- \usepackage{color}
- \usepackage{graphicx}
...
You'll also need to de-duplicate your YAML map keys. Sure, you can render RMarkdown to Word with the obvious caveat that LaTeX-specific formatting won't automagically carry over.

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}
---

I am getting error "command \bibname unspecified" while compiling a latex document in R markdown

I am trying to create an R Markdown Template on the basis of a LaTeX template I found on Overleaf. The template preamble uses many packages. I am trying to compile this latex file in RStudio using tinytex latex compiler, and during this process, I am getting error "Latex Error: Command \bibname unspecified. I am attaching both, the preamble from overleaf, and the YAML specification from R Markdown.
Overleaf Preamble
\documentclass[twoside]{report} %[twoside, 14pt]
% ==== MY BUNCH OF PACKAGES ====
\usepackage[a4paper,top=3cm,bottom=3cm,left=2.5cm,right=2.5cm,bindingoffset=5mm]{geometry}
\usepackage[english]{babel}
\usepackage{textcomp}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{multirow}
\usepackage{float}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{caption}
\captionsetup{font=footnotesize}
\usepackage{subcaption}
\usepackage{tabularx}
%\usepackage{mathabx}
\usepackage{subfiles}
\newcommand\quoteref[1]{\csname#1\endcsname}%
\usepackage{glossaries}
\usepackage[toc,page]{appendix}
\usepackage{rotating}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{changepage} % for the adjust width environment
\usepackage{setspace}
% ----------- code environment settings
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
% ---------------- chapter abstracts
\newenvironment{chapabstract}
{
\begin{center}
\noindent\makebox[0.8\linewidth]{\rule{0.8\paperwidth}{0.4pt}}
\end{center}
\vspace{2mm}
\list{}{
\setlength{\leftmargin}{1cm}
\setlength{\rightmargin}{\leftmargin}
}
\item\relax
}
{\par}
\makeatother
% ------------- hyperref
%\biboptions{authoryear,square,sort&compress}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue]{hyperref}
% -------------- journals shortcuts
\newcommand{\aaa}{Anonymous Ambiguous Abstracts}
\newcommand{\bbb}{Boring Bibliographic Book}
% ----------- glossary entries for summary of acronyms
\makenoidxglossaries
\newglossaryentry{AAA}{name=AAA, description={Ambitious Amicable Ants}}
\newglossaryentry{BBB}{name=BBBB, description={Babbling Bumbling Band of Baboons}}
\newglossaryentry{CCC}{name=CCC, description={Condescending Critical Cucumber}}
\newglossaryentry{BFG}{name=BFG, description={Big Friendly Giant}}
% ==== TEMPLATE PACKAGES ====
\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc} % use 8-bit T1 fonts
\usepackage{lmodern}
\usepackage{hyperref} % hyperlinks
\usepackage{url} % simple URL typesetting
\usepackage{booktabs} % professional-quality tables
\usepackage{amsfonts} % blackboard math symbols
\usepackage{nicefrac} % compact symbols for 1/2, etc.
\usepackage{microtype} % microtypography
\usepackage{lipsum} % fill with latin nonsense
YAML Specification in R Markdown
title: "Ambra2"
author: "Maulik Bhatt"
date: "`r Sys.Date()`"
output:
bookdown::pdf_document2:
template: Ambra.tex
keep_tex: true
Bibliography: references.bib
What am I doing wrong here?

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

Harvard citation format in R markdown

I am using R markdown for a document, however I just have to do some minor tweaks to my citations.
Firstly, is there a package which will put my BibTeX references in Harvard format?
And secondly, how can I get the author's surname and the year to be seperated by a comma within my in-text citations?
Here is one of the references in my BibTex file
#article{schwarz1978estimating,
title={Estimating the dimension of a model},
author={Schwarz, Gideon and others},
journal={The annals of statistics},
volume={6},
number={2},
pages={461--464},
year={1978},
publisher={Institute of Mathematical Statistics}
}
When I in-text reference this, it generates (Schwarz 1978). I would like a comma to seperate the surname and the year.
I've also added my YAML if that is needed
### Adding additional latex packages:
header-includes:
- \usepackage{rotating, caption} \DeclareMathOperator*{\argmin}{argmin}
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xcolor}
output:
pdf_document:
keep_tex: yes
template: Tex/TexDefault.txt
fig_width: 3.5 # Adjust default figure sizes. This can also be done in the chunks of the text.
fig_height: 3.5
I rely on the standard (and very powerful) natbib package which many Rmd styles already add, and which you can generally add to whichever RMarkdown template you use (and you didn't say).
Quick example using my pinp package:
---
title: Example
author:
- name: Dirk Eddelbuettel
affiliation: a
address:
- code: a
address: Planet Earth
papersize: letter
fontsize: 12pt
one_column: true
bibliography: ref
output: pinp::pinp
---
# Introduction
Two quick examples using natbib
- citet: \citet{schwarz1978estimating}
- citep: \citep{schwarz1978estimating}
For the rest, see the natbib documentation, either at source or contributed
such as https://gking.harvard.edu/files/natnotes2.pdf.
The body of that document comes out as follows:

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.

Resources