Search code examples
r-markdownrstudioxaringan

How to change font style, size and color in xaringan slide


I was wondering if there is a simple way to do the change of font size, colour and style (bold, italic, etc.) for particular words or phrases in individual xaraingan slides within rmarkdown.

The following is my code:

---
title: "Econ"
subtitle: "Week 1"
author: "Instructor"
date: "`r Sys.Date()`"
output: 
  xaringan::moon_reader:
    css: [default, metropolis, metropolis-fonts]
    lib_dir: libs
    nature:
      highlightStyle: arta
      highlightLines: true
      countIncrementalSlides: false
    
---

# Is economics a science?

--
Probably not, maybe pseudo-science?.

I want to make "pseudo-science" bold, italicized, red, slightly bigger.


Solution

  • To style certain lines or phrases, you need to define your own content class (say .my-style) using CSS and wrap those lines or phrases with that content class (like .my-style[lines or phrases]) and define CSS rules for styles that you want to use in CSS file styles.css. Then attach that CSS file by specifying it in the YAML key css: along with other style files.

    ---
    title: "Econ"
    subtitle: "Week 1"
    author: "Instructor"
    date: "`r Sys.Date()`"
    output: 
      xaringan::moon_reader:
        css: [default, metropolis, metropolis-fonts, "styles.css"]
        lib_dir: libs
        nature:
          highlightStyle: arta
          highlightLines: true
          countIncrementalSlides: false
        
    ---
    
    # Is economics a science?
    
    --
    Probably not, maybe .my-style[pseudo-science]?.
    

    styles.css

    .my-style {
      font-weight: bold;
      font-style: italic;
      font-size: 1.5em;
      color: red;
    }