title: How to use custom function for color text across all chapters of RBookdown?
body: <p>I'm using RBookdown with multiple chapters such as <a href="https://github.com/rstudio/bookdown-demo" rel="nofollow noreferrer">this minimal example</a>.</p>
  <p>In order to have color fonts in both html and pdf formats <a href="https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html" rel="nofollow noreferrer">section 5.1.1 here</a> provides the colorize function</p>
  <pre><code>colorize &lt;- function(x, color) {
  if (knitr::is_latex_output()) {
    sprintf(&quot;\\textcolor{%s}{%s}&quot;, color, x)
  } else if (knitr::is_html_output()) {
    sprintf(&quot;&lt;span style='color: %s;'&gt;%s&lt;/span&gt;&quot;, color,
      x)
  } else x 
  }
  </code></pre>
  <p>This is great and I want to use it, but I don't know where to put it.  If I put it inside the <a href="https://bookdown.org/yihui/bookdown/yaml-options.html" rel="nofollow noreferrer">YAML header</a> like so</p>
  <pre><code> --- 
   title: &quot;A Minimal Book Example&quot;
   author: &quot;Yihui Xie&quot;
   date: &quot;`r Sys.Date()`&quot;
   site: bookdown::bookdown_site
   output: bookdown::gitbook
   documentclass: book
   bibliography: [book.bib, packages.bib]
   biblio-style: apalike
   link-citations: yes
   github-repo: rstudio/bookdown-demo
   description: &quot;This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook.&quot;
   ```{r}
   colorize &lt;- function(x, color) {
   if (knitr::is_latex_output()) {
   sprintf(&quot;\\textcolor{%s}{%s}&quot;, color, x)
   } else if (knitr::is_html_output()) {
   sprintf(&quot;&lt;span style='color: %s;'&gt;%s&lt;/span&gt;&quot;, 
   color,
   x)
   } else x
   }
   ```
   ---
  </code></pre>
  <p>this is clearly the wrong path because of error message</p>
  <pre><code> Error in yaml::yaml.load(..., eval.expr = TRUE) : 
     Scanner error: while scanning for the next token 
  </code></pre>
  <p>However, if I try to put the colorize function within the body of an Rmd file such as</p>
  <pre><code> # Prerequisites
   ```{r}
   colorize &lt;- function(x, color) {
   if (knitr::is_latex_output()) {
   sprintf(&quot;\\textcolor{%s}{%s}&quot;, color, x)
   } else if (knitr::is_html_output()) {
   sprintf(&quot;&lt;span style='color: %s;'&gt;%s&lt;/span&gt;&quot;, 
   color,
   x)
   } else x
   }
   ```
   This is a _sample_ book written in **Markdown** with `r colorize(&quot;desired red text&quot;, &quot;red&quot;)`.
  </code></pre>
  <p>this appears somewhat more promising as (1) it does compile and (2) the desired text is red.  However, the colorize function actually appears in the book (since it's in the body of the markdown) and the colorize function only works within the same chapter in which it is placed.</p>
  <p>Obviously the goal is to have the colorize function <em>work</em> while <em>not actually appearing</em> in the rendered book, and we also want the color to work across all chapters.</p>