8 Advanced
8.1 Chinese Support
The support for Chinese typesetting with ‘bookdown’ is tricky. Briefly speaking, you need a LaTeX template which supports Chinese characters and styles. In Chapter 4 the template ‘yihui_zh’ was introduced. It is a nice template for daily use. However, you might encounter some problems when producing html files from the chapter headers which contains Chinese characters, especially those mixed with Latin letters. See the discussion between me and Yihui.30 One solution is to add {#identifier}
manually to those problematic headers. I tried this. It works fine if you do it when writing a new chapter, but it is boring if you do it after finishing a book. Another solution is using the ‘pinyin’ package (Zhao 2017d). A function called bookdown2py()
can convert the headers into pinyin (the official romanization system for Standard Chinese31) and add the #IDs automatically:
install.packages('pinyin')
library('pinyin')
?bookdown2py
More details about the ‘pinyin’ package can be found on my GitHub repo32.
‘bookdownplus’ gives your more options to create other documents in Chinese. For example, the ‘nte_zh’ template is designed for writing novels or proses which are not as structured as academic thesis:
bookdownplus(template = `nte_zh`)
The template name ‘nte’ is short for ‘Nothing to Envy’, the title of the book whose LaTeX code was the origin of this template. I have written a book with the ‘nte_zh’ template, which is online33.
If you are writing an academic article in Chinese, you can use the ‘article_zh’ template:
bookdownplus(template = 'article_zh')
You will get a file named ‘article_zh.pdf’ in ’_book/’ folder as an example (Fig. 8.1).
Similar to other templates, in your working directory you could now open ‘bookdownplus.Rproj’ with RStudio. You can write your own text in ‘index.Rmd’ and ‘body.Rmd’, and then press ‘ctrl+shift+b’ to build it.
The particular section of this demo file is the abstract. It is bilingual with different formats from the main body. You can revise your abstract in ‘abstract.tex’. It is self-explanatory, although written in LaTeX. I wish I could tailor it into markdown, but I don’t think it is necessary.
If these templates do not fulfil your request for academic thesis in Chinese, you could use the ‘theis_zju’ template:
bookdownplus(template = 'thesis_zju_zh')
You will get a file named ‘thesis_zju_zh.pdf’ in ’_book/’ folder as an example (Fig. 8.2).
Now you can write your own text in ‘index.Rmd’ and ‘body.Rmd’. There are much more fields in ‘index.Rmd’ for you owing to the complicated structure of the thesis. Be patient. It is only the beginning of a dissertation.
The postfix ‘zju’ is short for Zhejiang University, one of the top universities in China. I have never been there. I tailored it into ‘bookdownplus’ only because I found a LaTeX template of ZJU thesis and compiled it successfully. There are templates available for the thesis of other universities and institutes, such as Peking University34 and Tsinghua University.35 Unfortunately I failed in compiling these LaTeX templates on my computer. I am not a LaTeX expert. I wish these templates could perhaps be tailored and included in ‘bookdownplus’ in the future, if you can help and join me.
A combination of multiple templates could generate a new one, such as the ‘chemistry_zh’ template:
bookdownplus(template = 'chemistry_zh')
You will get a file named ‘chemistry_zh.pdf’ in ’_book/’ folder.
This template is the child parented by ‘yihui_zh’ and ‘chemistry’ templates. In this way you could customize your own template. A further description of creating your own templates is in Chapter 8.3.
Each of the templates which support Chinese characters has name ended with ’_zh’. To display these templates, run:
grep('_zh$', template(), value = TRUE)
## [1] "article_zh" "chemistry_zh" "nte_zh" "thesis_mypku_zh"
## [5] "thesis_zju_zh" "yihui_zh"
As a regular expression, $
means the end of a string.
8.2 Mind Map
Now you have your books or thesis done. It would be nice to show the outline of your book in a mind map. The mind map of this book is shown in Fig. 8.3.
Owing to the advantage of ‘bookdown’, you don’t have to draw such a mind map by hand. This mind map is automatically generated from the ‘body.Rmd’ file of the ‘R bookdownplus Textbook’ by using the R package ‘mindr’ (Zhao 2017c):
install.packages('mindr')
library('mindr')
?md2mm()
With the ‘mindr’ package you can easily extract the outline from your ‘bookdown’ file. More details about the ‘mindr’ package can be found on my GitHub repo36.
8.3 Create Your Own Templates
‘bookdownplus’ provides you around 20 templates (Version 1.2.0) and more will be included in the future. I am not an expert in LaTeX but I have successfully created the pdf templates described in this book. It means that creating a LaTeX template does not require much knowledge in LaTeX. If you know the general workflow of LaTeX, and study the ‘bookdownplus’ templates carefully, and read Chapter 4 of the official manual of ‘bookdown’37, you will get the idea of how to create your own templates.
Here is some hints from my experience on creating a new template from am existing LaTeX template:
Find a nice LaTeX template. There are many websites that provide free LaTeX templates. You can download one you like most. Those with good documentations and comments are highly recommended.
Compile the LaTeX template file to make sure that it can work fine and produce the right pdf file. You could either use command lines or use some software like TexStudio to compile it. You could send to me this template with its full documentation and your pdf file, if you do not want to continue by yourself. I would try tailoring it into ‘bookdownplus’ but it is not guaranteed. It depends on my time and mood. Thus I recommend you to be brave and continue the next steps.
Cut the template into ‘template_yours.tex’ and ‘index.Rmd’ in the following way:
The main body of the LaTeX are the part between
\begin{document}
and\end{document}
. Replace the main body with$body$
, which will be filled with ‘body.Rmd’. You can use any demo ’body*.Rmd’ created by ‘bookdownplus’.Use any ‘index.Rmd’ created by ‘bookdownplus’ and modify the name of the LaTex template in ‘index.Rmd’.
If the LaTeX template is simple enough, now you may build this template book with ‘bookdown’.
Usually it won’t work. A good-looking LaTeX template is most likely complicated, especially those in Chinese. Probably you have to modify the preamble, pick out some parts and save them and specify them in ‘index.Rmd’. See the official manual of ‘bookdown’38.
If you can successfully build your book with your new template, congratulations. It would be appreciated if you could send me your ‘index.Rmd’, ‘body.Rmd’, ‘template_yours.tex’ and other related files. I will add them into ‘bookdownplus’ templates and add your name into the contributor list.
Let’s build a ‘bookdownplus’ template library!
Bibliography
Zhao, Peng. 2017d. Pinyin: Convert Chinese Characters into Pinyin. https://CRAN.R-project.org/package=pinyin.
Zhao, Peng. 2017c. Mindr: Convert Files Between Markdown or Rmarkdown Files and Mindmaps. https://CRAN.R-project.org/package=mindr.