I'm trying to build a book with R/bookdown and I'm facing a problem when producing a html version using tufte_html_book or html_book:
Error in split_chapters(output, page_builder, number_sections, split_by, :
The document must start with a first (#) or second level (##) heading
The book is correctly produced in gitbook or tufte_book2 output.
The document has four files index.Rmd, 01-Chap1.Rmd, 02-Chap2.Rmd, 03-Chap3.Rmd. I like to keep things clear, so index.Rmd only contains the YAML headers, plus some R code but no heading, which is probably what yields the error. Is there a way to make the html outputs of bookdown behave similarly to gitbook or pdf output?
Here is a small reproducible example:
Index.Rmd:
---
title: "Tufte Handout"
subtitle: "An implementation in R Markdown"
author: "JJ Allaire and Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::tufte_html_book:
toc: yes
bookdown::tufte_book2:
toc: yes
---
```{r setup1, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra =
packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
01-Chap1.Rmd
# Introduction
The Tufte handout style is a style that Edward Tufte uses in his books and handouts. Tufte's style is known for its extensive use of sidenotes, tight integration of graphics with text, and well-set typography. This style has been implemented in LaTeX and HTML/CSS^[See Github repositories [tufte-latex](https://github.com/tufte-latex/tufte-latex) and [tufte-css](https://github.com/edwardtufte/tufte-css)], respectively. We have ported both implementations into the [**tufte** package](https://github.com/rstudio/tufte). If you want LaTeX/PDF output, you may use the `tufte_handout` format for handouts, and `tufte_book` for books.
For HTML output, use `tufte_html`. These formats can be either specified in the YAML metadata at the beginning of an R Markdown document (see an example below), or passed to the `rmarkdown::render()` function. See #R-rmarkdown more information about **rmarkdown**.
I'm answering my own question.
The difference in behaviour between gitbook and (tufte_)html_book is in the split_by argument, which is split_by="chapter" in gitbook and split_by="section" in (tufte_)html_book. Adding split_by: chapter to the bookdown::tufte_html_book: yaml section allows to build the html without error.
Related
I'm using RBookdown with multiple chapters such as this minimal example.
After successfully implementing the custom block instructions here everything looks great. The boxes appear in both html and pdf formats, as intended.
However, I am using color fonts across the document, and upon inclusion of the latex custom block preamble items.
\usepackage{tcolorbox}
\newtcolorbox{blackbox}{
colback=black,
colframe=orange,
coltext=white,
boxsep=5pt,
arc=4pt}
now the compile error appears as
Warning: Package xcolor Warning: Incompatible color definition on input line 215.
I see no problems with the colors, they are all working as intended. Similar for the custom blocks, they all appear fine. Unless anyone has a better suggestion, I will just ignore the xcolor warnings.
I found similar discussions here and here and here but I still haven't found any fix to the error messages.
Note: Color is implemented using the solution in previous post
UPDATE: To create MWE, use this minimal example
and edit the index.Rmd file to look as follows
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::html_book
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."
---
```{r echo=FALSE}
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>",
color,
x)
} else x
}
```
# 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$. This is a _sample_ book written in **Markdown** with `r colorize("desired brown text", "brown")` which appears as intended.
Then also edit the preamble.tex file to include the line below.
\usepackage{tcolorbox}
Then use R command
bookdown::render_book("index.Rmd", "bookdown::pdf_book")
to compile into pdf, and the described errors will appear.
I try to use a Unicode character (U+2685) in math mode with Bookdown, I set mathfont: STIX Two Math (which contains this character), yet the resulting PDF contains an empty space. (At the same time the HTML is correct.)
What's wrong here?
(My best guess is that I should perhaps use the unicode-math package. Unfortunately I can't include it in the preamble.tex as it is incompatible with the mathspec, but at the same time I see no way to get rid of mathspec; it seems to be hardcoded in Bookdown that mathspec is usepackaged when xelatex is used.)
Minimal reproducible example (showing index.Rmd, all other file is the same as with the default new project created with RStudio):
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
link-citations: yes
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
mainfont: STIX Two Math
mathfont: STIX Two Math
---
# Prerequisites
In text: ⚀.
In math mode: $⚀$.
This is how the result looks like:
This issue is now solved (after an update to rmarkdown), see here.
I need to update the color and font of the header (only header) in a R Markdown PDF file. I have found recourses on how to do this for the whole document, but can't find an answer for changing the headers only.
Thank you kindly!
---
title: "Untitled"
output: pdf_document
---
Simplified version of the solution provided by Grada Gukovic:
You can add simple LaTeX statements to your document via the YAML header header-includes, e.g.:
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{sectsty}
- \allsectionsfont{\color{cyan}}
---
```{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>.
Result:
This is most useful for small additions like the one seen here. If you want to add more than a few lines of LaTeX code it is often easier to save them to an external file, say preamble.tex and include that via
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
Other possible places are before_body and after_body, c.f. ?rmarkdown::includes.
There is no option to do this in rmarkdown::pdf_document. You can do this by modifying the .tex template being used using the sectsty package for latex.
For example the following changes the color of all headers to cyan:
Download the default latex template from here:
tex template
Open the template in Notepad and add the following lines on an appropriate place in the document preamble(I have them as lines nr. 200 and 201):
\usepackage{sectsty}
\allsectionsfont{\color{cyan}}
Save the modified file with the extension .tex (my file is called "Cyansections.tex") and put it in R's working directory.
Modify the header of the .rmd document:
---
title: "Untitled"
output:
pdf_document:
template: Cyansections.tex
---
If you want a different color or font consult this answer
and sectsty's manual Especially section 4 of the manual for chanhing fonts
I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.
Let's say this is my main text file called main.Rmd:
---
title: "Main text"
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:
---
title: "Supporting info"
output:
bookdown::pdf_book:
toc: no
---
Here is the supporting text.
```{r supporting-figure}
plot(cars)
```
How can I cross-ref supporting-figure in the main text?
I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.
I also found this question:
Cross-reference figure in a separate Rmarkdown (PDF) file
but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)
I am not entirely sure how you are compiling these two files into a single bookdown document, because as it stands they are just two separate R Markdown documents. But there are two issues:
Figures need a caption to be cross-referenced
You can only cross-reference figures which have a caption assigned with fig.cap, as explained here:
If we assign a figure caption to a code chunk via the chunk option
fig.cap, R plots will be put into figure environments, which will be
automatically labeled and numbered, and can also be cross-referenced.
Incorrectly configured bookdown project:
From what I can tell, you have not got the project configured correctly for bookdown:
The main file should be called index.Rmd
Supporting files should not have any YAML
You should include site: bookdown::bookdown_site in the YAML of the main documene
Check out this answer for some tips on a minimal bookdown file.
Solution
index.Rmd
---
title: "Main text"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
supporting.Rmd
Here is the supporting text.
```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684
Hum, i trying to understand how R Bookdown generate different html output to migrate easily my phd actually in latex on a html website.
\usepackage[backend=biber,backref=true, natbib=true, isbn=false, doi=true, url=true, style=authoryear,maxcitenames=1, maxbibnames=999, sorting=nyt, refsection=chapter, hyperref]{biblatex}
As you can see, my thesis use biblatex with biber and natbib which permit me to use \autocite and \autocites command. I try multiple configuration to reuse this type of citation directly in markdown (and not [#key] or #key citation), without success.
I start with simple things in index.Rmd :
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: bookdown::gitbook
documentclass: book
bibliography: [these.bib]
biblio-style: apalike
latex_engine: xelatex
citation_package: natbib
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."
---
# Introduction
test my citation with \autocite{Pumain1997}.
these.bib contain this simple citation :
#article{Pumain1997,
author = {Pumain, Denise},
doi = {10.3406/spgeo.1997.1063},
issn = {0046-2497},
journal = {Espace g\'{e}ographique},
number = {2},
pages = {119--134},
title = {{Pour une th\'{e}orie \'{e}volutive des villes}},
url = {http://www.persee.fr/web/revues/home/prescript/article/spgeo\_0046-2497\_1997\_num\_26\_2\_1063},
volume = {26},
year = {1997}
}
You need to keep in mind that following rules are applied when rendering the Rmd file
bookdown specific commands will be translated by bookdown into the relevant LaTeX\HTML equivalent
markdown will be converted by pandoc into the relevant LaTeX\HTML equivalent
LaTeX (HTML) will be passed as is when the output format is LaTeX (HTML)
LaTeX (HTML) will be ignored when the output format is HTML (LaTeX)
Bottom line: use #key and [#key] in case you want to convert the document to both LaTeX and HTML