I'm using RMarkdown to take course notes and each major section corresponds to a given lecture. I'd like to have the section headers automatically formatted as
"Lecture 1", "Lecture 2", etc. Here's basically what I'm looking for.
Lecture 1
Going over syllabus.
Lecture 2
Actually learning some stuff
However, when I use RMarkdown's default settings I get the following format (with section numbers preceding names):
1 Lecture
Going over syllabus.
2 Lecture
Actually learning some stuff.
How do I get the automatic numbering to either:
(1) follow the name (e.g. "October 1st - Lecture 1")
or
(2) be referenced in the name (e.g. with some sort of pseudocode "October 1st - Lecture {%section_number%}")?
Below is a minimal reproducible example of RMarkdown code which can be knit to PDF.
---
title: "Course_Notes"
output:
pdf_document:
number_sections: true
---
# Lecture
Going over the syllabus.
# Lecture
Actually learning some stuff
According to a TeX answer on altering the section title format, you can use the titlesec TeX package to change the section formatting as follows:
\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}
However, titlesec doesn't work out of the box with Pandoc: another Q&A shows that you need to add subparagraph: yes to the YAML header to get it working.
Putting it together, the following modifications should get you the result you're after:
---
title: "Course_Notes"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage[explicit]{titlesec}
- \titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}
subparagraph: yes
---
# Lecture
Going over the syllabus.
# Lecture
Actually learning some stuff
Related
I am rather experienced with R but new to markdown and especially bookdown. I wonder how to render a single chapter and merge it with the existing book without re-building the whole book. The reason why I am asking is that I am creating a book that contains parameterised chapters and each chapter runs very long (1-2 hours). All in all, it will be hundreds of chapters with the same analysis, but different input data and would time-wise add up. I am aware of the knit-and-merge approach, in fact, that's the reason why I chose bookdown, but somehow it is not working as expected.
I am honestly also a little bit confused by the different rendering possibilities. There is the built-book button, the knit button, serve_book(), render_book(), preview_chapter(), ... I figured that one should avoid using the knit button in bookdown and that serve_book() will automatically compile changes. My approach so far is entering serve_book() into console, where it renders the whole book, then I create a new chapter, hit save and it runs the chunks of only that chapter (exactly what I want!) and the single HTML file of the new chapter appears. However, it doesn't show them when I look at the book via the index.html file.
I am only interested in an HTML book and do not need any other output formats.
In order to recreate the problem, I am just using the bookdown example and adding chapters to it.
_bookdown.yaml
book_filename: "book_name"
new_session: yes #true
before_chapter_script: _common.R
delete_merged_file: true
language:
ui:
chapter_name: "Chapter "
_output.html
bookdown::bs4_book:
css: style.css
theme:
primary: "#10395B"
#repo: https://github.com/rstudio/bookdown-demo
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
bookdown::epub_book: default
_index.yaml
---
title: |
![](./Logo_PureGene.png){width=200px}|
|-|
||
author: "Name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
#output: bookdown::bs4_book
documentclass: book
bibliography: [book.bib, packages.bib]
# url: your book url like https://bookdown.org/yihui/bookdown
# cover-image: path to the social sharing image like images/cover.jpg
description: |
This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::bs4_book,
set in the _output.yml file.
biblio-style: apalike
csl: chicago-fullnote-bibliography.csl
---
# About {-}
I would appreciate any help!
Thanks in advance :)
The advice in bookdown for formatting long figure captions and other text is to create a reference,
(ref:foo) Title with _formatting_.
With a blank line above and below, and then referring to this in the figure caption.
I'm not getting this to work for theorem environments like example, however, when knitting to pdf.
Reproducible example:
---
title: "Table test"
output:
bookdown::pdf_document2:
keep_tex: true
---
# Test
(ref:foo) This is a test.
```{exercise revision1, echo = T, name="(ref:foo)"}
Some exercise description.
```
When knitted as a pdf:
rmarkdown::render("test.rmd")
Results in,
Exercise 1.1 ((ref:foo)). Some exercise description.
This is a bug in bookdown that I just fixed on Github. For now, you may try the development version of bookdown:
remotes::install_github(c('yihui/knitr', 'rstudio/bookdown'))
Here is simple RMarkdown document with two sections and two images.
---
output:
bookdown::html_document2: default
bookdown::word_document2: default
bookdown::pdf_document2: default
---
\newpage
# Part 1
Part 1 starts here. See Figure \#ref(fig:fig1-1)
![(\#fig:fig1-1) expected to be Figure 1.1.](/usr/lib/rstudio/www/images/rstudio.png)
# Part 2
Part 2 starts here. See Figure \#ref(fig:fig2-1)
![(\#fig:fig2-1) expected to be Figure 2.1.](/usr/lib/rstudio/www/images/rstudio.png)
I expect that two images will render with Knit to the following numbering - first is Figure 1.1, second is Figure 2.1. But I get this rendering only in html_document2 (see image below):
I use latest RStudio 1.1.414 with latest bookdown from Git (38efc82).
I have two questions here:
Why I don't have Figure 1.1 and Figure 2.1 in Word or PDF?
How can I get Figure 1.1 and Figure 2.1 in Word or PDF?
I can only address PDF output via LaTeX. By default the LaTeX documentclass article is used which uses continuous figure numbering. If your document is so long that it makes sense to number the figures within each top-level section, then you might want to use report or book instead, e.g.:
---
documentclass: book
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
Alternatively you can use some LaTeX package to change the formatting of figure numbers, e.g.:
---
header-includes:
\usepackage{chngcntr}
\counterwithin{figure}{section}
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
An alternative would be the \numberwithin command from amsmath in case you are using that package anyway.
I'd like to create a ToC (table of contents)-like list for all the theorems in a bookdown pdf book. It seems a similar question to how to Create index of definitions / theorems at end of bookdown book, but I want to insert the ToC of theorems right after the ToC of the chapters and before the main body. Moreover, page numbers are needed.
There are some clues in the TeX community:
Creating list of for \newtheoremstyle
ToC like list of definitions (using theorem environments)
Generate automatically a list of definitions (user-defined theorem environment) in appendix
Using the closest answer among them, I could get an imperfect ToC-like list of theorems. My reproducible mini example is as follows, which includes three files.
index.Rmd:
---
title: "A Minimal Book Example"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
before_body: before_body.tex
latex_engine: xelatex
keep_tex: yes
---
# Prerequisites
```{block, type='tip', name = 'My Important Tip'}
This is Tip One.
```
```{lemma}
This is Lemma One.
```
```{example}
This is Example One.
```
preamble.tex:
% format of tips
\usepackage{amsthm}
\usepackage{etoolbox}
\newtheoremstyle{mystyle}
{\topsep}{\topsep}{}{}{\bfseries}{:}{\newline}
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}%
\ifstrempty{#3}%
{\addcontentsline{def}{subsection}{#1~\thetip}}%
{\addcontentsline{def}{subsection}{#1~\thetip~(#3)}}}
\theoremstyle{mystyle}
\newtheorem{tip}{Tip}[chapter]
% toc of tips
\makeatletter
\newcommand\tipname{Tip}
\newcommand\listtipname{List of Tips}
\newcommand\listoftips{
\section*{\listtipname}\#starttoc{def}}
\makeatother
before_body.tex:
\listoftips
After the book was built, I was happy to see a List of Tips with page numbers in the pdf output:
However, there were two problems:
Not only the 'tip' blocks but also the lemmas in my book were listed in the table. How could I exclude the lemmas from the table?
The title should be Tip 1.1: My Important Tip. How could I display the title My Important Tip in the list?
Another unimportant question is: I have the 'Example' environment as well in the mini example. Why is Lemma 1 in the list of tips, but Example 1 not? What should I do if I want to create a list of tips, a list of lemmas, and a list of examples, separately?
I guess I am quite close to the solution, but I just do not know how to move a step forward.
Update:
I just found that the following codes in a .tex file can pass the tip title 'My Important Tip' to the ToC:
\begin{tip}[My Important Tip]
This is Tip One.
\end{tip}
This will result in Tip 1.1 (My Important Tip) in the ToC of the tips. How could I specify it in a knitr block? name = 'My Important Tip' does not work.
In a .Rmd file with the header below, I want to include an abstract, so I tried the standard LateX article form,
\abstract{This paper explores a variety of topics related to the question of testing the equality of
covariance matrices in multivariate linear models, particularly in the MANOVA setting.
The main focus is on graphical methods that can be used to understand features of data related
to this question.}
But, surprisingly (I know this seems weird), the references in my References section become badly formatted -- no spacing between references, odd indentations. So, how can I include something that looks like an abstract?
My YAML header is:
---
title: "Notes on Testing Equality of Covariance Matrices"
author: "Michael Friendly"
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
includes:
in_header: mystyles.tex
csl: apa.csl
bibliography:
- "C:/Users/friendly/Dropbox/localtexmf/bibtex/bib/statistics.bib"
- "C:/Users/friendly/Dropbox/localtexmf/bibtex/bib/graphics.bib"
---
Edit: Thinking this over, the problem may be that pandoc-citeproc is somehow confused by something done by using \abstract{} in the document.
The rmarkdown package now allows for including an abstract in your YAML. Like so:
abstract: "This is my abstract."
See this blog post for an example.