Generate phd html site using bookdown output from latex file - r

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

Related

R Bookdown: Append chapter without re-rending whole book

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 :)

How can I show affiliation in title page of bookdown pdf book while using authblk latex package?

I am using bookdown package in R to compile a bookdown::pdf_book. But I am unable to knit it when I use authblk latex package. The strange thing is that I can compile the same thing outside of RStudio, even when I am using authblk latex package. Let me share a minimal example here.
Preamble taken from TeXStudio
\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{authblk}
\begin{document}
\author[1]{First Author Name}
\author[2]{Second Author Name}
\affil[1]{College1}
\affil[2]{College2}
\title{Simple Book Example}
\date{January 2013}
\frontmatter
\maketitle
\tableofcontents
\mainmatter
\include{./TeX_files/chapter01}
\include{./TeX_files/chapter02}
\backmatter
% bibliography, glossary and index would go here.
\end{document}
YAML Header from Index.Rmd page
title: "A Minimal Book Example"
author: "Author Name"
affil: "College 1"
date: "`r Sys.Date()`"
output: pdf_document
documentclass: book
bibliography:
- book.bib
- packages.bib
description: |
This is a minimal example of using the bookdown package to write a book.
set in the _output.yml file.
The HTML output format for this example is bookdown::bs4_book,
site: bookdown::bookdown_site
---
YAML header from _output.yml file
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: pdflatex
citation_package: biblatex
keep_tex: yes
Latex code in preamble.tex
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{authblk}
The problem is that when I knit (or Build pdf_book) from RStudio, the college name is not seen in the title page of the PDF book. I have tried various ways, but I am getting one or the other errors. I want to show two authors (along with their respective affiliation) on the title page of the book. But here, I can't display it even for one author when I compile it through TinyTeX in RStudio. However, when I compile the TexStudio code in TeXStudio using TinyTeX, it displays two authors and their affiliations correctly. How can I solve this?

Appendix after references not working with biblatex

I wonder if it is possible to put an appendix after references when using biblatex. These question and answer or this answer showed how to do so without citation_package: biblatex but do not work anymore when I add this in YAML. Using child document to print appendix after references does not work either.
Is it possible to use both biblatex and this solution?
Here's a reproducible example (Not working. Remove citation_package: biblatex for a functional example):
---
title:
author:
date:
abstract:
output:
pdf_document:
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Part 1
#greenwood_financial_1989
# My references
<div id="refs"></div>
# Appendix
bla bla
with references.bib:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Edit: As suggested, I compared the latex document produced by the .Rmd file (by adding keep_tex: true in YAML) when I use citation_package: biblatex and when I don't.
I renamed the section "References" as "My references" in the .Rmd file to be able to distinguish the two sections in the latex document.
It appears that:
using citation_package: biblatex automatically adds \printbibliography at the end of the latex document, hence the "References" section with the actual references at the end of the PDF output. Besides, the section "My references" only contains \hypertarget{refs}{}.
when I remove citation_package: biblatex, the \printbibliography line disappears from the latex document, and the "My references" section contains \leavevmode\hypertarget{}{} with the id of the reference and its full description (that I do not paste here because it would only make this less readable).
Therefore, I guess this problem appears during the conversion from the .Rmd file to .tex format.

R Markdown: change the title of the "References" section?

I am writing a report with R Markdown in which I include references. The problem is that R markdown automatically includes references at the end of the report (without calling them with \printbibliography for example) and the section title is "References". I am writing in French so I would like the title to be "Références", but more generally is there any way to modify the title of that section?
Below is reproducible example:
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Partie 1
\cite{greenwood_financial_1989}
# Partie 2
bla bla
and here is the content of the references.bib file:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Does anybody have a solution?
Here's the solution that combines this answer on doing something similar with bibtex and this one on the TeX required to do it in biblatex, plus #Ralf's comment about adding lang: fr
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
lang: fr
---
In my document, I used :
header-includes:
- \usepackage[french]{babel}
And your Contents section also changes.
I had a similar problem I wanted to place the bibliography above a supplemental section and placing <div id = "refs"></div> where you want the references successfully repositioned the section. I also had no problem renaming the section by simply changing the # title above that section. This did work for PDF output as well as HTML despite appearing to be an HTML only solution, though I was also using the markdown flavored ([#Smith2019]) references in the body of my document I don't know if using the LaTeX style will affect things.

Error when rendering tufte_html_book() with R/bookdown

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.

Resources