在 Bookdown 中,当我尝试使用 Pandoc 编译 epub 书籍时无法识别 `\textcolor` [英] In Bookdown, `\textcolor` is not recognized when I attempt to compile an epub book using Pandoc

查看:56
本文介绍了在 Bookdown 中,当我尝试使用 Pandoc 编译 epub 书籍时无法识别 `\textcolor`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 bookdown 中,当我尝试使用

In bookdown, it appears that \textcolor in latex is not recognized when I try to compile using

bookdown::render_book('bookdown::epub_book')

即使我已将 \usepackage{xcolor} 添加到 preamble.tex 文件中.

even though I have added \usepackage{xcolor} into the preamble.tex file.

这有什么原因吗?

推荐答案

我们来看看文档的处理方式.我将省略 knitr 和 R Markdown 包的处理,专注于 pandoc,这是从 Markdown 转换为任何其他格式的最后一步.

Let's have a look at the way the document is processed. I will leave out processing by the knitr and R Markdown packages and focus on pandoc, which is the final step when converting from Markdown to any other format.

Pandoc 首先将文档解析为内部的中间格式.我们可以通过选择 native 作为目标格式来检查该格式.因此,当我们将 \textcolor{red}{Mars} 提供给 pandoc 时,它会向我们显示原生表示:

Pandoc first parses the document into an internal, intermediate format. We can inspect that format by choosing native as the target format. So when we feed \textcolor{red}{Mars} to pandoc, it will show us the native representation:

$ printf '\\textcolor{red}{Mars}' | pandoc --from=markdown --to=native
[Para [RawInline (Format "tex") "\\textcolor{red}{Mars}"]]

这意味着,在不涉及太多细节的情况下,pandoc 将 \textcolor... 识别为嵌入 Markdown 的原始 LaTeX 命令.它不会尝试进一步解析 LaTeX,因为它被告知阅读 Markdown,而不是 LaTeX.在生成支持包含 LaTeX 的格式的输出时,此原始 LaTeX 片段将包含在输出中.pandoc 在生成 HTML/EPUB 时会忽略该片段,因为包含它不会产生预期的效果.

What this means is that, without going into too much detail, pandoc recognizes \textcolor... as a raw LaTeX command embedded into Markdown. It does not try to parse the LaTeX any further, as it was told to read Markdown, not LaTeX. This raw LaTeX snippet will be included in the output when producing output of a format which supports inclusion of LaTeX. The snippet is left out by pandoc when generating HTML/EPUB, as including it would not have the intended effect.

那么我们能否说服 pandoc 继续解析,pandoc 是否知道如何将 \textcolor 从 LaTeX 翻译成 HTML?

So can we convince pandoc to continue parsing, and does pandoc know how to translate \textcolor from LaTeX to HTML?

第二个问题很容易通过 pandoc 将 LaTeX 转换为 HTML 来回答:

The second question is easy to answer by converting the LaTeX to HTML via pandoc:

$ printf '\\textcolor{red}{Mars}' | pandoc --from=latex --to=html
<p><span style="color: red">Mars</span></p>

这看起来不错,所以下一步让 pandoc 解析原始 LaTeX 片段.Pandoc 有一个名为 Lua 过滤器 的功能,它允许我们以编程方式修改内部文档表示.这是一个将解析原始 LaTeX 命令的过滤器:

That looks good, so let's make pandoc parse the raw LaTeX snippets as the next step. Pandoc has a feature called Lua filters which allow us to modify the internal document representation programmatically. Here is such a filter which will parse the raw LaTeX commands:

function RawInline (el)
  if el.format == 'tex' then
    local blocks = pandoc.read(el.text, 'latex').blocks
    return blocks[1] and blocks[1].content or el
  end
end

将此代码保存到文件 parse-latex.lua 并使您的文档处理管道使用此文件:

Save this code to a file parse-latex.lua and make your document processing pipeline use this file:

---
output:
  bookdown::epub_book:
    pandoc_args: ['--lua-filter=/path/to/your/parse-latex.lua']
---

彩色文本现在应该显示在您的 epub 图书中.

The colored text should now show up in your epub book.

这篇关于在 Bookdown 中,当我尝试使用 Pandoc 编译 epub 书籍时无法识别 `\textcolor`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