作者:Yihui Xie
译者:郑宝童
日期:2021.05.18
3.3 E-Books
3.3 电子书
Currently bookdown provides two e-book formats, EPUB and MOBI. Books in these formats can be read on devices like smartphones, tablets, or special e-readers such as Kindle.
目前bookdown 提供了两种电子书格式,EPUB和MOBI。这些格式的书籍可以在智能手机、平板电脑或Kindle等特殊电子阅读器上阅读。
3.3.1 EPUB
To create an EPUB book, you can use the
epub_book()
format. It has some options in common withrmarkdown::html_document()
:
你可使用 epub_book()
创建一个EPUB电子书. 它和 rmarkdown::html_document()
有一些相同的参数选项:
epub_book(fig_width = 5, fig_height = 4, dev = "png",
fig_caption = TRUE, number_sections = TRUE, toc = FALSE,
toc_depth = 3, stylesheet = NULL, cover_image = NULL,
metadata = NULL, chapter_level = 1, epub_version = c("epub3",
"epub", "epub2"), md_extensions = NULL, pandoc_args = NULL,
template = "default")
The option
toc
is turned off because the e-book reader can often figure out a TOC automatically from the book, so it is not necessary to add a few pages for the TOC. There are a few options specific to EPUB:
stylesheet
: It is similar to thecss
option in HTML output formats, and you can customize the appearance of elements using CSS.cover_image
: The path to the cover image of the book.metadata
: The path to an XML file for the metadata of the book (see Pandoc documentation for more details).chapter_level
: Internally an EPUB book is a series of “chapter” files, and this option determines the level by which the book is split into these files. This is similar to thesplit_by
argument of HTML output formats we mentioned in Section 3.1, but an EPUB book is a single file, and you will not see these “chapter” files directly. The default level is the first level, and if you set it to 2, it means the book will be organized by section files internally, which may allow the reader to load the book more quickly.epub_version
: Version 3 or 2 of EPUB.
toc
参数选项是关闭的,因为电子书阅读器通常可以自动从书中计算出toc,所以没有必要为toc添加几页。bookdown有一些特定于EPUB的参数选项:
stylesheet
: 它和HTML输出格式的css
参数选项有点想, 您可以使用CSS自定义元素的外观。.cover_image
: 书封面路径.metadata
: 书的metadata的XML文件的路径(请参阅Pandoc文档以获得更多细节)。chapter_level
: 在EPUB图书内部是一系列“章节”文件,该选项决定了图书被拆分为这些文件的级别。这类似于我们在3.1节中提到的HTML输出格式的split_by
参数,但是EPUB书籍是一个单独的文件,您不会直接看到这些“章节”文件。默认级别是第一级,如果您将其设置为2,这意味着图书将在内部按章节文件组织,这可能让读者更快地加载图书。epub_version
: Version 3 or 2 of EPUB.
An EPUB book is essentially a collection of HTML pages, e.g., you can apply CSS rules to its elements, embed images, insert math expressions (because MathML is partially supported), and so on. Figure/table captions, cross-references, custom blocks, and citations mentioned in Chapter 2 also work for EPUB. You may compare the EPUB output of this book to the HTML output, and you will see that the only major difference is the visual appearance.
EPUB书籍本质上是HTML页面的集合,例如,您可以对其元素应用CSS规则、嵌入图像、插入数学表达式(因为MathML得到了部分支持),等等。图/表标题、交叉引用、自定义块和第2章中提到的引用也适用于EPUB。您可以比较这本书的EPUB输出和HTML输出,您将看到唯一的主要区别是视觉外观。
There are several EPUB readers available, including Calibre (https://www.calibre-ebook.com), Apple’s iBooks, and Google Play Books.
有几种EPUB阅读器可供选择,包括Calibre (https://www.calibre-ebook.com)、苹果的iBooks和谷歌Play Books。
3.3.2 MOBI
MOBI e-books can be read on Amazon’s Kindle devices. Pandoc does not support MOBI output natively, but you may use third-party tools to convert EPUB to MOBI. One possible tool is Calibre. Calibre is open-source and free, and supports conversion among many more formats. For example, you can convert HTML to EPUB, Word documents to MOBI, and so on. The function
calibre()
in bookdown is a wrapper function of the command-line utilityebook-convert
in Calibre. You need to make sure that the executableebook-convert
can be found via the environment variablePATH
. If you use macOS, you can install Calibre with Homebrew (https://brew.sh) via the commandbrew cask install calibre
, so you do not need to worry about thePATH
issue.
MOBI电子书可以在亚马逊的Kindle设备上阅读。Pandoc本身不支持MOBI输出,但是您可以使用第三方工具将EPUB转换为MOBI。一个可用的工具是Calibre。Calibre是开源和免费的,并且支持更多格式之间的转换。例如,您可以将HTML转换为EPUB,将Word文档转换为MOBI,等等。bookdown中的函数 calibre()
是Calibre中的命令行工具ebook-convert的封装函数。您需要确保可执行的电子书转换可以通过环境变量PATH找到。如果你使用macOS,你可以通过brew cask install calibre
安装Calibre,所以您不需要担心PATH问题。