mhwh
mhwh

Reputation: 490

Set caption font size for figures in YAML header of Rmarkdown

I am using Rstudios markdown to write a report which I would like to compile to pdf using latex as well as to html...

Is it somehow possible to adjust the size of caption fonts for images included in an ![caption](figure.jpg) environment? At the moment captions in html look like the rest of the text.

Upvotes: 4

Views: 4659

Answers (3)

Masood Sadat
Masood Sadat

Reputation: 1337

Thanks to @Johnny's earlier comment, I wanted a bit more modification to my figure caption in my pdf report. I wanted a caption to look like the following:

Figure 1. This is a graph to explain productivity.

To get this, add in YAML:

---
title: "Title"
author: "Author"
header-includes:
  - \usepackage[font={small,it}, labelfont={bf}]{caption}
output:
  pdf_document
---

Upvotes: 3

Johnny
Johnny

Reputation: 771

This answer has obviously already been accepted but just for completeness I am adding the PDF aspect of the question.

If you wish to alter the caption font size for your PDF documents I would recommend to use the caption package via one of the following two approaches:

header-includes - option:

---
title: "Title"
author: "Jorge Luis Borges"
header-includes:
  - \usepackage[font={small}]{caption}
output:
  pdf_document
---

The focus here is on font={<font options>}.

in_header - option:

---
title: "Title"
author: "Jorge Luis Borges"
output:
  pdf_document:
    includes:
      in_header: style.tex
---

Where style.tex is included in the same directory that contains the .Rmd file. The style.tex file needs to contain \usepackage[font={small}]{caption}.

As before, the focus here is on font={<font options>}.


With the caption package you have loads of customisation options, just to name a few, see below:

  • scriptsize = very small size
  • footnotesize = size typically used for footnotes
  • small = small size
  • large = large size
  • it = italics
  • bf = bold face

For full reference please visit the official documentation.

Also, some cross-references here to articles on the TexSE site:

Change the font of figure captions

How to include Latex package in RMarkdown

Upvotes: 10

scoa
scoa

Reputation: 19867

You can control this with css. For instance:

<style type="text/css">
.caption {
    font-size: x-small;
}
</style>

![caption](figure.jpg)

See css guides such as this for the available font-size options.

Upvotes: 3

Related Questions