15

I want to use index in Vietnamese Alphabetically. The order must be a, b, c, d, đ, e, ê. But in following code. That order is wrong. The first is "Ê", not "A", the second is "Đ", not "B". How to arrange that order?enter image description here

This is my code

\documentclass{book}
\usepackage{imakeidx}
\makeindex[columns=3]
\usepackage[utf8]{vietnam}
\begin{document}

\chapter{Hello}

Some text.\index{Apple}\index{Apricot}\index{Avocado}\index{Banana}
\index{Bilberry}\index{Blackberry}\index{Blackcurrant}\index{Blueberry}
\index{Currant}\index{Cherry}\index{Cherimoya}\index{Clementine}
\index{Date}\index{Damson}\index{Dragonfruit}\index{Durian}
\index{Eggplant}\index{Elderberry}\index{Feijoa}\index{Gooseberry}
\index{Grape}\index{Grapefruit}\index{Guava}\index{Huckleberry}
\index{Jackfruit}\index{Jambul}\index{Kiwi fruit}\index{Kumquat}
\index{Legume}\index{Lemon}\index{Lime}\index{Lychee}\index{Mandarine}
\index{Mango}\index{Melon}\index{Nectarine}\index{Orange}\index{Peach}
\index{Pear}\index{Pitaya}\index{Physalis}\index{Plum}\index{Pineapple}
\index{Pomegranate}\index{Purple Mangosteen}\index{Raisin}\index{Raspberry}
\index{Rambutan}\index{Redcurrant}\index{Salal berry}\index{Satsuma}
\index{Star fruit}\index{Strawberry}\index{Tangerine}\index{Tomato}
\index{Ugli fruit}\index{Watermelon}\index{Ziziphus mauritiana}
\index{Đồng biến}
\index{Nghịch biến}
\index{Dao động điều hòa}
\index{Ếch}
\printindex
\end{document}
15

As Egreg said, makeindex, which is used with imakeidx to produce index by default, doesn't support unicode. There is support for vietnamese in xindy, the other index processor. Unfortunately, there is some problem with pdflatex, Vietnamese language and xindy, because index entries are written as TeX sequences, not utf-8 codes, and xindy is unable to sort them correctly. This doesn't happen with other languages, so it is probably a bug. Nevertheless, you can switch to xelatex, which is good idea for Asian language anyway, and the index is then processed correctly, I hope.

Modified preamble:

\documentclass{book}
\usepackage{polyglossia}
\setmainlanguage{vietnamese}
\usepackage[xindy]{imakeidx}
\makeindex[columns=3,options={-M lang/vietnamese/utf8-lang}]    

For xelatex, use polyglossia package instead of vietnam, we use xindy option for imakeidx to avoid makeindex usage and options={-M lang/vietnamese/utf8-lang} will use Vietnamese sorting.

compile with xelatex -shell-escape filename. the -shell-escape option enables compilation of the index directly when your document is compiled. if you are not using the command line to compile your document, you must configure your editor to use this option.

The result:

Vietnamese index

Edit: As workaround for pdflatex, I wrote simple filter which convert control sequences generated by inputenc with utf8 option to utf8 characters.

iec2utf.lua:

kpse.set_program_name("luatex")

local cmd_arg = [[
iec2utf.lua - Script for converting LaTeX LICR codes to utf8 characters
Usage: cat filename | texlua iec2utf.lua > newfilename

Parameters:
  comma separated list of LaTeX font encodings used in the document
]]

local enc = {}

local licrs = {}
local codepoint2utf = unicode.utf8.char 

function load_encodings(f)
  local file= io.open(f,"r")
  local encodings = file:read("*all")
  file:close()
  for codepoint, licr in encodings:gmatch('DeclareUnicodeCharacter(%b{})(%b{})') do
    local codepoint = codepoint2utf(tonumber(codepoint:sub(2,-2),16))
    local licr= licr:sub(2,-2):gsub('@tabacckludge','')
    licrs[licr] = codepoint
  end
end

function sanitize_licr(l)
  return l:gsub(" (.)",function(s) if s:match("[%a]") then return " "..s else return s end end):sub(2,-2)
end
if arg[1] == nil then
  enc = {"T1"}
