Adapt lua filter in Rmarkdown for font color text with citations

Dear all, I was wondering if it's possible to use/readapt the following Lua filter in r-markdown to get font colours when using citations with r-markdown syntax. For example, when I try to replicate the code from R Markdown Cookbook and add a citation inside the [...] block, I get the following result:

The following R code can be used to replicate the same pdf document:

# packages
library(rmarkdown)

# Create a citation file
my_bib <- file.path(tempdir(), "my-bib.bib")
cit_R <- "
@Manual{R,
  title = {R: A Language and Environment for Statistical Computing},
  author = {{R Core Team}},
  organization = {R Foundation for Statistical Computing},
  address = {Vienna, Austria},
  year = {2021},
  url = {https://www.R-project.org/},
}
"
writeLines(cit_R, my_bib)

# Create an Rmd file with a reference
my_Rmd <- file.path(tempdir(), "my-Rmd.Rmd")
text_Rmd <- '
---
title: "Color text with a Lua filter"
output: 
  pdf_document: 
    pandoc_args: ["--lua-filter=color-text.lua"]
bibliography: my-bib.bib
---

```{cat, engine.opts = list(file = "color-text.lua")}
Span = function(el)
  color = el.attributes[\'color\']
  -- if no color attribute, return unchanged
  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
```

Roses are [red and **bold**]{color="red"}

Citation: ABC in @R

Col + Citation: [ABC in @R]{color="red"}

# Citations

<div id="refs"></div>
'
writeLines(text_Rmd, my_Rmd)

# render it
render(
  input = my_Rmd, 
  output_file = file.path(tempdir(), "test1.pdf")
)
browseURL(file.path(tempdir(), "test1.pdf"))

I also posted the same question in SO.

Hi,

Which rmarkdown version are you using ?
Which Pandoc version rmarkdown::pandoc_version() ?

This is working on my side

Thanks for your reply. I copy here the versions:

packageVersion("rmarkdown")
#> [1] '2.11'
rmarkdown::pandoc_version()
#> [1] '2.14.0.3'
rmarkdown::find_pandoc()
#> $version
#> [1] '2.14.0.3'
#> 
#> $dir
#> [1] "C:/Program Files/RStudio/bin/pandoc"

Created on 2021-12-02 by the reprex package (v2.0.1)

After a few test, this is an issue with Pandoc version. If I use 2.14.0.3 as you, this is not working.

But they fixed it seems.

You can either install a newer Pandoc and use it

Or download the daily version of RStudio which bundle a recent Pandoc that works

Thank you very much, that worked perfectly. Do you want to copy the same answer to SO?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.