Bookdown/thesisdown/huskydown: change page margins for a single page within a document

Viewed 826

I have a huskydown document (which is based on thesisdown/bookdown) written in Rmarkdown which is knitted to PDF/LaTex but later also to HTML.

For a single page within the document, I would like to change the page margins (e.g. to include a very large table/figure/external PDF). After this page, the page margins should return to their default values.

What I tried:

---
author: 'Your R. Name'
date: 'May 20xx'
institution: 'Reed College'
division: 'Mathematics and Natural Sciences'
advisor: 'Advisor F. Name'
# If you have more two advisors, un-silence line 7
#altadvisor: 'Your Other Advisor'
department: 'Mathematics'
degree: 'Bachelor of Arts'
title: 'My Final College Paper'
knit: "bookdown::render_book"
site: bookdown::bookdown_site
output: 
 thesisdown::thesis_pdf: default
---

### Normal page margins again
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

\newpage
### Reduced page margins
```{r, echo = FALSE, results = "asis"}
cat("\\newgeometry{left=1cm,right=1cm,top=1cm,bottom=1cm}")
```

```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

```{r, echo = FALSE, results = "asis"}
cat("\\restoregeometry")
```

\clearpage
Restore the page margins

### Normal page margins again
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()
```

# ! Undefined control sequence.
# l.190 \newgeometry
#                   {left=1cm,right=1cm,top=1cm,bottom=1cm} 

I also tried

\newgeometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
<!-- material for this page -->
\clearpage
\restoregeometry

as suggested by @bretauv. It works in a simple Rmd file, but not in my use case with huskydown. I ended up with the same error message.

1 Answers

As hinted by @bretauv in the comment to your question, this issue isn't really coming from rmarkdown or bookdown. It's a LaTeX error which is easily solved.

Background

As you seem to know, commands from the geometry package can be used to adjust the layout (i.e. especially the page margins) in LaTeX documents. Basically, it is possible to use such LaTeX commands within rmarkdown documents. However, a prerequisite is that the corresponding LaTeX package is loaded. This is not the case here, hence the error

! Undefined control sequence.
l.190 \newgeometry

which states that the command \newgeometry couldn't be found.

Solution

Packages in LaTeX are loaded with the command \usepackage{<pkg_name>} in the header. This command cannot simply be used in the rmakdown document, it has to be set in a separate template that tells pandoc how to create a PDF from the .Rmd file in the end. For huskydown / thesisdown this is the file template.tex in the project directory.

Open the file and ensure that the package is loaded by adding right the following line before \begin{document} and then save the file:

\usepackage[left=6cm,right=6cm,top=6cm,bottom=6cm]{geometry}

This will set some default margins and spacings. I chose some (rather dramatic) values that you can of course adjust to your liking.

Knitting your .Rmd should work and you now should see the effects of \newgeometry{} or \restoregeometry. Also, note that is not necessary to fence these with a code chunk.

Example

(using thesisdown)

### Default (reduced) page margins as defined by geometry    
```{r, echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point()
```
 
\newpage
 
\newgeometry{left=3cm,top=6cm,right=3cm,bottom=6cm}
 
### Larger margins
 
```{r, echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point()
```

enter image description here

Related