R Markdown with Tex Header - r

I have created a LaTeX header file for assignments to make my R Markdown convert to PDF as desired. The header file works perfectly but has a couple of defined commands to give my name, assignment number/name and the class name.
\newcommand{\hmwkAuthorName}{Joe Bloggs}
\newcommand{\hmwkClass}{CompSci 101}
\newcommand{\hmwkTitle}{Assignment Numero Uno}
I want to be able to define these in the YAML section of my R Markdown but always getting "Undefined Control Sequence" and error 43 when converting which is remedied by putting these sections back in the header file.
This is what the YAML section looks like currently (not working):
---
title: "Assignment Numero Uno"
author: "Joe Bloggs"
header-includes:
- \newcommand{\hmwkAuthorName}{Joe Bloggs}
- \newcommand{\hmwkClass}{CompSci 101}
- \newcommand{\hmwkTitle}{Assignment Numero Uno}
output:
pdf_document:
includes:
in_header: header_temp.tex
---
How can I achieve this?
EDIT: The full header file is
\usepackage{fancyhdr}
\usepackage{lastpage}
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in
\linespread{1.1}
\pagestyle{fancy}
\lfoot{\hmwkAuthorName}
\chead{\hmwkClass:\ \hmwkTitle}
\lhead{}
\cfoot{}
\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}
\setlength\parindent{0pt}
\title{
\vspace{2in}
\textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
\vspace{3in}
}
\author{\textbf{\hmwkAuthorName}}
\date{}

Related

How to disable automatic table number for bookdown::html_document2?

