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 the 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.

0 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:


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.

1 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.

1.0 Installing Google Go Language

In Ubuntu 16.04, golang can be installed from PPA.

$ 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

1.1 Installing Golang Packages

To compile zhmakeindex, these Go packages should be installed:

$ 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:

$ 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 .

1.2 Installing Zhmakeindex

$ 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

2 How to Use Zhmakeindex

Run zhmakeindex in your terminal and will see:

$ 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:

$ 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

3 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.

3.0 Specific Package Commands

\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.

%%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@}} 

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

\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 

3.1 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.

(markup-index :open  "\begin{theindex} ~n
          :close "~n~n\end{theindex}~n"
;; 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          ", "
;; delim_1          ", "
;; delim_2          ", "
(markup-locclass-list :open "\hfill\linfont " :sep ", ")
;; delim_n          ", "
(markup-locref-list   :sep ", ")
;; delim_r          "-"
(markup-range :sep "-")

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

texindy -M cj.xdy cj.idx

The output file will be like below:

xdy style output

3.2 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.

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:

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

The output file will be like below:

ist style output

4 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.

\newpagestyle{mipage}{ %%index chapter
                [{\profonta Appendices—\chaptertitle}]%even head
            {{\profonta Appendices—\chaptertitle}}
                {{\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

   Reprint policy

《Zhmakeindex and the Indices Creating of Chinese Books》 by Mèng Xiànjūn is licensed under a Creative Commons Attribution 4.0 International License
Tikz Examples in Re-composing McQuail's Textbook Tikz Examples in Re-composing McQuail's Textbook
Last month re-composed the classic Textbook, McQuail’s Mass Communication Theory, the following are some tikz examples i
蒼頡檢字法字根典型字選 蒼頡檢字法字根典型字選
博主按: 蒼頡檢字法,坊間亦稱「六代倉頡」,但其與通常所謂「三代倉頡」、「五代倉頡」差異較大,目的非僅供漢字輸入,而在完整解決漢字檢索及應用問題。 因蒼頡檢字法至今並無所謂「官方」版本釋出,據稱「原本並不公開,僅供與『朱邦復工作室』有工