5.1 Font color

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:

  1. ---
  2. output: pdf_document
  3. ---
  4. 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.

5.1.1 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:

  1. colorize <- function(x, color) {
  2. if (knitr::is_latex_output()) {
  3. sprintf("\\textcolor{%s}{%s}", color, x)
  4. } else if (knitr::is_html_output()) {
  5. sprintf("<span style='color: %s;'>%s</span>", color,
  6. x)
  7. } else x
  8. }

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).

5.1.2 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:

  1. ---
  2. title: "Color text with a Lua filter"
  3. output:
  4. html_document:
  5. pandoc_args: ["--lua-filter=color-text.lua"]
  6. pdf_document:
  7. pandoc_args: ["--lua-filter=color-text.lua"]
  8. keep_tex: true
  9. ---
  10. First, we define a Lua filter and write it to
  11. the file `color-text.lua`.
  12. ```{cat, engine.opts = list(file = "color-text.lua")}
  13. Span = function(el)
  14. color = el.attributes['color']
  15. -- if no color attribute, return unchange
  16. if color == nil then return el end
  17. -- tranform to <span style="color: red;"></span>
  18. if FORMAT:match 'html' then
  19. -- remove color attributes
  20. el.attributes['color'] = nil
  21. -- use style attribute instead
  22. el.attributes['style'] = 'color: ' .. color .. ';'
  23. -- return full span element
  24. return el
  25. elseif FORMAT:match 'latex' then
  26. -- remove color attributes
  27. el.attributes['color'] = nil
  28. -- encapsulate in latex code
  29. table.insert(
  30. el.content, 1,
  31. pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
  32. )
  33. table.insert(
  34. el.content,
  35. pandoc.RawInline('latex', '}')
  36. )
  37. -- returns only span content
  38. return el.content
  39. else
  40. -- for other format return unchanged
  41. return el
  42. end
  43. end
  44. ```
  45. Now we can test the filter with some text in brackets with
  46. the `color` attribute, e.g.,
  47. > Roses are [red and **bold**]{color="red"} and
  48. > 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).


  1. If you are not familiar with cat code chunks, please see Section 15.6. We used this engine here to conveniently write out a chunk to a .lua file, so we do not have to manage the Lua script in a separate file color-text.lua. If you do not want to use the cat engine, you can definitely copy the Lua code and save it to a separate file, instead of embedding the Lua code in a code chunk.↩︎