# 4Colours in equations

$\definecolor{wongBlue}{RGB}{0, 114, 178}$

## 4.1 Colors defined in LaTeX xcolor package

Let us say we have a quarto book we are working on and we would like the $$\beta$$ term in the following equation to appear in blue: $y = X\beta + \varepsilon$ The following solution is easy:

$$y = X{\color{blue}\beta} + \varepsilon$$

$y = X{\color{blue}\beta} + \varepsilon$

## 4.2 Colors defined by the user

However, what if we want the color to be a different blue, one that we define? For example, what if we want the color whose hexadecimal code is #0072B2?

1. In the qmd file in which the equation appears, we need to define the color using a LaTeX definition. Note that the RGB code is required. For some reasons I do not understand, we cannot use the HTML model.

$$\definecolor{wongBlue}{RGB}{0, 114, 178}$$
2. Write your equation with the newly-defined color

$$y = X{\color{wongBlue}\beta} + \varepsilon$$

$y = X{\color{wongBlue}\beta} + \varepsilon$

If the final document is a PDF file rendered by LaTeX, an additional step is required:

1. In the YAML, the LaTeX colours need to be defined as well. For example, you can do as follows:

pdf:
documentclass: scrreprt
- text: |
\usepackage{xcolor}
\definecolor{wongBlue}{RGB}{0, 114, 178}

As of today (May 2023), the definition of the colours must be made in each qmd file:

$$\definecolor{wongBlue}{RGB}{0, 114, 178}$$

## 4.3 Colouring words

Now, what is we want to colour some words in the text, to match with the equations? Let us say that we want to describe the coefficient $$\beta$$ from the above example.

Let us adapt the solution that was proposed in the Rmarkdown cookbook by Yihui Xie, Christophe Dervieux, and Emily Riederer (See Chapter 5).

wongBlue <- "#0072B2"

colorize <- function(x, color) {
if (knitr::is_latex_output()) {
if (grep(x = color, "^#")) {
color <- deparse(substitute(color))
}
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color,
x)
} else x
}

This piece of code, as suggested by dj_a on stackoverflow can be saved in an R script, let us name it _myfunctions.R, and included in each qmd file that needs word to be be highlighted by a user-defined colour:

{r}
#| echo: false
#| eval: true
#| message: false
#| warning: false
#| file: _myfunctions.R


Then all we need to do is call the colorize() function as an inline code:

The r colorize("vector of coefficient", wongBlue),
$\color{wongBlue}\beta$, is to be estimated, with OLS.

The vector of coefficient, $$\color{wongBlue}\beta$$, is to be estimated, with OLS.

## 4.4 Colors in equations with revealjs

First, we need to create a file that will contain the defined colors for mathjax. Let us name that file mathjax-color.html. Here is its content:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({ TeX: { extensions: ["color.js"] }});
</script>
<script type="text/x-mathjax-config">
MathJax.Extension["TeX/color"].colors["wongBlack"] = '#000000';
MathJax.Extension["TeX/color"].colors["wongGold"] = '#E69F00';
MathJax.Extension["TeX/color"].colors["wongLightBlue"] = '#56B4E9';
MathJax.Extension["TeX/color"].colors["wongGreen"] = '#009E73';
MathJax.Extension["TeX/color"].colors["wongYellow"] = '#F0E442';
MathJax.Extension["TeX/color"].colors["wongBlue"] = '#0072B2';
MathJax.Extension["TeX/color"].colors["wongOrange"] = '#D55E00';
MathJax.Extension["TeX/color"].colors["wongPurple"] = '#CC79A7';

MathJax.Extension["TeX/color"].colors["IBMBlue"] = '#648FFF';
MathJax.Extension["TeX/color"].colors["IBMPurple"] = '#785EF0';
MathJax.Extension["TeX/color"].colors["IBMMagenta"] = '#DC267F';
MathJax.Extension["TeX/color"].colors["IBMOrange"] = '#FE6100';
MathJax.Extension["TeX/color"].colors["IBMYellow"] = '#FFB000';
});
</script>

Then, in the YAML of the presentation file, we need to include that file:

format:
revealjs:
theme: [dark, custom.scss]
- file: mathjax-color.html
That’s it, we can use \color{} inside equations in the presentation.