Setting custom chapter numbering in Markdown - header

I'm creating an R Markdown document which outputs a PDF document. My YAML header is the following:
---
title: "Introduction"
author: "John Doe
date: "August 26, 2018"
mainfont: Pancetta Pro
documentclass: book
output:
pdf_document:
number_sections: true
df_print: kable
fig_caption: yes
fig_width: 6
highlight: tango
includes:
in_header: preamble.tex
latex_engine: xelatex
geometry: headheight=25pt, tmargin=25mm, bmargin=20mm, innermargin=20mm, outermargin=20mm
---
In the preamble.tex file I want to have the following LaTeX commands (which just modify the way the headers are displayed):
\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]
However, when these last lines are included in the preamble.tex I get an error when knitting the R Markdown file:
! Argument of \paragraph has an extra }.
<inserted text>
\par
l.1290 \ttl#extract\paragraph
Error: Failed to compile Template.tex
I can't figure out why it won't run. The contents of thepreamble.texfile are the following:
% !TeX program = lualatex
\usepackage{relsize} % To make math slightly larger.
\newcommand{\thehook}{%
\hspace{.5em}%
\setlength{\unitlength}{1em}%
\raisebox{-.5em}{\begin{picture}(.4,1.7)
\put(0,0){\line(1,0){.2}}
\put(.2,0){\line(0,1){1.7}}
\put(.2,1.7){\line(1,0){.2}}
\end{picture}}%
\hspace{0.5em}%
} %This creates the "hook" symbol at the beginning of each chapter.
\usepackage{anyfontsize}
\usepackage{fontspec}
\setmainfont{Pancetta Pro}
% We set the font for the chapters:
\newfontfamily\chapterfont{Pancetta Pro}
% And now for the sections:
\newfontfamily\sectionfont{Pancetta Pro}
\usepackage{fancyhdr}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[RO]{\large\sffamily\rightmark\thehook\textbf{\thepage}}
\fancyhead[LE]{\large\sffamily\textbf{\thepage}\thehook\rightmark}
\fancypagestyle{plain}{%
\fancyhf{}
}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}
\fontsize{12}{20}\selectfont
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]
When excluding the last 6 lines in the previous code there's no error and the pdf is created.

If you want to use titlesec together with rmarkdown you have to add
subparagraph: yes
to your YAML headers, c.f. several other answers.
The default LaTeX class used by rmarkdown is article, which has no chapters. You should add
documentclass: report
or
documentclass: book
to your YAML header.

Related

Regulate how many PDF pages is to be displayed per page in when knitting to PDF in Rmarkdown?

I have a rmarkdown document where I import a few PDFs, and I would like to display four PDF-import-pages per page in my knitted PDF.
---
title: "combinePages"
author: "xxx"
date: '2022-xx-xx'
output:
pdf_document:
toc: true
header-includes:
- \usepackage{pdfpages}
---
\includepdf[pages={-}]{pdfs/Lecture1.pdf}
\includepdf[pages={-}]{pdfs/Lecture2.pdf}
Can this be done in rmarkdown?
According to the manual of pdfpages,
nup => Puts multiple logical pages onto each sheet of paper. The syntax of this option is: nup=⟨xnup⟩x⟨ynup⟩. Where ⟨xnup⟩ and ⟨ynup⟩ specify the umber of logical pages in horizontal and vertical direction, which are arranged on each sheet of paper. (Default: nup=1x1)
So, four pdf pages from an imported pdf file can be knitted per page using nup=2x2 in the option of \includepdf.
---
title: "combinePages"
author: "xxx"
date: '2022-xx-xx'
output:
pdf_document:
toc: true
header-includes:
- \usepackage{pdfpages}
---
## Multiple Page
\includepdf[pages={1-}, nup=2x2]{lorem_ipsum.pdf}
\includepdf[pages={1-}, nup=2x2]{lorem_ipsum.pdf}
So A page of the rendered document looks like this,
Explore the manual for more options.

RMarkdown cache leads to Latex error when knitting to PDF

