├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── css_style.R ├── define_colors.R ├── flair_chunk.R ├── mathjax.R └── setup_colors.R ├── README.Rmd ├── README.md ├── man ├── figures │ └── logo.png └── setup_colors.Rd └── xaringancolor.Rproj /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^xaringancolor\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^README\.Rmd$ 4 | ^LICENSE\.md$ 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .Rdata 4 | .httr-oauth 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: xaringancolor 2 | Title: Uniform Colors in Xaringan Presentations 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "Emil", 6 | family = "Hvitfeldt", 7 | role = c("aut", "cre"), 8 | email = "emilhhvitfeldt@gmail.com", 9 | comment = c(ORCID = "0000-0002-0679-1945")) 10 | Description: Setup uniform colors in xaringan presentations to be used across 11 | html, equations and code chunks. 12 | License: MIT + file LICENSE 13 | Encoding: UTF-8 14 | LazyData: true 15 | Roxygen: list(markdown = TRUE) 16 | RoxygenNote: 7.1.1.9000 17 | Imports: 18 | purrr, 19 | flair, 20 | clipr 21 | URL: https://github.com/EmilHvitfeldt/xaringancolor 22 | BugReports: https://github.com/EmilHvitfeldt/xaringancolor/issues 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2021 2 | COPYRIGHT HOLDER: xaringancolor authors 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2021 xaringancolor authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(setup_colors) 4 | -------------------------------------------------------------------------------- /R/css_style.R: -------------------------------------------------------------------------------- 1 | css_style <- function(colors) { 2 | c( 3 | "" 6 | ) 7 | } 8 | -------------------------------------------------------------------------------- /R/define_colors.R: -------------------------------------------------------------------------------- 1 | define_color <- function(name, color) { 2 | rgb <- grDevices::col2rgb(color)[, 1] / 255 3 | paste0( 4 | "$$\\require{color}\\definecolor{", 5 | name, 6 | "}{rgb}{", 7 | rgb[1], 8 | ", ", 9 | rgb[2], 10 | ", ", 11 | rgb[3], 12 | "}$$" 13 | ) 14 | } 15 | 16 | define_colors <- function(colors) { 17 | c( 18 | "
", 19 | purrr::imap_chr(colors, ~define_color(.y, .x)), 20 | "
" 21 | ) 22 | } 23 | -------------------------------------------------------------------------------- /R/flair_chunk.R: -------------------------------------------------------------------------------- 1 | flair_chunk <- function(colors) { 2 | c( 3 | "```{r flair_color, echo=FALSE}", 4 | "library(flair)", 5 | paste0(names(colors), " <- \"", colors, "\""), 6 | "```" 7 | ) 8 | } 9 | -------------------------------------------------------------------------------- /R/mathjax.R: -------------------------------------------------------------------------------- 1 | mathjax_config <- c( 2 | " 3 | 14 | " 15 | ) 16 | 17 | mathjax_color <- function(name) { 18 | colors <- paste0(" ", name, ": [\"{\\\\color{", name, "}{#1}}\", 1]") 19 | 20 | if (length(colors) == 1) { 21 | return(colors) 22 | } 23 | 24 | c( 25 | paste0(colors[-length(colors)], ","), 26 | colors[length(colors)] 27 | ) 28 | } 29 | 30 | mathjax <- function(colors) { 31 | c( 32 | mathjax_config[1], 33 | mathjax_color(names(colors)), 34 | mathjax_config[2] 35 | ) 36 | } 37 | -------------------------------------------------------------------------------- /R/setup_colors.R: -------------------------------------------------------------------------------- 1 | #' Setup colors for xaringan document 2 | #' 3 | #' @param ... named parameters of colors to use. 4 | #' 5 | #' @return code to clipboard 6 | #' @export 7 | #' 8 | #' @examples 9 | #' \dontrun{ 10 | #' setup_colors(red = "red", blue = "blue", green = "green") 11 | #' } 12 | setup_colors <- function(...) { 13 | colors <- list(...) 14 | 15 | colors <- purrr::map_chr(colors, col2hex) 16 | 17 | res <- c( 18 | define_colors(colors), 19 | mathjax(colors), 20 | css_style(colors), 21 | "\n", 22 | flair_chunk(colors) 23 | ) 24 | 25 | clipr::write_clip(res) 26 | 27 | message( 28 | "Color setup have been copied to clipboard. ", 29 | "Please paste at start of xaringan document" 30 | ) 31 | return(invisible()) 32 | } 33 | 34 | col2hex <- function(x) { 35 | colMat <- grDevices::col2rgb(x) 36 | grDevices::rgb( 37 | red = colMat[1, ] / 255, 38 | green = colMat[2, ] / 255, 39 | blue = colMat[3, ] / 255 40 | ) 41 | } 42 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 | # xaringancolor 17 | 18 | 19 | 20 | 21 | The goal of xaringancolor is to allow you to change the colors uniformly across text, equations, and code. 22 | 23 | ## Installation 24 | 25 | You can install the package with: 26 | 27 | ``` r 28 | # install.packages("devtools") 29 | devtools::install_github("EmilHvitfeldt/xaringancolor") 30 | ``` 31 | 32 | This package will most likely never be put on CRAN. 33 | 34 | ## How to setup 35 | 36 | xaringancolor only contain 1 function; `setup_colors()`. Pass the colors you want to use in as named arguments. Please be careful and use simple names, preferably letters only, no numbers, spaces, or other characters. 37 | 38 | ```{r, eval=FALSE} 39 | library(xaringancolor) 40 | setup_colors( 41 | red = "red", 42 | green = "green", 43 | blue = "blue" 44 | ) 45 | ``` 46 | 47 | Running this code includes some lines of code into your clipboard. Paste this into the beginning of your [xaringan](https://github.com/yihui/xaringan) document. After you have done that you can delete the above code, you won't be needing it anymore. 48 | 49 | ## How to use 50 | 51 | ### HTML 52 | 53 | css classes named according to the colors have been created, so if you want some text to be red you type `.red[my text]` and text inside the square brackets turn red. This only works for the colors you have created and you need to put a leading period (.) before the color name when you use the css class. 54 | 55 | ### Equations 56 | 57 | A LaTeX macro has been created for each color. Use them as follows 58 | 59 | ```{code, eval=FALSE} 60 | $$\red{Y} = a \blue{X} + b$$ 61 | ``` 62 | 63 | and `Y` will be colored red and `X` will be colored blue. 64 | 65 | ### Code 66 | 67 | A variable for each color has been created. These can be used directly in plots such as with `geom_abline(..., color = red)` but they can also be used with **flair**. 68 | 69 | Start with a named chunk with both `include` and `eval` set to `FALSE`. This is the code you want to highlight 70 | 71 | ```code 72 | ``{r mtcars_mean, include=FALSE, eval=FALSE} 73 | lapply(mtcars, mean) 74 | `` 75 | ``` 76 | 77 | then you add another chunk with `echo=FALSE` where you use the `decorate()` to select the previous chunk by name and `flair()` to denote which parts of the code should be colors which way. Use the color variables here to achieve uniform coloring throughout the presentation. 78 | 79 | ```code 80 | ``{r, echo=FALSE} 81 | decorate("mtcars_mean") %>% 82 | flair("mtcars", color = red) %>% 83 | flair("mean", color = blue) 84 | `` 85 | ``` 86 | 87 | See [this link](https://r-for-educators.github.io/flair/index.html) for more information on how to use **flair**. 88 | 89 | ## How it works 90 | 91 | The code will look something like the code below and serve 3 main purposes. The first section defines the LaTeX colors and the second section uses [Mathjax](https://www.mathjax.org/) to generate LaTeX macros which allow us to use colors in the equations. 92 | 93 | ```code 94 | 95 |
96 | $$\require{color}\definecolor{red}{rgb}{1, 0, 0}$$ 97 | $$\require{color}\definecolor{green}{rgb}{0, 1, 0}$$ 98 | $$\require{color}\definecolor{blue}{rgb}{0, 0, 1}$$ 99 |
100 | 101 | 114 | 115 | ``` 116 | 117 | The third section defines a \ tag with sets the colors so we can use them in the html text. 118 | 119 | ```code 120 | 121 | 126 | 127 | ``` 128 | 129 | And the fourth and final section creates a R chunk that sets us up to use [flair](https://github.com/r-for-educators/flair) package. It loads the package and defines character vectors that specify the colors. 130 | 131 | ```code 132 | 133 | ``{r flair_color, echo=FALSE} 134 | library(flair) 135 | red <- "#FF0000" 136 | green <- "#00FF00" 137 | blue <- "#0000FF" 138 | `` 139 | 140 | ``` 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # xaringancolor 5 | 6 | 7 | 8 | 9 | The goal of xaringancolor is to allow you to change the colors uniformly 10 | across text, equations, and code. 11 | 12 | ## Installation 13 | 14 | You can install the package with: 15 | 16 | ``` r 17 | # install.packages("devtools") 18 | devtools::install_github("EmilHvitfeldt/xaringancolor") 19 | ``` 20 | 21 | This package will most likely never be put on CRAN. 22 | 23 | ## How to setup 24 | 25 | xaringancolor only contain 1 function; `setup_colors()`. Pass the colors 26 | you want to use in as named arguments. Please be careful and use simple 27 | names, preferably letters only, no numbers, spaces, or other characters. 28 | 29 | ``` r 30 | library(xaringancolor) 31 | setup_colors( 32 | red = "red", 33 | green = "green", 34 | blue = "blue" 35 | ) 36 | ``` 37 | 38 | Running this code includes some lines of code into your clipboard. Paste 39 | this into the beginning of your 40 | [xaringan](https://github.com/yihui/xaringan) document. After you have 41 | done that you can delete the above code, you won’t be needing it 42 | anymore. 43 | 44 | ## How to use 45 | 46 | ### HTML 47 | 48 | css classes named according to the colors have been created, so if you 49 | want some text to be red you type `.red[my text]` and text inside the 50 | square brackets turn red. This only works for the colors you have 51 | created and you need to put a leading period (.) before the color name 52 | when you use the css class. 53 | 54 | ### Equations 55 | 56 | A LaTeX macro has been created for each color. Use them as follows 57 | 58 | ``` code 59 | $$\red{Y} = a \blue{X} + b$$ 60 | ``` 61 | 62 | and `Y` will be colored red and `X` will be colored blue. 63 | 64 | ### Code 65 | 66 | A variable for each color has been created. These can be used directly 67 | in plots such as with `geom_abline(..., color = red)` but they can also 68 | be used with **flair**. 69 | 70 | Start with a named chunk with both `include` and `eval` set to `FALSE`. 71 | This is the code you want to highlight 72 | 73 | ``` code 74 | ``{r mtcars_mean, include=FALSE, eval=FALSE} 75 | lapply(mtcars, mean) 76 | `` 77 | ``` 78 | 79 | then you add another chunk with `echo=FALSE` where you use the 80 | `decorate()` to select the previous chunk by name and `flair()` to 81 | denote which parts of the code should be colors which way. Use the color 82 | variables here to achieve uniform coloring throughout the presentation. 83 | 84 | ``` code 85 | ``{r, echo=FALSE} 86 | decorate("mtcars_mean") %>% 87 | flair("mtcars", color = red) %>% 88 | flair("mean", color = blue) 89 | `` 90 | ``` 91 | 92 | See [this link](https://r-for-educators.github.io/flair/index.html) for 93 | more information on how to use **flair**. 94 | 95 | ## How it works 96 | 97 | The code will look something like the code below and serve 3 main 98 | purposes. The first section defines the LaTeX colors and the second 99 | section uses [Mathjax](https://www.mathjax.org/) to generate LaTeX 100 | macros which allow us to use colors in the equations. 101 | 102 | ``` code 103 |
104 | $$\require{color}\definecolor{red}{rgb}{1, 0, 0}$$ 105 | $$\require{color}\definecolor{green}{rgb}{0, 1, 0}$$ 106 | $$\require{color}\definecolor{blue}{rgb}{0, 0, 1}$$ 107 |
108 | 109 | 122 | ``` 123 | 124 | The third section defines a \ 133 | ``` 134 | 135 | And the fourth and final section creates a R chunk that sets us up to 136 | use [flair](https://github.com/r-for-educators/flair) package. It loads 137 | the package and defines character vectors that specify the colors. 138 | 139 | ``` code 140 | ``{r flair_color, echo=FALSE} 141 | library(flair) 142 | red <- "#FF0000" 143 | green <- "#00FF00" 144 | blue <- "#0000FF" 145 | `` 146 | ``` 147 | -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EmilHvitfeldt/xaringancolor/5e9f84cd6ef28393d3e2438d551207d3cc2b8bbe/man/figures/logo.png -------------------------------------------------------------------------------- /man/setup_colors.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/setup_colors.R 3 | \name{setup_colors} 4 | \alias{setup_colors} 5 | \title{Setup colors for xaringan document} 6 | \usage{ 7 | setup_colors(...) 8 | } 9 | \arguments{ 10 | \item{...}{named parameters of colors to use.} 11 | } 12 | \value{ 13 | code to clipboard 14 | } 15 | \description{ 16 | Setup colors for xaringan document 17 | } 18 | \examples{ 19 | \dontrun{ 20 | setup_colors(red = "red", blue = "blue", green = "green") 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /xaringancolor.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | LineEndingConversion: Posix 18 | 19 | BuildType: Package 20 | PackageUseDevtools: Yes 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | PackageRoxygenize: rd,collate,namespace 23 | --------------------------------------------------------------------------------