rstudio / thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
https://rstudio.github.io/thematic/
Other
244 stars 10 forks source link

Support auto-theming inside of bookdown #109

Open stephan-koenig opened 2 years ago

stephan-koenig commented 2 years ago

Hello!

I want to use {thematic} with bookdown to propagate the styling of the bookdown to plots, tables, etc., to generate a more cohesive look. Would it be possible to extend the use of thematic::thematic_rmd() also to support bookdown?

I have tried the following approach in a bookdown project that only contains an index.Rmd file by adding thematic::thematic_rmd():

--- 
title: "A Minimal Book Example"
author: "John Doe"
date: "`r Sys.Date()`"
description: "A simple bookdown using {thematic}"
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::bs4_book:
    theme:
      bootswatch: cyborg
---

# About

```{r setup, include = FALSE}
thematic::thematic_rmd()

Here a plot:

plot(pressure)

The output looks like this:

![Screen Shot 2021-09-15 at 1 19 00 PM](https://user-images.githubusercontent.com/11433347/133505966-e1d38983-75c7-40d7-af19-45dcca7a890e.png)

i.e. the plot is not styled like the theme, and I receive the following warnings:

Warning messages: 1: thematic was unable to resolve bg='auto'. Try providing an actual color (or NA) to the bg argument of thematic_on(). By the way, 'auto' is only officially supported in shiny::renderPlot(), some rmarkdown scenarios (specifically, html_document() with theme!=NULL), in RStudio, or if auto_config_set() is used. 2: thematic was unable to resolve fg='auto'. Try providing an actual color (or NA) to the fg argument of thematic_on(). By the way, 'auto' is only officially supported in shiny::renderPlot(), some rmarkdown scenarios (specifically, html_document() with theme!=NULL), in RStudio, or if auto_config_set() is used. 3: thematic was unable to resolve accent='auto'. Try providing an actual color (or NA) to the accent argument of thematic_on(). By the way, 'auto' is only officially supported in shiny::renderPlot(), some rmarkdown scenarios (specifically, html_document() with theme!=NULL), in RStudio, or if auto_config_set() is used.



Thank you,

Stephan

Edit: Added missing line of code.
cpsievert commented 2 years ago

Looks like some changes will have to be made to bookdown::bs4_book() for the auto-theming to work, but in the meantime, you can do something like thematic::thematic_rmd(bg = "#060606", fg = "#fff", accent = "#0068D9") (which would make the plots match the cyborg theme)

cderv commented 2 years ago

@cpsievert can you detail the change required in bookdown::bs4_book() ? A deeper introduction to thematic would help me know what to look for.

@stephan-koenig thanks for opening the issue in bookdown

cpsievert commented 2 years ago

@cderv thematic_rmd(bg = "auto", fg = "auto") usage in {rmarkdown} without shiny: runtime relies on a {bslib} theme to be bslib::bs_global_set() (which rmarkdown::html_document_base does). Since bookdown::bs4_book() doesn't pass its theme to html_document, the theme isn't set globally, so {thematic} can't find it. It seems like bs4_book should/could be doing that, but if there's a good reason not to, it seems you could do something similar to https://github.com/rstudio/rmarkdown/blob/ebf0d096c935acf4eb3374c850dcd5cbb7944a08/R/html_document_base.R#L55-L68 ?

cderv commented 2 years ago

Ok I see. I don't know if there is a good reason or not. I'll play with that. Thanks !