For html_document or pdf_document, there have been solutions to suppress automatic table name and number, [for example,] (How to suppress automatic table name and number in an .Rmd file using xtable or knitr::kable?).
However, adding the following in the YAML section
header-includes:
- \usepackage{caption}
and the following before the code chunk
\captionsetup[table]{labelformat=empty}
does not work for html_document2.
Knitted from the following .rmd file, the automatic table name and number "Table 1.1: " cannot be disabled in the output html file. Thanks in advance for help!
---
title: "Suppress automatic table name and number"
author: Generated by bookdown
output:
bookdown::html_document2: default
header-includes:
- \usepackage{caption}
---
# https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html
\captionsetup[table]{labelformat=empty}
```{r myirischunk, results = 'asis', tab.cap = NULL, echo = TRUE}
print(knitr::kable(head(iris), caption = "Table 1.a - First own table name"))
print(knitr::kable(tail(iris), caption = "Table 1.b - Last own table name"))

Text (not syntax) highlighting in Rmarkdown/Papaja

I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.
A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.
I'll include a minimal example below.
1) File called papaja_file.Rmd
---
# https://crsh.github.io/papaja_man/index.html
title : "Some Title"
shorttitle : "HEADER"
author:
- name : "Name R Here"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Include addresss"
email : "randomemail#usa.edu"
affiliation:
- id : "1"
institution : "Any University"
author_note: |
....
abstract: |
Text here for abstract.
keywords : "Keyword1, keyword2, keyword3"
bibliography : ["references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
lang : "english"
class : "man"
output : papaja::apa6_pdf
---
```{r load_packages, include = FALSE}
library("papaja")
```
```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```
```{r child = 'child_document.Rmd'}
```
\newpage
# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
Notice that it references a single child Rmarkdown document.
2) The child document with text, called child_document.Rmd
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
One sentence without highlighting. \hl{Another with highlighting.}
3) The preamble, called preamble.tex
\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}
Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.
Result after putting YAML header within the main papaja document rather than the child:
The YAML header in your child document is not evaluated, c.f.
The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.
https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document
But you can include your preamble.tex in you master file using:
output:
papaja::apa6_pdf:
includes:
in_header: preamble.tex
c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4
Result:

How to add .sty file to rmarkdown pdf_output

I'm not sure if I have the right approach here, I've been searching all day for a way to do this, none of the documents are explicit enough in their instruction.
I would like to use a template style which looks like this:
Which I have the .sty file for. I've tried to connect that file in my rmarkdown, with a header like this:
---
title: "title"
author: "author"
date: "`r format(Sys.time(), '%d %B, %Y')`"
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
includes:
template: analysis_orax.sty
---
However, this isn't adding the style. If I add the .tex file, it adds all of the body text, which isn't what I'm going for.
I don't quite care for using .tex or .sty; all I want is to have a nicely formatted .pdf for reporting!!
The .sty:
\usepackage{titlesec}
\usepackage{tikz}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage[left=4cm,right=2.5cm,top=2.5cm,bottom=2cm]{geometry}
\usepackage{fancyhdr}
%------------------Main Font-------------------------
\setmainfont{Fira Sans}
%Make sure you have the compiler "XeLaTeX" activated on your settings for your LaTeX document in order to see the font
%------------------Color Set--------------------------
\definecolor{LightBlue}{RGB}{66, 163, 251}
\definecolor{DarkBlue}{RGB}{36, 100, 176}
\definecolor{LightGray}{gray}{.94}
\definecolor{DarkGray}{gray}{.172}
\definecolor{Orange}{RGB}{229, 133, 3}
\definecolor{MediumBlue}{RGB}{38, 119, 193}
%------------------Section Default Setting-------------
\titleformat*{\section}{\color{DarkBlue}\normalfont\bfseries\Huge}
\titleformat*{\subsection}{\color{LightBlue}\normalfont\bfseries\LARGE}
\titleformat*{\subsubsection}{\color{MediumBlue}\normalfont\bfseries\LARGE}
%-------------------Section Numbers Removal------------
\setcounter{secnumdepth}{0}
%-------------------------Header & Footer------------------------
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{
\begin{tikzpicture}[remember picture,overlay] \node[anchor=north west, yshift=1.5mm, xshift=-1.5mm] at (current page.north west) {\includegraphics[height=25mm]{figures/header_corner.png}};
\end{tikzpicture}
}
\fancyfoot[C]{
\begin{tikzpicture}[remember picture,overlay] \node[anchor=south east, yshift=-1.5mm, xshift=1.5mm] at (current page.south east) {\includegraphics[height=29mm]{figures/banner.png}};
\end{tikzpicture}
\textcolor{LightGray}{\thepage}
}
Though I am not sure which yaml structure you have tried, the page you might see has the right answer. However, the indentation of that answer was not correct. Just try the yaml below, in which we need to indent by two spaces to specify the arguments of output.
---
title: "title"
author: "author"
date: "`r format(Sys.time(), '%d %B, %Y')`"
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
includes:
in_header:
- analysis_orax.sty
---
The section
in_header:
- analysis_orax.sty
is equivalent to
in_header: analysis_orax.sty
in your case, but if you have more files (.sty or .tex etc.) to include in header, you can use - and line breaks to indicate the file names, as shown below.
in_header:
- analysis_orax.sty
- another_one.sty

Set and resize image as footer Rmarkdown title page

I'm trying to set and resize an image as a footer in the title page of a markdown document.
I got the desired results in LaTex with:
\documentclass{article}
\title{Test}
\author{}
\date{}
\usepackage{graphicx}
\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\lfoot{\includegraphics[width=400px]{footer.png}}
}
\begin{document}
\maketitle
\end{document}
But when I try to do the same thing in the Markdown document I get the following error:
Error in yaml::yaml.load(string, ...) :
Scanner error: while scanning a plain scalar at line 6, column 5 found a tab character that violate intendation at line 7, column 1
Calls: ... parse_yaml_front_matter -> yaml_load_utf8 ->
Execution halted
The code I have is:
---
title: "Test"
author: ""
header-includes:
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \fancypagestyle{plain}{
- \fancyhf{}
- \renewcommand{\headrulewidth}{0pt}
- \renewcommand{\footrulewidth}{0pt}
- \lfoot{\includegraphics[width=400px]{footer.png}}}
output: pdf_document
---
I've tried solutions like Creating a footer for every page using R markdown but it does't do the job for the title page.
Try:
---
title: "Test"
author: ""
header-includes: |
\usepackage{graphicx}
\usepackage{fancyhdr}
\fancypagestyle{plain}{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\lfoot{\includegraphics[width=400px]{footer.png}}}
output: pdf_document
---
Yaml is a beast for escaping quotes and suchlike. You might want to create a separate file containing your latex header and just include that.
---
header-includes: yourfile.tex
---

Rmarkdown: different output folders, shared libs

I have an .R file wherein I for each unique value in a list, render a number of different .Rmd files. Something like this:
for (uddannelse in unique(c("Monkey","Horse"))) {
rmarkdown::render("file1.Rmd", output_dir=file.path(getwd(), uddannelse) ,output_file=paste("file1", uddannelse,".html", sep="_"), encoding="UTF-8")
rmarkdown::render("file2.Rmd", output_dir=file.path(getwd(), uddannelse), output_file=paste("file2", uddannelse,".html", sep="_"), encoding="UTF-8")
}
As evident by the render parameters, the html-output should go into a separate folder for each value in the list, in the above example: folder: "Monkey" and folder "Horse".
Each .Rmd file has the following front matter (the files go to a static html website and needs to have self_contained: false:
---
output:
html_document:
theme: readable
self_contained: false
lib_dir: pub/libs
css: pub/libs/custom_css/custom.css
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
However: When I call the render functions I recieve this error:
Error in relativeTo(basepath, dir) :
The path C:/Users/ac/Dropbox/2014_07_WIP/pub/libs/jquery-1.11.0 does not appear to be a descendant of C:/Users/ac/Dropbox/2014_07_WIP/Monkey/
So I guess the rmarkdwown::render first creates the lib directories relative to the Rmd file, yet expect the files to be placed relative to the output files.
How can I get around this, so that I can have a set of common Rmd input files in one folder, and have output in different folders, yet share a common lib?
I tried to place something like this in the fronmatter.
---
output:
html_document:
theme: readable
self_contained: false
lib_dir: "`r file.path(uddannelse, "libs")`"
css: "`r file.path(uddannelse, "libs", "custom_css", "custom.css")`"
date: "`r format(Sys.time(), '%d %B, %Y')`"
---
And I got this error:
Error in yaml::yaml.load(front_matter) :
Parser error: while parsing a block mapping at line 3, column 5did not find expected key at line 5, column 50
I solved my immediate problem by passing some of the front matter in the render call:
rmarkdown::render("file1.Rmd",
output_dir=file.path(uddannelse),
output_file=paste("file1", uddannelse,".html", sep="_"),
output_options=list(html_document =
list(self_contained = FALSE,
lib_dir = file.path(uddannelse, "lib"),
css = paste("lib", "custom_css", "custom.css",
sep="/"),
include = list(
after_body = file.path(uddannelse,
"footer_w_index.html")))),
encoding="UTF-8")
Notice that lib_dir has to be relative to the Rmdfile and css has to be relative to the output-file.
For some reason - regardless of whether I use paste or file.path(fsep="/", ...) the css path in the output file is linked with windows separator ("\") - and thus not usable in e.g. Firefox.

Resources