The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words:
For HTML, we can wrap the text in the
<span>
tag and set color with CSS, e.g.,<span style="color: red;">text</span>
.For PDF, we can use the LaTeX command
\textcolor{}{}
. This requires the LaTeX package xcolor, which is included in Pandoc’s default LaTeX template.
As an example of changing the color in PDF text:
---
output: pdf_document
---
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
In the above example, the first set of curly braces contains the desired text color, and the second set of curly braces contains the text to which this color should be applied.
If you want to design an R Markdown document for multiple output formats, you should not embed raw HTML or LaTeX code in your document, because they will be ignored in the other output formats (e.g., LaTeX code will be ignored in HTML output, and HTML tags will be lost in LaTeX output). Next, we provide two possible methods to deal with this issue.
Using an R function to write raw HTML or LaTeX code
We can write a custom R function to insert the correct syntax depending on the output format using the is_latex_output()
and is_html_output()
functions in knitr as follows:
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color,
x)
} else x
}
We can then use the code in an inline R expression `r colorize("some words in red", "red")`
, which will create some words in red (you will not see the red color if you are reading this book printed in black and white).
Using a Pandoc Lua filter (*)
This method may be a little advanced for R users because it involves another programming language, Lua, but it is extremely powerful—you can programmatically modify Markdown elements via Pandoc’s Lua filters (see Section 4.20). Below is a full example:
---
title: "Color text with a Lua filter"
output:
html_document:
pandoc_args: ["--lua-filter=color-text.lua"]
pdf_document:
pandoc_args: ["--lua-filter=color-text.lua"]
keep_tex: true
---
First, we define a Lua filter and write it to
the file `color-text.lua`.
```{cat, engine.opts = list(file = "color-text.lua")}
Span = function(el)
color = el.attributes['color']
-- if no color attribute, return unchange
if color == nil then return el end
-- transform to <span style="color: red;"></span>
if FORMAT:match 'html' then
-- remove color attributes
el.attributes['color'] = nil
-- use style attribute instead
el.attributes['style'] = 'color: ' .. color .. ';'
-- return full span element
return el
elseif FORMAT:match 'latex' then
-- remove color attributes
el.attributes['color'] = nil
-- encapsulate in latex code
table.insert(
el.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
table.insert(
el.content,
pandoc.RawInline('latex', '}')
)
-- returns only span content
return el.content
else
-- for other format return unchanged
return el
end
end
```
Now we can test the filter with some text in brackets with
the `color` attribute, e.g.,
> Roses are [red and **bold**]{color="red"} and
> violets are [blue]{color="blue"}.
In this example, we implicitly used a Pandoc Markdown extension named bracketed_spans
, which allows us to write text with attributes, e.g., [text]{.class attribute="value"}
. The Lua filter defined in the cat
code chunk7 puts text in <span style="color: ..."></span>
if the output format is HTML, and in \textcolor{...}{}
if the output format is LaTeX. The Lua filter is written to a file color-text.lua
, and enabled through the command-line option --lua-filter
passed to Pandoc via the pandoc_args
option of the output formats.
Compared to the previous method, the advantage of using the Lua filter is that you can still use Markdown syntax inside the brackets, whereas using the R function colorize()
in the previous section does not allow Markdown syntax (e.g., colorize('**bold**')
will not be bold).