Bookdown: Cross-reference figure in another file - r

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

Related

Formatting bookdown theorem environment titles (e.g, example) using (ref:foo)

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

R Markdown PDF Change Font & Color for Headers

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

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.

How to create customized chapter heading in a Dynamic Document R and knitr

I´m not a LaTex user...actually started learning something about it as consequence of dynamic document I´m working with for a book document class I´m writing in R and knitr.
I´ve tried to customize the chapter headings, but meanwhile I did not get....I found the way to do this by LaTex (https://tex.stackexchange.com/questions/73421/how-to-create-specific-chapter-style-in-book-documentclass) however I´m not knowing how to address by RMardkown.
Does anybody could help me with that? How can I arrange the below LaTex commands to be triggered by RMarkdown (by .sty file?) or something similar way to customize the chapter heading in output pdf?
\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]
Thanks
Fabio
You can put the stuff you want in the header, except the documentclass line, in a seperate tex file (I use header.tex below). You can then specify in the header of your R-markdown file that you want to include this file in the header. The documentclass van also be set in the header of your R-markdown file. More info on this can be found on the rstudio site.
Below an example:
---
title: "Untitled"
output:
pdf_document:
includes:
in_header: header.tex
documentclass: book
---
\chapter{Introduction}
Section
=======
This is an R Markdown document.
header.tex looks like:
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]

How can I change paper size when using Knit PDF in RStudio?

By default the PDF documents created by the Knit PDF are US Letter size. Instead I would like to create A4 size documents. I have a feeling this should simple to change, either in the RStudio GUI or by adding an option to the metadata at the top of the Rmd file. Unfortunately I can't find any instructions how to do this. Is there a way to specify paper size, preferably within the Rmd file itself? I am still using RStudio version 0.98.953 but can upgrade if it would help.
I'd be grateful if someone could point me in the right direction.
OK, so I figured it out. In the .Rmd file's header, options documentclass and classoption get written into the preamble of the resulting .tex file. The article document class accepts a number of paper size options including a4paper. The header in the .Rmd file will then look something like this:
---
title: "Title"
author: "Name"
date: "Date"
output:
pdf_document
documentclass: article
classoption: a4paper
---
For more information see: http://rmarkdown.rstudio.com/pdf_document_format.html
At least in newer versions of the rmarkdown R package (and Pandoc) you can just set:
---
output: pdf_document
papersize: a4
---

Resources