Zhmakeindex and the Indices Creating of Chinese Books

In some big documents, for example The Cangjie Chinese-English Dictionary, it is necessary both for authors and readers to create one or two keywords lists in book appendices. These lists can help to find information quite efficiently. In this article, I introduce the zhmakeindex program that aims to sort Chinese index entries.

Gen­er­a­ting LaTeX Indices Separately

With the LaTeX in­dex gen­er­a­tor makeindex, an index can be produced. If you want to create multiple indices in a book, you should use the imakeidx package.

In The Cangjie Chinese-English Dictionary, there are six or seven indices in appendices, and the LaTeX package imakeidx was used. “The pack­age en­ables the user to pro­duce and type­set one or more in­dexes si­mul­ta­ne­ously with a doc­u­ment.”

This package is invoked as usual by means of a \usepackage command in preamble.tex:

1
\usepackage[noautomatic]{imakeidx}

The package option noautomatic disables the automatic splitting of the raw index files and running of the system programs. And then We can use the LaTeX index processor texindy and zhmakeindex simultaneously to make Chinese indices and Other Indices separately.

How to Install Zhmakeindex in Mainland China

Zhmakeindex is a program for making Chinese indices in a document generated with LaTeX. It is written by Leo Liu in Google Go language, and released with statically linked binary code. To run zhmakeindex, we should first install Google Go language, and then compile zhmakeindex from the source code.

Installing Google Go Language

In Ubuntu 16.04, golang can be installed from PPA.

1
2
3
4
5
6
7
8
9
10
11
$ sudo add-apt-repository ppa:ubuntu-lxc/lxd-stable
$ sudo apt update
$ sudo apt install golang-go
$ go version
go version go1.6.2 linux/amd64
$ mkdir ~/go
$ emacs ~/.zshrc
export GOPATH=$HOME/go
export PATH=$HOME/bin:$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin
$ exit

Installing Golang Packages

To compile zhmakeindex, these Go packages should be installed:

1
2
$ go get golang.org/x/text
$ go get github.com/yasushi-saito/rbtree

I do love our Great Country undoubtedly. But there is a wall named The Great Firewall of China, if you can’t climb over the GFW, to install these packages will make you go insane. You can build a door to reach the source pages, for exemple shadowsocks and goagent.

If you can’t climb over the Wall, here is a way to install these packages:

1
2
3
4
$ go get github.com/golang/text
$ mkdir -p ~/go/src/golang.org/x
$ cd ~/go/src/golang.org/x
$ ln -s ~/go/src/github.com/golang/text .

Installing Zhmakeindex

1
2
3
4
5
$ mkdir -p ~/go/src/github.com/leo-liu
$ cd ~/go/src/github.com/leo-liu
$ git clone https://github.com/leo-liu/zhmakeindex.git
$ cd zhmakeindex
$ ./install.sh

How to Use Zhmakeindex

Run zhmakeindex in your terminal and will see:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ zhmakeindex
用法:
zhmakeindex [-c] [-i] [-o <ind>] [-q] [-r] [-s <sty>] [-t <log>]
[-enc <enc>] [-senc <senc>] [-strict] [-z <sort>]
[<输入文件1> <输入文件2> ...]
中文索引处理程序
选项 默认值 说明
-c false 忽略条目首尾空格
-enc utf-8 读写索引文件的编码
-i false 从标准输入读取
-o 无 输出文件
-q false 静默模式,不输出错误信息
-r false 禁用自动生成页码区间
-s 无 格式文件名
-senc utf-8 格式文件的编码
-strict false 严格区分不同 encapsulated 命令的页码
-t 无 日志文件名
-z pinyin 中文分组排序方式,可以使用 pinyin (reading)、bihua (stroke) 或 bushou (radical)
版本:devel-1f89f3a(2016-07-29) 作者:刘海洋<leoliu.pku@gmail.com>

Usage of zhmakeindex is similar to makeindex. Just cd your file path and run command in terminal:

1
2
3
$ zhmakeindex youridxfile.idx %%using default .ist file
$ zhmakeindex -s youistfile.ist youridxfile.idx %%using your personal style file
$ zhmakeindex -s youistfile.ist -z bushou youridxfile.idx %%sorting index entries by chinese characters stroke order

The Layout of Index Entries

Package imakeidx can produce multiple indices and defines some others. For instance, the following commands may be used in preamble.tex file. For detailed information see the imakeidx package documentation.

Specific Package Commands

1
2
3
4
5
6
7
8
\usepackage[noautomatic]{imakeidx}
\makeindex[name=ec,title=英漢索引,intoc,columns=5,columnseprule=true,columnsep=5pt,options={-s ec.ist}]
\makeindex[name=th,title=類詞索引,intoc,columns=5,columnseprule=true,columnsep=5pt,options={-s th.ist}]
\makeindex[name=zb,title=字辨索引,intoc,columns=5,columnseprule=true,columnsep=5pt,options={-s zb.ist}]
\makeindex[name=gy,title=國音索引,intoc,columns=5,columnseprule=true,columnsep=5pt,options={-s gy.ist}]
\makeindex[name=yy,title=粵音索引,intoc,columns=5,columnseprule=true,columnsep=5pt,options={-s yy.ist}]
\makeindex[name=bh,title=總畫索引,intoc,columns=6,columnseprule=true,columnsep=5pt,options={-M bh.xdy}]
\makeindex[name=cj,title=倉頡索引,intoc,columns=6,columnseprule=true,columnsep=5pt,options={-M cj.xdy}]

