Set itemize spacing in Quarto / RMarkdown PDF - pdf

In LaTeX, it's easy to control the vertical spacing in an itemized list by using \setlength\itemsep{1em}:
\begin{itemize}
\setlength\itemsep{1em}
\item one
\item two
\item three
\end{itemize}
How would I control this in Quarto / RMarkdown? If I write an itemized list using just markdown, I can't control the spacing, e.g.:
1. one
2. two
3. three
Is there a way to set a "global" spacing setting for all itemized or bullet lists? That would work.
Otherwise, is there a way to set the vertical spacing for a markdown list?
Edit:
This is the header I'm using for a Quarto PDF document (I didn't realize the solutions might be sensitive to the document class):
---
format:
pdf:
documentclass: scrartcl
papersize: letter
pdf-engine: xelatex
geometry:
- margin=1in
- heightrounded
include-in-header:
- preamble.tex
---

Assuming your are using the quarto pdf format, you could do something like this:
---
format: pdf
header-includes:
- \apptocmd{\tightlist}{\setlength{\itemsep}{10pt}}{}{}
---
1. one
2. two
3. three

Related

Quarto: PDF Document - Figure Caption size

How to customize font size of Figure Caption in a quarto pdf document? I have checked about the mainfont and fontfamily options, but the documentation doesn't provide examples of how to use change the font size for individual elements in a pdf document.
Since for pdf output, ultimately latex is used, you just need to find the corresponding latex solution to do what you want to do and incorporate those latex codes using LaTex Includes.
So to change the figure caption size, we can use the caption package. From section 2.3 of the caption package manual,
There are three font options which affect different parts of the caption: One affecting the whole caption (font), one which only affects the caption label and separator (labelfont) and at least one which only affects the caption text (textfont).
You set them up using the options font={⟨font options⟩}, labelfont={⟨font options⟩}, and textfont={⟨font options⟩}, where ⟨font options⟩ is a list of comma separated font options.
And these are the available font options:
scriptsize => Very small size
footnotesize => The size usually used for footnotes
small => Small size
normalsize => Normal size
large => Large size
Large => Even larger size
Read the manual (section 2.3) to know the details and more options.
---
title: "Figure Caption Size"
format:
pdf:
include-in-header:
text: |
\usepackage[font=Large,labelfont={bf,Large}]{caption}
---
## Quarto
```{r}
#| fig-cap: "Just a scatterplot"
plot(rnorm(1:10), rnorm(1:10))
```

Change Title/Headings Font in Quarto PDF Output

When RMarkdown .rmd documents are knitted as PDF, the text body as well as the title, subtitle and headings are rendered in the same LaTeX standard font.
When rendering a Quarto .qmd document as PDF, the font for the text body remains the same, but the title, subtitle and headings are rendered in a different font, without serifs.
To achieve consistency between the outputs of older R Markdown documents and newer Quarto documents, I would like to change the font for the title, subtitle and headings back to the normal font. How can I achieve this?
I tried using fontfamily: in the YAML header, but this did not find the fonts I wanted. I had some success by using \setkomafont{section}{\normalfont} in include-in-header:, as this did change the font, but only for h1 headings, not for h2 nor for the title or subtitle. It also removed all other formatting for h1 (e.g. fontsize, bold, etc.), which is not what I want.
Using this answer from Tex StackExchange we can do this in quarto easily.
---
title: "Fonts"
subtitle: "Changing fonts of title, subtitle back to normal font"
author: "None"
format:
pdf:
include-in-header:
text: |
\addtokomafont{disposition}{\rmfamily}
---
## Quarto
Quarto enables you to weave together content and executable code into a
finished document. To learn more about Quarto see <https://quarto.org>.
## Running Code
When you click the **Render** button a document will be generated that includes
both content and the output of embedded code.
And of course, check the section 3.6 - Text Markup of KOMA-Script manual, which provides a very detailed list of elements (like author, chapter, title, subtitle, date, etc.) for whose such changes can be done.
If the font used in the body is known, then you can set the font used in title and headings with sansfont: .... It's wise to also set mainfont to make sure they are the same.
The default font used is Latin Modern Roman, so adding this to the YAML frontmatter should do it:
---
mainfont: Latin Modern Roman
sansfont: Latin Modern Roman
---

