【问题标题】:Colorizing text in Rmarkdown fails: color-text.lua doesn't work in RevealJS在 Rmarkdown 中为文本着色失败:color-text.lua 在 RevealJS 中不起作用
【发布时间】:2020-04-17 15:29:44
【问题描述】:

我正在尝试通过r-markdown 制作revealjs 幻灯片。另外,我想使用lua 过滤器以简单的表示法(即类似降价的表示法)为一些文本着色,如建议的那样 R Markdown 食谱Using a Pandoc Lua filter

但是,生成的幻灯片没有上色。在下面的幻灯片中,单词 red 应该是 red 而 blue 应该是 blue,但实际上并非如此。

此外,简单的符号[blue]{color="blue"},例如,意外地转换为<span color="blue">blue</span>,而不是理想的HTML代码<span style="color: blue;">blue</span>

谁能告诉我我错过了什么...?

---
title: "title"
output: 
  bookdown::html_document2:
    base_format: "function(..., number_sections) revealjs::revealjs_presentation(...)"
    theme: moon
    pandoc_args: 
      - "--lua-filter=color-text.lua"
    transition: default
    background_transition: zoom
    center: true
    incremental: true
    number_sections: true
    toc: true
    toc_depth: 3
    fig_caption: TRUE
    #dev: cairo_pdf
    self_contained: false
    reveal_plugins: ["zoom", "notes", "menu"] #"search"
    reveal_options:
      slideNumber: true
      previewLinks: true
      margin: 0.1
      menu:
        numbers: true
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' 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' 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"}.

【问题讨论】:

    标签: html lua r-markdown reveal.js


    【解决方案1】:

    是的,我可以,你也可以:你的问题已经有了答案。 你是

    尝试做revealjs

    因此,当您运行代码时,您的 lua 过滤器不会产生任何有趣的东西,只是在倒数第二行执行 return span 而不是所需的更改。

    因此,一个简单的改变就是你的解决方法。
    替换:
    if FORMAT:match 'html' then

    if FORMAT:match 'html' or FORMAT:match 'revealjs' then
    这样做,lua 过滤器完成了它的工作,我得到了所需的输出,格式正确。

    【讨论】:

    • 感谢您的回答,对我迟到的回复感到抱歉。您的解决方案确实有效!我投票并接受了你的回答!
    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多