Search code examples
latexr-markdown

To make a compact list in r-markdown


From here I know that enumitem provides an option nosep which removes all spacing before and after the list as well as between the items, which works well in pure latex file test.tex as follows:

\documentclass[a4paper]{article}

\usepackage{lipsum}
\usepackage[shortlabels]{enumitem}
\setlist{nosep}

\begin{document}

\lipsum[11]
\begin{enumerate}
\item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
\item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
\end{enumerate}
\lipsum[47]

\end{document} 

I try to realize this nosep functionality in the rmd file test.rmd to make a compact file and then I try:

---
documentclass: article
classoption: a4paper
output: 
  pdf_document: 
    latex_engine: pdflatex 
header-includes:
  - \usepackage{lipsum} 
  - \usepackage[shortlabels]{enumitem}
  - \setlist{nosep}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\lipsum[11]
\begin{enumerate}
\item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
\item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
\end{enumerate}
\lipsum[47]

The spacing between the items is fine with the output PDF of test.rmd, but the spacing before and after the list keeps unchanged, which is very different from the output PDF of test.tex! Did I miss something?


Solution

  • The difference between your latex code and the rmarkdown code is that rmarkdown will automatically load all kinds of unnecessary packages, amongst others the parskip package, which will add some vertical space between paragraphs. You can remove this vertical space like this

    ---
    documentclass: article
    classoption: a4paper
    output: 
      pdf_document: 
        latex_engine: pdflatex 
    header-includes:
      - \usepackage{lipsum} 
      - \usepackage[shortlabels]{enumitem}
      - \setlist{nosep}
      - \setlength{\parskip}{0pt}
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    \lipsum[11]
    \begin{enumerate}
    \item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
    \item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
    \end{enumerate}
    \lipsum[47]
    

    enter image description here

    (you might want to combine it with something like \setlength{\parindent}{1em} so paragraphs will again be recognisable)