Struggling with PDF output of bookdown

I thought it would be a good idea to write a longer report/protocol using bookdown since it's more comfortable to have one file per topic to write in instead of just one RMarkdown document with everything. Now I'm faced with the problem of sharing this document - the HTML looks best (except for wide tables being cut off) but is difficult to send via e-mail to a supervisor for example. I also can't expect anyone to be able to open the ePub format on their computer, so PDF would be the easiest choice. Now my problems:
My chapter headings are pretty long, which doesn't matter in HTML but they don't fit the page headers in the PDF document. In LaTeX I could define a short title for that, can I do that in bookdown as well?
I include figure files using knitr::include_graphics() inside of code chunks, so I generate the caption via the chunk options. For some figures, I can't avoid having an underscore in the caption, but that does not work out in LaTeX. Is there a way to escape the underscore that actually works (preferrably for HTML and PDF at the same time)? My LaTeX output looks like this after rendering:
\textbackslash{}begin\{figure\}
\includegraphics[width=0.6\linewidth,height=0.6\textheight]{figures/0165_HMMER} \textbackslash{}caption\{Output of HMMER for PA\_0165\}\label{fig:0165}
\textbackslash{}end\{figure\}
Edit
MWE showing that the problem is an underscore in combination with out.height (or width) in percent:
---
title: "MWE FigCap"
author: "LilithElina"
date: "19 Februar 2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE, fig.cap="This is a nice figure caption", out.height='40%'}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r pressure2, echo=FALSE, fig.cap="This is a not nice figure_caption", out.height='40%'}
plot(pressure)
```
Concerning shorter headings: pandoc, which is used for the markdown to LaTeX conversion, does not offer a "shorter heading". You can do that yourself, though:
# Really long chaper heading
\markboth{\thechapter~short heading}{}
[...]
## Really long section heading
\markright{\thesection~short heading}
This assumes a document class with chapters and sections.
Concerning the underscore in the figure caption: For me it works for both PDF and HTML to escape the underscore:
```{r pressure2, echo=FALSE, fig.cap="This is a not nice figure\\_caption", out.height='40%'}
plot(pressure)
```

Using rmarkdown to create headers from a large .txt input

I'm trying to create a PDF with headers from rmarkdown. I'm reading in a large text file, and I want to print out this PDF using headers. I can easily read in and print to the PDF with desired formatting, minus the headers, using
```{r comment='', echo=FALSE}
cat(readLines("blah.txt", encoding="UTF-8"), sep="/n")
```
However, I can't get rmarkdown to evaluate '#' in my text, which creates headers. I've inserted the '#' into different sections of the .txt file where I want to create a header, but it doesn't evaluate the hashtag.
Does anyone know how to get rmarkdown to evaluate the '#' as a header without messing up the formatting of the text file as I already have it?
RMarkdown recognizes # ...-headers only if there is an empty line before. So what you need is simply to use one more line-break \n (note the back-slashes).
```{r comment='', echo=FALSE, results='asis'}
cat(readLines("blah.txt", encoding="UTF-8"), sep="\n\n")
```
Note, that you may want to add results='asis' in the R-junk.

R markdown to PDF preserve (leading) whitespace

I have an R markdown file that I want to convert to PDF using knitr (or sweave).
For example:
---
output: pdf_document
---
```{python}
for x in range(3):
for y in range(3):
print(x+y)
```
If I knit to PDF and copy paste the for loop part back to a text editor, the tabs are gone. This is certainly expected behaviour considering how anti whitespace-preservation markdown and pdf are, but can I still somehow preserve the actual whitespace characters when knitting from R markdown to PDF?
It is not possible, there's nothing that can be done other than making sure the document looks right, i.e. visually the whitespace is there.
It comes down to what the PDF format actually is, read the accepted answer to this question - https://superuser.com/questions/198392/how-to-copy-text-out-of-a-pdf-without-losing-formatting