else
  for _,n in pairs(arg) do
    enc[#enc+1] = n
  end
end

for _,e in pairs(enc) do
  local filename = e:lower() .. "enc.dfu"
  local dfufile = kpse.find_file(filename)
  if dfufile then
    load_encodings(dfufile)
  end
end

local input = io.read("*all")

local cache = {}

local output = input:gsub('\\IeC[%s]*(%b{})',function(iec)
  local code = cache[iec] or licrs[sanitize_licr(iec)] or '\\IeC '..iec
  -- print(iec, code)
  cache[iec] = code
  return code
end)

io.write(output)

save this script to the same directory as your tex file. This script takes as parameter used font encoding, which is T5 in the case of Vietnamese.

if you are using Windows, you can create batch script to use this filter:

vietxindy.bat:

@echo of
type %~n1.idx | texlua iec2utf.lua T5 | texindy -i -M lang/vietnamese/utf8-lang -o %~n1.ind

for Linux, create shell script vietxindy:

#!/usr/bin/env sh
texlua iec2utf.lua T5 < `basename $1 .tex`.idx | texindy -i -M lang/vietnamese/utf8-lang -o `basename $1 .tex`.ind 

And then compile the document with:

pdflatex filename 
vietxindy filename
pdflatex filename

from the command line

  • I can not compile with xelatex -shell-escape filename. Please help me. Thank you. – minthao_2011 Sep 3 '13 at 14:56
  • @minthao_2011 doest it fail with some error message? – michal.h21 Sep 3 '13 at 15:13
  • No. I don't know how to compile with xelatex -shell-escape filename. – minthao_2011 Sep 3 '13 at 15:57
  • @minthao_2011 do you compile from the command line, or directly from your text editor? – michal.h21 Sep 3 '13 at 18:01
  • I get the warning Package expl3 Error: Mismatched LaTeX3 packages detected – minthao_2011 Sep 3 '13 at 23:19
6

Here's another possible solution that uses glossaries instead:

\documentclass{book}
\usepackage[utf8]{vietnam}

\usepackage[xindy={codepage=utf8,language=vietnamese},nopostdot]{glossaries}
\usepackage{glossary-mcols}

\glossarystyle{mcolindex}
\renewcommand{\glsmcols}{3}

\makeatletter
\newcommand*{\glsindex}[1]{%
  \def\theglslabel{#1}%
  \def\theglsname{#1}%
  \@onelevel@sanitize\theglslabel
  \ifglsentryexists{\theglslabel}%
  {%
  }%
  {%
    \newglossaryentry{\theglslabel}%
      {name={#1},text={\expandonce\theglsname},description={}}%
  }%
  \glsadd{\theglslabel}%
}
\makeatother

\makeglossaries

\begin{document}

\chapter{Hello}

Some text.\glsindex{Apple}\glsindex{Apricot}\glsindex{Avocado}\glsindex{Banana}
\glsindex{Bilberry}\glsindex{Blackberry}\glsindex{Blackcurrant}\glsindex{Blueberry}
\glsindex{Currant}\glsindex{Cherry}\glsindex{Cherimoya}\glsindex{Clementine}
\glsindex{Date}\glsindex{Damson}\glsindex{Dragonfruit}\glsindex{Durian}
\glsindex{Eggplant}\glsindex{Elderberry}\glsindex{Feijoa}\glsindex{Gooseberry}
\glsindex{Grape}\glsindex{Grapefruit}\glsindex{Guava}\glsindex{Huckleberry}
\glsindex{Jackfruit}\glsindex{Jambul}\glsindex{Kiwi fruit}\glsindex{Kumquat}
\glsindex{Legume}\glsindex{Lemon}\glsindex{Lime}\glsindex{Lychee}\glsindex{Mandarine}
\glsindex{Mango}\glsindex{Melon}\glsindex{Nectarine}\glsindex{Orange}\glsindex{Peach}
\glsindex{Pear}\glsindex{Pitaya}\glsindex{Physalis}\glsindex{Plum}\glsindex{Pineapple}
\glsindex{Pomegranate}\glsindex{Purple Mangosteen}\glsindex{Raisin}\glsindex{Raspberry}
\glsindex{Rambutan}\glsindex{Redcurrant}\glsindex{Salal berry}\glsindex{Satsuma}
\glsindex{Star fruit}\glsindex{Strawberry}\glsindex{Tangerine}\glsindex{Tomato}
\glsindex{Ugli fruit}\glsindex{Watermelon}\glsindex{Ziziphus mauritiana}
\glsindex{Đồng biến}
\glsindex{Nghịch biến}
\glsindex{Dao động điều hòa}
\glsindex{Ếch}

\printglossary[title=\indexname]
\end{document}

Result:

Image of index

Requires (pdf)latex, makeglossaries, (pdf)latex

Edit:

Further information on using the glossaries package:

0

This is my way, for example, if I want to sort ếch, I typed \index{eesch@ếch}

\documentclass{book}
\usepackage{imakeidx}
\makeindex[columns=2]
\usepackage[utf8]{vietnam}
\begin{document}
\chapter{Hello}
\index{Uwsnd dujng@Ứng dụng}
\index{ddoofng bieesn@đồng biến}
\index{nghijch bieesn@nghịch biến}
\index{Dao ddoojng ddieefu hoaf@Dao động điều hòa}
\index{eesch@ếch}
\printindex
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.