And then adjust vertical space and hangindent sapce of index entries in preamble.tex.

1
2
3
4
5
6
7
8
9
10
11
\makeatletter
%%define index items layout
\def\@idxitem{\par\addvspace{7\p@ \@plus 3\p@ \@minus 3\p@}\hangindent 17\p@}
%%define index subitems layout
\def\subitem{\par\hangindent 0\p@ \hspace*{0\p@}}
%%define index subsubitems layout
\def\subsubitem{\par\hangindent 0\p@ \hspace*{0\p@}}
%%define vspace above lettergroup name
\def\indexspace{\par\addvspace{12\p@ \@plus 2\p@ \@minus 2\p@}}
\patchcmd\theindex{\indexname}{\indexname\vspace{5pt}}{}{}
\makeatother

We can type below commands into our .tex file bottom:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
......
\phantomsection
\addcontentsline{toc}{part}{辭典檢索}
\backmatter
\onecolumn
\pagestyle{mipage}
\printindex[zb] % zhmakeindex -s zb.ist -z pinyin ec.idx
\printindex[ec] % zhmakeindex -s ec.ist -z pinyin ec.idx
\printindex[th] % zhmakeindex -s th.ist -z pinyin th.idx
\printindex[bh] % texindy -M bh.xdy bh.idx
\printindex[cj] % texindy -M cj.xdy cj.idx
\printindex[yy] % zhmakeindex -s yy.ist -z bihua yy.idx
\printindex[gy] % zhmakeindex -s gy.ist -z bihua gy.idx
\end{document}

Editing and Using .xdy style files

Some indices need to generate with texindy and in some cases we should edit personal .xdy files. Below is an example of a .xdy file. Here can find more detailed information about the usage of texindy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(markup-index :open  "\begin{theindex} ~n
\providecommand*\lettergroupDefault[1]{}
\providecommand*\lettergroup[1]{%
\par{\color{black}\HGBM\centering~——{\fontsizec{1.2cm}\ABCFONTU{#1}}——}\par\vspace{-0.5em}
\nopagebreak
}"
:close "~n~n\end{theindex}~n"
:tree)
;; The indexentries (item_<..> specifiers)
(markup-indexentry :open "~n \item \indexmark " :depth 0)
(markup-indexentry :open "~n \linfont\subitem " :depth 1)
(markup-indexentry :open "~n \linfont\subsubitem " :depth 2)
;; Location-references
;; delim_0 <string> ", "
;; delim_1 <string> ", "
;; delim_2 <string> ", "
(markup-locclass-list :open "\hfill\linfont " :sep ", ")
;; delim_n <string> ", "
(markup-locref-list :sep ", ")
;; delim_r <string> "-"
(markup-range :sep "-")

Then we can run texindy in terminal with this style file:

1
texindy -M cj.xdy cj.idx

The output file will be like below:

xdy style output

Editing and Using .ist style files

For sorting Chinese characters in .idx file, we can run command zhmakeindex in terminal and in some cases we should edit personal .ist files. Below is an example of a .ist file. Here can find more detailed information about the usage of zhmkeindex.

1
2
3
4
5
6
7
delim_0 "\\hfill "
delim_1 "\\hfill "
delim_r "-"
headings_flag 1
heading_prefix "\\nopagebreak\\par\n{\\abcfontu\\fontsizec{1.1cm}\\centering——{" heading_suffix "}——}\\vspace*{0em}\\nopagebreak\n"
item_x1 "} \n \\linfont\\subitem " % Provide the right brace for level_0 items when they have no page number:
item_0 "\n \\ubufont\\centering\\item \\indexmark{"%added this with its usual default to mark it up

Then run zhmakeindex in terminal with this style file:

1
zhmakeindex -s gy.ist -z bihua gy.idx

The output file will be like below:

ist style output

Another Example on Index Page Layout

With LaTaX package titleps we can set \sectionmark to the index entry. Below is an example that index entries are marked to page header.

1
2
3
4
5
6
7
8
9
10
11
12
\newcommand{\indexmark}[1]{\sectionmark{#1}#1}
\newpagestyle{mipage}{ %%index chapter
\setheadrule{0.7pt}
\sethead[{\toptitlemarks\sectiontitle}---{\bottitlemarks\sectiontitle}]
[\linfontb\thepage]
[{\profonta Appendices—\chaptertitle}]%even head
{{\profonta Appendices—\chaptertitle}}
{\linfontb\thepage}
{{\toptitlemarks\sectiontitle}---{\bottitlemarks\sectiontitle}}%odd head
\setfoot[][][] %even foot
{}{}{} %odd foot
}

Then add \indexmark to .ist file or .xdy file as above .ist or .xdy example. Finally the output file will be like below:

indexmark style output

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器