Harvard citation format in R markdown - r

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:

Related

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

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.

Why Rmarkdown Dynamic TITLE in YAML does not appear in the pdf file

I am trying to generate dynamic PDF reports from Rmarkdown with dynamic titles. To do this I use the function rmarkdown::render() in my R script and I pass in the argument params the title parametre as set.title = title.
The YAML of my Rmarkdown script I have the next code:
author: "Author test"
date: "`r Sys.setlocale('LC_TIME','C');format(Sys.Date(),'%B %d, %Y')`"
output:
pdf_document:
toc: yes
toc_depth: 5
keep_tex: yes
html_document:
theme: united
toc: yes
classoption: table
header-includes:
- \usepackage{array}
- \usepackage{float}
- \usepackage{xcolor}
- \usepackage{caption}
- \usepackage{longtable}
#- \usepackage{mulicol}
params:
set.title: title
title: "`r params$set.title`"
Once I run my code everything goes well and generate the different pdfs that I expect, the problem is that neither of these PDFs show the title, author and date. I am working with version 1.8 of rmarkdown package.
Can anyone help me with this issue?
For closing the conversation of this question I write how has been solved this question.
At the beginning I was thinking this issue was related with the code in YAML (where the title was informed from parameters) but eventually I discovered that there was nothing to do that. The problem was that I was using a the function cat() in the varible title that I pass as a parametre and this function changes the value of variable title into NULL.
For knowing exactly all the explanation about how to write dynamic titles in rmarkdown documents redirect yourself to the next entrance: Setting document title in Rmarkdown from parameters

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 and YAML not able to load packages

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.

Resources