rmarkdown-cookbook
rmarkdown-cookbook copied to clipboard
[Request] Font color modification with a Pandoc lua filter for Beamer and revealjs
* This is just a kind of feature request and not an urgent issue (Is it appropriate to post such a comment here on this GitHub repo?).
In the section you wrote about the font colour modification with a Pandoc lua filter, you included a lua filter which contains the following line:
elseif FORMAT:match 'latex'
I would like to suggest that the following line would be more practical for those who want to knit with LaTeX/Beamer option than the original script I mentioned above:
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
(My example is inspired by an answer on Stack Overflow)
The motivation of my post here is that some rmarkdown
beginners like me might wrongly think that LaTeX and Beamer are the same output format of Pandoc (or other relevant programmes), since Beamer is one of the documentclass
es of LaTeX. However, actually these programmes treat Beamer as a different format from LaTeX and the original example is not applicable for Beamer output.
The similar 'confusion' might occur when producing revealjs. So, the line can be replaced with:
if FORMAT:match 'html' or FORMAT:match 'revealjs' then
as the ansewerer of the SO post I linked here suggests.
Hi @CLRafaelR,
Thanks for this great suggestion! This Cookbook contains mainly examples to help others build on it, but if we can make it more clear from the beginning, it would be better.
Did you test the Lua filter with this modification already ?
If it works as is with this change, we can surely add this or
on the current page.
If it requires modification of the Lua filter logic, we'll do it for next version of the book.
Thanks again!
Yes, I've already tested the lua filter. The following Rmd is my MWE.
---
title: "Changing the font color"
author: "Masataka Ogawa (CLRR)"
output:
bookdown::html_document2:
base_format: "function(..., number_sections) revealjs::revealjs_presentation(...)"
pandoc_args:
- "--lua-filter=color-text.lua"
self_contained: false
bookdown::beamer_presentation2:
pandoc_args:
- "--lua-filter=color-text.lua"
always_allow_html: yes
link-citations: yes
---
## 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(span)
color = span.attributes['color']
-- if no color attribute, return unchange
if color == nil then return span end
-- tranform to <span style="color: red;"></span>
if FORMAT:match 'html' or FORMAT:match 'revealjs' then
-- remove color attributes
span.attributes['color'] = nil
-- use style attribute instead
span.attributes['style'] = 'color: ' .. color .. ';'
-- return full span element
return span
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
-- remove color attributes
span.attributes['color'] = nil
-- encapsulate in latex code
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
table.insert(
span.content,
pandoc.RawInline('latex', '}')
)
-- returns only span content
return span.content
else
-- for other format return unchanged
return span
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"}.