I'm getting a weird error that I can't seem to understand. I have an rMarkdown file with a mixture of code, tables, and plain text (in RStudio).
I want to knit to PDF. I am able to do this, but only if I've deleted the cache folder. If the cache has been created, I get the following error when trying to do subsequent knits:
! LaTeX Error: Illegal character in array arg.
This is especially confusing as it always works the first time, and the PDF output looks exactly as I want it to. My code components are set to "cache=TRUE", because it's a big file and I want to cut down on loading time. I've looked up what that error means, but I can't find anything on how it relates to the cache.
If it's of any use, this is my header:
---
output:
pdf_document:
toc: yes
toc_depth: 4
number_sections: no
fig_caption: yes
latex_engine: xelatex
mainfont: Helvetica
font-family: Helvetica Neue
fontsize: 11pt
geometry: left=35mm, right=20mm, top=30mm, bottom=30mm
bibliography: resources/refs.bib
csl: resources/apa.csl
header-includes:
- \usepackage{fancyhdr} # Latex package for formatting PDFs.
- \pagestyle{fancy} # Set page style.
- \renewcommand{\headrulewidth}{1.5pt} # Thickness of header line.
- \setlength{\headheight}{13.59999pt} # Ensures consistent space between header line and text.
- \renewcommand{\href}[2]{#2\footnote{\url{#1}}} # Show URLs in footnote.
---
I can't directly share my data, but the setup for one of my tables looks like this:
exp2_age_gender_table %>%
kable("latex", booktabs = T) %>%
kable_styling(latex_options = "hold_position") %>%
column_spec(2, italic = TRUE) %>%
column_spec(4, italic = TRUE) %>%
row_spec(5, bold = TRUE)
The list of files contained in the created cache folder:
load-packages_0e5c21f97bd6e3c6091073c85b87d490.rdb
__packages
exp2-age-gend-table_009cc9300f71eb94b8251a5ed005e883.RData
exp2-age-gend-table_009cc9300f71eb94b8251a5ed005e883.rdb
exp2-age-gend-table_009cc9300f71eb94b8251a5ed005e883.rdx
exp2-age-gend-table-data_fc15cdaa97d5a922fc695858c44a93ac.RData
exp2-age-gend-table-data_fc15cdaa97d5a922fc695858c44a93ac.rdb
exp2-age-gend-table-data_fc15cdaa97d5a922fc695858c44a93ac.rdx
load-packages_0e5c21f97bd6e3c6091073c85b87d490.RData
load-packages_0e5c21f97bd6e3c6091073c85b87d490.rdx
raw-data_d44409d75a5aed8718bc355ff7b167fc.RData
raw-data_d44409d75a5aed8718bc355ff7b167fc.rdb
raw-data_d44409d75a5aed8718bc355ff7b167fc.rdx

YAML issue when knitting to pdf

I have a paper written in rnotebook, which I want to knit to pdf.
this is the beginning of it, including the YAML:
---
title: "Are shifts between points of view challenging for readers? An examination of readers' eye movements in response to Woolf's *To the Lighthouse* and *Mrs Dalloway*"
author: "Giulia Grisot, Kathy Conklin, Violeta Sotirova - The University of Nottingham"
date: '`r format(Sys.time(), "%d %B %Y")`'
output:
html_notebook:
fig_caption: yes
force_captions: yes
#highlight: pygments
number_sections: false
theme: readable
# csl: sage-harvard.csl
csl: apa.csl
bibliography: library.bib
link-citations: yes
nocite: |
#Grisot2018
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
```
```{r, include=F}
library(tidyverse)
library(ggpubr)
```
# Abstract
The emergence of empirical approaches within stylistics has increased interest in how literary texts are processed by readers. Techniques of speech and thought representation represent an area ripe for empirical investigation in this respect, especially when these cause interpretative ambiguities.
I have installed tinytex, and I have got miktex installed and pandoc.
I have tried to just use the "knit to pdf" command in RStudio but it gives me this error and i don't know what to do
Error in yaml::yaml.load(..., eval.expr = TRUE) :
Scanner error: while scanning for the next token at line 4, column 31 found character that cannot start any token at line 4, column 31
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted
I think this is down to the formatting in your yaml. When I knitted your code R Studio did some re-formatting of the title, but in the process replaced the ' with " in your date, causing the error. I don't have your .bib file so I can't test your exact code, but the following worked for me:
---
title: Are shifts between points of view challenging for readers? An examination of
readers' eye movements in response to Woolf's *To the Lighthouse* and *Mrs Dalloway*
author: "Giulia Grisot, Kathy Conklin, Violeta Sotirova - The University of Nottingham"
date: '`r format(Sys.time(), "%d %B %Y")`'
output:
pdf_document: default
html_notebook:
fig_caption: yes
force_captions: yes
number_sections: no
theme: readable
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
```
```{r, include=F}
library(tidyverse)
#library(ggpubr)
```
# Abstract

#nocite: | cites show up in html & pub but not in PDF

If I insert references via #nocite, these references show up in the html and pub versions of the knit book but not in the PDF. How can I ensure that the bibliography stays consistent across formats?
Here is a MWE with a single nocite reference added to book.bib ... #Book{Lander2014, author = {Lander, Jared P}, title = {R for everyone: Advanced analytics and graphics}, year = {2014}, publisher = {Pearson Education}}
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: bookdown::gitbook
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
nocite: |
#Lander2014
---
# Prerequisites
This is a _sample_ book written in **Markdown**. You can use anything that Pandoc's Markdown supports, e.g., a math equation $a^2 + b^2 = c^2$.
For now, you have to install the development versions of **bookdown** from Github:
```{r eval=FALSE}
devtools::install_github("rstudio/bookdown")
```
Remember each Rmd file contains one and only one chapter, and a chapter is defined by the first-level heading `#`.
To compile this example to PDF, you need to install XeLaTeX.
```{r include=FALSE}
# automatically create a bib database for R packages
knitr::write_bib(c(
.packages(), 'bookdown', 'knitr', 'rmarkdown'
), 'packages.bib')
```
Here is the sessionInfo()
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1 backports_1.1.0 bookdown_0.5 magrittr_1.5 rsconnect_0.8.5 rprojroot_1.2
[7] htmltools_0.3.6 tools_3.4.1 yaml_2.1.14 Rcpp_0.12.12 stringi_1.1.5 rmarkdown_1.6
[13] knitr_1.17 stringr_1.2.0 digest_0.6.12 evaluate_0.10.1
Within the output YAML for the .pdf, if you remove the option citation_package: natbib, it should work. See the pandoc docs on citation rendering: https://pandoc.org/MANUAL.html#citation-rendering. If "natbib" or "biblatex" are passed in, then "pandoc-citeproc" is not used.
I experimented with this and was able to generate references in the nocite element to appear in both HTML and .pdf. When you do this, you will want to include # References {-} or something similar, just as you would when writing to HTML.
Note that I am not expert in LaTeX and cannot speak to any unintended consequences which may occur when "natbib" or "biblatex" are not used for a .pdf build.

rmarkdown to pdf: image in headers x margin

I am new to markdown and want to produce pdf files with the university (and other) logo on each page. I tried to use fancyheadings, and I have a problem:
Putting the image in the headings works only if I call the image in the document, otherwise the pdf is not produced
NB: I also had to set the heading height otherwise the page 2 would not work with - \setlength{\headheight}{2.4 cm}.
Any suggestions were the problem is?
(using knit in Rstudio, all freshly installed)
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \pagestyle{fancy}
- \rhead {\includegraphics{pictures/headers.pdf}}
- \fancyfoot[CO,CE]{}
- \setlength{\headheight}{2.4 cm}
- \fancyfoot[LE,RO]{\thepage}
geometry: top=2cm, bottom=3cm
output: pdf_document
abstract: This is a pandoc test . . .
---
\lipsum[1-20]
![](./pictures/headers.pdf)
this line needed or it doesnot make an output ???
\cleardoublepage
This is an R Markdown document.
the includegraphics works only if there is a picture in the document, maybe because of the latex template (?).
A solution is to create a cache picture (1white pixel picture) and call it at the end of the code: