% \iffalse meta-comment
%
% Copyright (C) 1999 National Electronics and Computer Technology Center
% (NECTEC), Thailand.  All rights reserved.
% 
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Theppitak Karoonboonyanan.
% 
% This work consists of the files thai.dtx and thai.ins
% and the derived file thai.ldf.
% 
% \fi
% \CheckSum{272}
% \iffalse
%    Tell the \LaTeX\ system who we are and write an entry on the
%    transcript.
%<*dtx>
\ProvidesFile{thai.dtx}
%</dtx>
%<code>\ProvidesLanguage{thai}
%\fi
%\ProvidesFile{thai.dtx}
        [2011/03/17 v1.3 Thai support from the babel system]
%\iffalse
%% File `thai.dtx'
%% Thai Language Definition File
%% Copyright (C) 1999 - 2011
%%           by Surapant Meknavin,
%%              Theppitak Karoonboonyanan (thep at linux.thai.net),
%%              Chanop Silpa-Anan (chanop at debian.org),
%%              Poonlap Veerathanabutr (poonlap at linux.thai.net)
%%              Thai Linux Working Group
%%              http://linux.thai.net/
%%
%% Please report errors to: Theppitak Karoonboonyanan
%%                          (thep at linux.thai.net)
%%
%%   This file is part of the babel system, it provides the source
%%   code for the Thai language definition file.  The original version
%%   was written by Surapant Meknavin.
%%
%%   It is currently maintained by Theppitak Karoonboonyanan 
%%   (thep at linux.thai.net), Chanop Silpa-Anan (chanop at debian.org)
%%   and Poonlap Veerathanabutr (poonlap at linux.thai.net).
%
%<*filedriver>
\documentclass{ltxdoc}
\newcommand*\TeXhax{\TeX hax}
\newcommand*\babel{\textsf{babel}}
\newcommand*\langvar{$\langle \it lang \rangle$}
\newcommand*\note[1]{}
\newcommand*\Lopt[1]{\textsf{#1}}
\newcommand*\file[1]{\texttt{#1}}
\begin{document}
 \DocInput{thai.dtx}
\end{document}
%</filedriver>
%\fi
% \GetFileInfo{thai.dtx}
%
% \changes{thai-1.0d}{2000/05/09}{Theppitak Karoonboonyanan:
%    Encoding changed from TIS to LTH, according to TeX convention.
%    Let `norasi' be Thai roman font, `dbttx' Thai sans serif.
%    Correct some Thai translations.}
% \changes{thai-1.0d}{2001/12/04}{Theppitak Karoonboonyanan:
%    Add checks for \cs{thechapter} existence before redefining
%    digits as Thai.
%    Move thaitoday definitions into thainumber and remove
%    thainumber option.
%    Use \cs{ifcase} instead of nested \cs{if} in \cs{thaitranslate}.
%    Fix wrong encoding in heading for book style (as suggested by Chanop).}
% \changes{thai-1.0d}{2001/12/04}{Chanop Silpa-Anan:
%    Add thainumber and thaitoday option}
% \changes{thai-1.0d}{2002/01/16}{Theppitak Karoonboonyanan:
%    Fix extra spaces appearing in \cs{selectlanguage\{thai\}}
%    (as suggested by C. Visavakul)}
% \changes{thai-1.0d}{2003/01/07}{Poonlap Veerathanabutr:
%    Change `dbttx' to `dbtt'}
% \changes{thai-1.0d}{2003/02/26}{Theppitak Karoonboonyanan:
%    Run Thai number in part, subsubsection, paragraph,
%    subparagraph also}
% \changes{thai-1.0d}{2003/06/02}{Chanop Silpa-Anan:
%    Use `garuda' for default Thai sans serif since
%    `dbtt' is still slightly buggy}
% \changes{thai-1.0d}{2003/08/10}{Theppitak Karoonboonyanan:
%    Fix translation of \cs{prefacename}.
%    Redefine \cs{frontmatter} and \cs{mainmatter} for book class
%    so thainumber option applies to the page numbering styles.
%    (Experimental:) Add initialization of Thai character codes
%    to fix the \cs{MakeUppercase} and \cs{MakeLowercase} problem
%    also comment out the old workarounds.}
% \changes{thai-1.0d}{2004/02/02}{Poonlap Veerathanabutr:
%    Fix \cs{lccode} and \cs{uccode}}
% \changes{thai-1.0d}{2004/02/28}{Theppitak Karoonboonyanan:
%    Always use Thai alphabets for appendix (as suggested by K. chakkree01)}
% \changes{thai-1.0d}{2004/03/04}{Theppitak Karoonboonyanan:
%    Use `pmono' as default Thai ttfamily}
% \changes{thai-1.0d}{2004/03/06}{Theppitak Karoonboonyanan:
%    Use `garuda' instead of `dbtt' as default Thai sffamily}
% \changes{thai-1.0d}{2004/03/18}{Theppitak Karoonboonyanan:
%    Move \cs{frontmatter} and \cs{mainmatter} redefinition back under 
%    `thainumber' option (wrongly moved along with previous 
%    appendix fix)}
% \changes{thai-1.0d}{2004/04/08}{Theppitak Karoonboonyanan:
%    Borrow code from previous encoding fix to inhibit capitalizing 
%    chapters \& sections in book headers. (proposed by P'Joy)}
% \changes{thai-1.0d}{2004/11/15}{Theppitak Karoonboonyanan:
%    `pmono' is now `ttype'}
% \changes{thai-1.1}{2006/07/05}{Theppitak Karoonboonyanan:
%    Create `thai.dtx' for documenting and generating `thai.ldf'.
%    Remove unnecessary workarounds for inhibition of capitalization
%    of headers, as the catcodes already do the job.
%    Also remove the mysterious redefinitions of figure and table counters.}
% \changes{thai-1.2}{2009/07/27}{Theppitak Karoonboonyanan:
%    Re-encode Thai messages with unicode symbolic names, instead of explicit
%    TIS-620 bytes.  Adjust documentation to cover UTF-8 support.  Make
%    \cs{ttypist} the default typewriter font instead of \cs{ttype}.
%    Drop the obsolete \cs{\backslash{}thai} command.}
% \changes{thai-1.3}{2011/03/17}{Theppitak Karoonboonyanan:
%    Add \cs{\backslash{}textpali} macro for typesetting Pali text with
%    descender-less Yo Ying and Tho Than.  Update translation for 
%    \cs{\backslash{}refname}}
%
% \section{The Thai language}
%
% The file \file{\filename}\footnote{The file described in this section has 
% version number \fileversion\ and was last revised on \filedate. Contributions 
% were made by Theppitak Karoonboonyanan (\texttt{thep@linux.thai.net}).} 
% defines language-specific macros for Thai language.
%
% Thai documents to be supported by this language definition can be encoded
% in TIS-620 (or ISO/IEC 8859-11) or UTF-8 (with the aids of \texttt{inputenc}
% package).
%
% Thai is written continuously without word delimitors.  The word boundary 
% analysis is considered too complicated for rule-based approaches. 
% Most good quality word analysis tools available employ special matching
% algorithms against pre-defined dictionaries.  Some even use contextual 
% analysis for ambiguous cases based on statistics.  Therefore, no good 
% hyphenation pattern is found to be comparable with those tools yet.  
%
% \DescribeMacro{\wbr}
%   What the Thai language definition provides here for the word boundary 
%   problem is the |\wbr| command for separating words.  It occupies no 
%   space but aids the \TeX\ typesetter in wrapping lines at proper positions, 
%   meanwhile leaves some room for distributing little spaces between words 
%   in case of narrow columns.
%
% Some preprocessing tools that analyze word boundaries and insert the |\wbr| 
% commands are available, for example
%   \begin{itemize}
%   \item \emph{cttex} by Vuthichai Ampornaramveth
%   \item \emph{swath} by Phaisarn Charoenpornsawat, now maintained by Thai 
%         Linux Working Group, downloadable from:\\
%         \texttt{ftp://linux.thai.net/pub/thailinux/software/swath}
%   \end{itemize}
%
%   Some certain Thai documents use Thai digits instead of Arabic.
%   And Thai alphabetic numbering is also commonly used for appendices.
%   This file provides a number of numbering styles that can apply 
%   to \LaTeX\ |\pagenumbering| command:
%
%    \begin{itemize}
%    \item |\thainum| for plain Thai digits
%    \item |\thaibracenum| for Thai digits in parentheses
%    \item |\thaialph| for Thai alphabetic numbering, using a reduced set
%          (with three alphabets skipped: Kho Khuat, Kho Khon and Kho Rakhang)
%    \item |\thaiAlph| for Thai alphabetic numbering, using the full set
%          (with full alphabet set, plus two semi-vowels, Ru and Lu, in the
%          order defined in Thai official dictionary)
%    \end{itemize}
%
% \DescribeEnv{thainumber}
%   The above numbering styles can be automatically chosen with the 
%   \Lopt{thainumber} option.  When supplying this option to the \babel{}
%   package, Thai numberings will be applied to pages, sections, enumerations, 
%   dates, etc.
%
% \StopEventually{}
%
%    \subsection{Thai language definition}
%
%    The macro |\LdfInit| takes care of preventing that this file is
%    loaded more than once, checking the category code of the
%    \texttt{@} sign, etc.
%    \begin{macrocode}
%<*code>
\LdfInit\CurrentOption{captions\CurrentOption}
%    \end{macrocode}
%
%    When this file is read as an option, i.e. by the |\usepackage{babel}|
%    command, \texttt{thai} will be an `unknown' language in which
%    case we have to make it known.  So we check for the existence of
%    |\l@thai| to see whether we have to do something here.
%
%    \begin{macrocode}
\ifx\l@thai\@undefined
  \@nopatterns{thai}
  \adddialect\l@thai0\fi
%    \end{macrocode}
%
%    \subsubsection{Thai character encoding}
%
%    In this language definition, we support TIS-620, the national standard 
%    8-bit character encoding, and Unicode by means of \texttt{inputenc} 
%    package.
%
%    The encoding has been described in terms of TIS-620 as \texttt{LTH} in 
%    the \file{lthenc.def} file.  So, we require it.
%
%    \begin{macrocode}
\InputIfFileExists{lthenc.def}{%
  \message{Loading the definitions for the Thai font encoding}}{%
  \errhelp{I can't find the lthenc.def file for the Thai fonts}%
  \errmessage{Since I do not know what the LTH encoding means^^J
    I can't typeset Thai.^^J
    I stop here, while you get a suitable lthenc.def file}\@@end
 }
%    \end{macrocode}
%
%    Then, we define default fonts for the document.  \texttt{norasi} and 
%    \texttt{garuda} are Serif and San Serif fonts from the National Fonts 
%    Project, initiated by NECTEC.  \texttt{ttypist} is a typewriter font 
%    created by Theppitak Karoonboonyanan from Thai Linux Working Group.  
%    All fonts are available in free license.
%
%    \begin{macrocode}
\def\thairmdefault{norasi}
\def\thaisfdefault{garuda}
\def\thaittdefault{ttypist}
\def\englishrmdefault{cmr}
\def\englishsfdefault{cmss}
\def\englishttdefault{cmtt}
%    \end{macrocode}
%
%    The next step consists of defining commands to switch to the
%    Thai language. The reason for this is that a user might want
%    to switch back and forth between languages.
%
% \begin{macro}{\thaitext}
%    We define |\thaitext| as a declarative switch to Thai font encoding.
%    \begin{macrocode}
\DeclareRobustCommand{\thaitext}{%
  \fontencoding{LTH}\fontfamily{\thairmdefault}\selectfont%
  \def\rmdefault{\thairmdefault}%
  \def\sfdefault{\thaisfdefault}%
  \def\ttdefault{\thaittdefault}%
  \def\encodingdefault{LTH}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\textthai}
%    This command takes an argument which is then typeset using Thai font 
%    encoding.
%    \begin{macrocode}
\DeclareRobustCommand{\textthai}[1]{{\thaitext #1}}
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{Pali-Sanskrit Characters}
%
%    Thai script can also be used for writing Pali and Sanskrit.  Additional
%    conventions are applied when doing so: (a) Yo Ying and Tho Than must be
%    written without descender; (b) Phinthu is used for marking cluster;
%    (c) Nikhahit is used as a Pali-Sanskrit consonant.  
%
%    (b) is inherently applicable as combining character.  So is (c), with
%    an exception for the case in which it is combined over upper vowel
%    (namely, Sara I), where it must be shifted to higher position.  Nikhahit
%    shifting is implemented in the ligkern rules with the extra glyph
%    provision in the fonts.
%
%    What is left here is (a).  Yo Ying and Tho Than must be converted to
%    their descender-less vairations.  The |\textpali| macro is defined
%    for this purpose.  Note that |\wbr| commands which are normally inserted
%    by automatic tools are ignored here, as most of such tools are not
%    designed for Pali/Sanskrit.  Besides, Pali/Sanskrit is already space
%    delimited.
%
% \begin{macro}{\textpali}
%    This command converts all Yo Ying and Tho Than in its argument to
%    their descender-less variations.
%    \begin{macrocode}
\DeclareRobustCommand{\textpali}[1]{%
  \begingroup
    \def\thaiYoYing{\textYoYingPali{}}%
    \def\thaiThoThan{\textThoThanPali{}}%
    \def\wbr{}%
    \scantokens\expandafter{#1}\relax
  \endgroup
}
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{Hyphenation}
%
%    We define |\thaihyphenmins| macro for hyphenation parameters.  This is 
%    one of the five macros required by \babel{}.
%
% \begin{macro}{\thaihyphenmins}
%    This macro is used to store the correct values of the hyphenation
%    parameters |\lefthyphenmin| and |\righthyphenmin|. They are set to 11.
% \changes{thai-1.1}{2006/07/02}{Theppitak Karoonboonyanan:
%    Now use \cs{providehyphenmins} to provide a default value}
%    \begin{macrocode}
\providehyphenmins{thai}{11}
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{Captions translation}
%
%    We define |\captionsthai| macro for translations of strings.  This is 
%    one of the five macros required by \babel{}.
%
% \begin{macro}{\captionsthai}
%    The macro |\captionsthai| defines all strings used in the four
%    standard documentclasses provided with \LaTeX.
%    \begin{macrocode}
\addto\captionsthai{%
  \def\prefacename{\thaiKhoKhwai\thaiSaraAm\thaiNoNu\thaiSaraAm}%
  \def\refname{\thaiSaraE\thaiOAng\thaiKoKai\thaiSoSua\thaiSaraAa\thaiRoRua%
               \thaiOAng\thaiMaiTho\thaiSaraAa\thaiNgoNgu%
               \thaiOAng\thaiSaraI\thaiNgoNgu}%
  \def\abstractname{\thaiBoBaimai\thaiThoThahan%
                    \thaiKhoKhwai\thaiMaiHanakat\thaiDoDek%
                    \thaiYoYak\thaiMaiEk\thaiOAng}%
  \def\bibname{\thaiBoBaimai\thaiRoRua\thaiRoRua\thaiNoNen\thaiSaraAa%
               \thaiNoNu\thaiSaraU\thaiKoKai\thaiRoRua\thaiMoMa}%
  \def\chaptername{\thaiBoBaimai\thaiThoThahan%
                   \thaiThoThahan\thaiSaraIi\thaiMaiEk}%
  \def\appendixname{\thaiPhoSamphao\thaiSaraAa\thaiKhoKhwai%
                    \thaiPhoPhung\thaiNoNu\thaiWoWaen\thaiKoKai}%
  \def\contentsname{\thaiSoSua\thaiSaraAa\thaiRoRua%
                    \thaiBoBaimai\thaiMaiHanakat\thaiYoYing}%
  \def\listfigurename{\thaiSoSua\thaiSaraAa\thaiRoRua%
                      \thaiBoBaimai\thaiMaiHanakat\thaiYoYing%
                      \thaiRoRua\thaiSaraUu\thaiPoPla}%
  \def\listtablename{\thaiSoSua\thaiSaraAa\thaiRoRua%
                     \thaiBoBaimai\thaiMaiHanakat\thaiYoYing%
                     \thaiToTao\thaiSaraAa\thaiRoRua\thaiSaraAa\thaiNgoNgu}%
  \def\indexname{\thaiDoDek\thaiRoRua\thaiRoRua\thaiChoChang%
                 \thaiNoNu\thaiSaraIi}%
  \def\figurename{\thaiRoRua\thaiSaraUu\thaiPoPla%
                  \thaiThoThahan\thaiSaraIi\thaiMaiEk}%
  \def\tablename{\thaiToTao\thaiSaraAa\thaiRoRua\thaiSaraAa\thaiNgoNgu%
                 \thaiThoThahan\thaiSaraIi\thaiMaiEk}%
  \def\partname{\thaiPhoSamphao\thaiSaraAa\thaiKhoKhwai}%
  \def\enclname{\thaiSoSua\thaiSaraI\thaiMaiEk\thaiNgoNgu%
                \thaiThoThahan\thaiSaraIi\thaiMaiEk%
                \thaiSaraAe\thaiNoNu\thaiBoBaimai%
                \thaiMoMa\thaiSaraAa%
                \thaiDoDek\thaiMaiTho\thaiWoWaen\thaiYoYak}%
  \def\ccname{\thaiSoSua\thaiSaraAm\thaiSaraE\thaiNoNu\thaiSaraAa%
              \thaiThoThung\thaiSaraUe\thaiNgoNgu}%
  \def\headtoname{\thaiSaraE\thaiRoRua\thaiSaraIi\thaiYoYak\thaiNoNu}%
  \def\pagename{\thaiHoHip\thaiNoNu\thaiMaiTho\thaiSaraAa}%
  \def\seename{\thaiDoDek\thaiSaraUu}%
  \def\alsoname{\thaiDoDek\thaiSaraUu%
                \thaiSaraE\thaiPhoPhan\thaiSaraI\thaiMaiEk\thaiMoMa%
                \thaiSaraE\thaiToTao\thaiSaraI\thaiMoMa}%
  \def\proofname{\thaiPhoPhan\thaiSaraI%
                 \thaiSoSua\thaiSaraUu\thaiChoChan\thaiNoNu\thaiThanthakhat}%
  }
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{Date}
%
%    Here we define |\datethai| macro for Thai date format.  This is 
%    one of the five macros required by \babel{}.
%
%    First, let's define the months in Thai.
%
%    \begin{macrocode}
\def\th@month{%
  \ifcase\month\or
    \thaiMoMa\thaiKoKai\thaiRoRua\thaiSaraAa\thaiKhoKhwai\thaiMoMa \or 
    \thaiKoKai\thaiSaraU\thaiMoMa\thaiPhoSamphao\thaiSaraAa%
      \thaiPhoPhan\thaiMaiHanakat\thaiNoNu\thaiThoThong\thaiThanthakhat \or 
    \thaiMoMa\thaiSaraIi\thaiNoNu\thaiSaraAa\thaiKhoKhwai\thaiMoMa \or 
    \thaiSaraE\thaiMoMa\thaiSoRusi\thaiSaraAa\thaiYoYak\thaiNoNu \or 
    \thaiPhoPhan\thaiRu\thaiSoRusi\thaiPhoSamphao\thaiSaraAa%
      \thaiKhoKhwai\thaiMoMa \or 
    \thaiMoMa\thaiSaraI\thaiThoThung\thaiSaraU\thaiNoNu\thaiSaraAa%
      \thaiYoYak\thaiNoNu \or 
    \thaiKoKai\thaiRoRua\thaiKoKai\thaiDoChada\thaiSaraAa%
      \thaiKhoKhwai\thaiMoMa \or 
    \thaiSoSua\thaiSaraI\thaiNgoNgu\thaiHoHip\thaiSaraAa%
      \thaiKhoKhwai\thaiMoMa \or 
    \thaiKoKai\thaiMaiHanakat\thaiNoNu\thaiYoYak\thaiSaraAa%
      \thaiYoYak\thaiNoNu \or 
    \thaiToTao\thaiSaraU\thaiLoLing\thaiSaraAa\thaiKhoKhwai\thaiMoMa \or 
    \thaiPhoPhan\thaiRu\thaiSoSala\thaiChoChan\thaiSaraI\thaiKoKai\thaiSaraAa%
      \thaiYoYak\thaiNoNu \or 
    \thaiThoThong\thaiMaiHanakat\thaiNoNu\thaiWoWaen\thaiSaraAa%
      \thaiKhoKhwai\thaiMoMa
  \fi}
%    \end{macrocode}
%
%    Thai official calendar uses Buddhist Era, the era whose start is marked 
%    by Buddha's entry into Parinirvana.  In original form, it is a lunar 
%    calendar.  But in Thai contemporary calendar, it is synchronized to 
%    Gregorian calendar, with offset of 543 years back.
%
%    \begin{macrocode}
\newcount\th@year
\th@year=\year
\advance\th@year by 543
%    \end{macrocode}
%
%    Then, we come to the |\datethai| macro.
%
% \begin{macro}{\datethai}
%    Thai date format is in the day-month-year order, using Buddhist Era.
%    \begin{macrocode}
\def\datethai{%
  \def\today{\number\day \space \th@month\space %
             \thaiPhoPhan.\thaiSoSala.~\number\th@year}}
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{Extra definitions for language switching}
%
%    Then, the last two macros required by \babel{} are |\extrasthai| and 
%    |\noextrasthai| containing extra definitions needed for Thai language 
%    upon switching to and out of it, respectively.
%
%    \begin{macrocode}
\addto\extrasthai{\thaitext}
\addto\noextrasthai{\latintext}
%    \end{macrocode}
%
%    \subsection{Word break support}
%
%    As Thai script is written continuously without word delimitors, we define 
%    |\wbr| command to mark word boundaries without taking space, so that 
%    \TeX typesetter can wrap Thai lines at proper positions.
%
%    \begin{macrocode}
\def\wbr{\hskip0pt plus0.6pt\relax}
\renewcommand{\baselinestretch}{1.2}
%    \end{macrocode}
%
%    \subsection{Thai numbering support}
%
%    This section provides Thai numbering styles as described at the beginning
%    of this document: |\thainum|, |\thaibracenum|, |\thaialph| and |\thaiAlph|.
%
%    First, we define a macro for translating decimal digits into Thai digits.
%
% \begin{macro}{\thaitranslate}
%    The |\thaitranslate| macro determines decimal digits in the argument and 
%    convert them to Thai digit one by one, until the `|@|' terminator is found.
%    \begin{macrocode}
\def\thaitranslate#1{\ifx @#1%
     \else\ifcase#1
       \thaizero \or \thaione \or \thaitwo \or \thaithree \or \thaifour \or 
       \thaifive \or \thaisix \or \thaiseven \or \thaieight \or \thainine
       \else\fi
       \expandafter\thaitranslate
     \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thainum}
%   The |\thainum| macro is to be used as a numbering style in \LaTeX\ 
%   |\pagenumbering| command.
%
%   It takes a counter name and converts its value into Thai digits.  
%   In details, it prepends the counter name with `|c@|' to access the counter 
%   value, then calls |\@thainum| to convert it into Thai digits.
%
%   The |\@thainum| macro converts a number into Thai digits.  It expands the 
%   argument into a number, terminates it with `|@|', and calls 
%   |\thaitranslate| to convert it into Thai digits.
%
%   \begin{macrocode}
\def\@thainum#1{\expandafter\thaitranslate\number#1@}
\def\thainum#1{\expandafter\@thainum\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thaibracenum}
%    Similar to |\thainum|, the |\thaibracenum| macro is to be used as a 
%    numbering style in \LaTeX\ |\pagenumbering| command.
%
%    It provides Thai digits numbering, surrounded by parentheses.
%
%    \begin{macrocode}
\def\@thaibracenum#1{(\expandafter\thaitranslate\number#1@)}
\def\thaibracenum#1{\expandafter\@thaibracenum\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%    Now come to Thai alphabetic numbering, beginning with the reduced set.
%
% \begin{macro}{\thaialph}
%    The |\thaialph| macro is to be used as a numbering style in \LaTeX\ 
%    |\pagenumbering| command.
%
%    It converts a number not greater than 41 into Thai alphabet in the
%    corresponding order in the reduced set.  In this set, 3 characters
%    are skipped: Kho Khuat, Kho Khon and Kho Rakhang.
%
%    \begin{macrocode}
\def\thaialph#1{\expandafter\@thaialph\csname c@#1\endcsname}
\def\@thaialph#1{%
  \ifcase#1\or \thaiKoKai\or \thaiKhoKhai\or \thaiKhoKhwai\or \thaiNgoNgu\or 
    \thaiChoChan\or \thaiChoChing\or \thaiChoChang\or \thaiSoSo\or 
    \thaiChoChoe\or \thaiYoYing\or \thaiDoChada\or \thaiToPatak\or 
    \thaiThoThan\or \thaiThoNangmontho\or \thaiThoPhuthao\or \thaiNoNen\or 
    \thaiDoDek\or \thaiToTao\or \thaiThoThung\or \thaiThoThahan\or 
    \thaiThoThong\or \thaiNoNu\or \thaiBoBaimai\or \thaiPoPla\or 
    \thaiPhoPhung\or \thaiFoFa\or \thaiPhoPhan\or \thaiFoFan\or 
    \thaiPhoSamphao\or \thaiMoMa\or \thaiYoYak\or \thaiRoRua\or 
    \thaiLoLing\or \thaiWoWaen\or \thaiSoSala\or \thaiSoRusi\or 
    \thaiSoSua\or \thaiHoHip\or \thaiLoChula\or \thaiOAng\or
    \thaiHoNokhuk\else\@ctrerr\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thaiAlph}
%    Similar to |\thaialph|, the |\thaiAlph| macro provides Thai alphabetic 
%    numbering with full alphabet set in the order defined in Thai official 
%    dictionary.  So, it supports up to 44 entries.
%
%    \begin{macrocode}
\def\thaiAlph#1{\expandafter\@thaiAlph\csname c@#1\endcsname}
\def\@thaiAlph#1{%
  \ifcase#1\or \thaiKoKai\or \thaiKhoKhai\or \thaiKhoKhuat\or \thaiKhoKhwai\or 
    \thaiKhoKhon\or \thaiKhoRakhang\or \thaiNgoNgu\or \thaiChoChan\or 
    \thaiChoChing\or \thaiChoChang\or \thaiSoSo\or \thaiChoChoe\or 
    \thaiYoYing\or \thaiDoChada\or \thaiToPatak\or \thaiThoThan\or 
    \thaiThoNangmontho\or \thaiThoPhuthao\or \thaiNoNen\or \thaiDoDek\or 
    \thaiToTao\or \thaiThoThung\or \thaiThoThahan\or \thaiThoThong\or 
    \thaiNoNu\or \thaiBoBaimai\or \thaiPoPla\or \thaiPhoPhung\or
    \thaiFoFa\or \thaiPhoPhan\or \thaiFoFan\or \thaiPhoSamphao\or 
    \thaiMoMa\or \thaiYoYak\or \thaiRoRua\or \thaiLoLing\or 
    \thaiWoWaen\or \thaiSoSala\or \thaiSoRusi\or \thaiSoSua\or 
    \thaiHoHip\or \thaiLoChula\or \thaiOAng\or \thaiHoNokhuk\else\@ctrerr\fi}
%    \end{macrocode}
% \end{macro}
%
%    \subsubsection{\Lopt{thainumber} option}
%
%    Not all Thai documents use Thai digits.  So, we support this as an 
%    option.  If \Lopt{thainumber} option is supplied in the |\usepackage| 
%    command, all numbering will use Thai digits.
%
%    \begin{macrocode}
\DeclareOption{thainumber}{
%    \end{macrocode}
%
%    We need to use the `|@|' character in macro implementations.  So, make it 
%    a normal charactor.
%
%    \begin{macrocode}
\catcode`\@=11
%    \end{macrocode}
%
%    Then, set default page numbering to |thainum|, and redefine counters
%    to |thainum|.
%
%    \begin{macrocode}
\pagenumbering{thainum}
\renewcommand\theenumi{\@thainum\c@enumi}
\renewcommand\theenumii{\@thaialph\c@enumii}
%\renewcommand\theenumiii{\@roman\c@enumiii}
%\renewcommand\theenumiv{\@Alph\c@enumiv}
\@ifundefined{thechapter}%
  {\renewcommand\thesection{\@thainum\c@section}%
   \renewcommand\thesubsection{\thesection.\@thainum\c@subsection}%
   \renewcommand\thesubsubsection{\thesubsection.\@thainum\c@subsubsection}%
   \renewcommand\theparagraph    {\thesubsubsection.\@thainum\c@paragraph}%
   \renewcommand\thesubparagraph {\theparagraph.\@thainum\c@subparagraph}%
   \renewcommand\theequation{\@thainum\c@equation}%
   \renewcommand\thetable{\@thainum\c@table}%
   \renewcommand\thefigure{\@thainum\c@figure}}%
  {\renewcommand\thepart{\@thainum\c@part}%
   \renewcommand\thechapter{\@thainum\c@chapter}%
   \renewcommand\thesection{\thechapter.\@thainum\c@section}%
   \renewcommand\thesubsection{\thesection.\@thainum\c@subsection}%
   \renewcommand\thesubsubsection{\thesubsection.\@thainum\c@subsubsection}%
   \renewcommand\theparagraph    {\thesubsubsection.\@thainum\c@paragraph}%
   \renewcommand\thesubparagraph {\theparagraph.\@thainum\c@subparagraph}%
   \renewcommand\theequation%
       {\ifnum \c@chapter>\z@ \thechapter.\fi \@thainum\c@equation}%
   \renewcommand \thetable%
       {\ifnum \c@chapter>\z@ \thechapter.\fi \@thainum\c@table}%
   \renewcommand \thefigure%
       {\ifnum \c@chapter>\z@ \thechapter.\fi \@thainum\c@figure}}
\renewcommand\thefootnote{\@thainum\c@footnote}
%    \end{macrocode}
%
%    For theorems, we support all custom theorem counters at once by 
%    redefining |\@thmcounter| macro.
%
%    \begin{macrocode}
\def\@thmcounter#1{\noexpand\thainum{#1}} 
%    \end{macrocode}
%
%    In book class, page numberings for frontmatter and mainmatter are
%    different.  So, redefine them.
%
%    \begin{macrocode}
\@ifclassloaded{book}{%
  % redefine page numbering for frontmatter and mainmatter
  \def\ps@headnum{\let\@mkboth\@gobbletwo
       \def\@oddhead{\reset@font\hfil\thepage\hfil}\let\@oddfoot\@empty
       \let\@evenhead\@oddhead\let\@evenfoot\@empty}
  \let\@ltxfrontmatter\frontmatter
  \renewcommand\frontmatter{\@ltxfrontmatter\pagestyle{headnum}\pagenumbering{thaibracenum}}
  \let\@ltxmainmatter\mainmatter
  \renewcommand\mainmatter{\@ltxmainmatter\pagestyle{headings}\pagenumbering{thainum}}
}{}
%    \end{macrocode}
%
%    Redefine Thai date using Thai digits.
%
%    \begin{macrocode}
\def\datethai{%
  \def\today{\@thainum\day \space \th@month\space \thaiPhoPhan.\thaiSoSala.~\@thainum\th@year}}
%    \end{macrocode}
%
%    And finish declaration of \Lopt{thainumber} option
%
%    \begin{macrocode}
\relax
}
%    \end{macrocode}
%
%    Always use thaialph as counter for appendix
%
%    \begin{macrocode}
\@ifclassloaded{book}{%
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@thaialph\c@chapter}}
}{}
\@ifclassloaded{report}{%
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@thaialph\c@chapter}}
}{}
\@ifclassloaded{article}{%
\renewcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@thaialph\c@section}}
}{}
%    \end{macrocode}
%
%    \subsection{Miscelleneous}
%
%    Initialize character codes for Thai
%
%    \begin{macrocode}
\catcode`\^^a1=11 \lccode`\^^a1=`\^^a1 \uccode`\^^a1=`\^^a1
\catcode`\^^a2=11 \lccode`\^^a2=`\^^a2 \uccode`\^^a2=`\^^a2
\catcode`\^^a3=11 \lccode`\^^a3=`\^^a3 \uccode`\^^a3=`\^^a3
\catcode`\^^a4=11 \lccode`\^^a4=`\^^a4 \uccode`\^^a4=`\^^a4
\catcode`\^^a5=11 \lccode`\^^a5=`\^^a5 \uccode`\^^a5=`\^^a5
\catcode`\^^a6=11 \lccode`\^^a6=`\^^a6 \uccode`\^^a6=`\^^a6
\catcode`\^^a7=11 \lccode`\^^a7=`\^^a7 \uccode`\^^a7=`\^^a7
\catcode`\^^a8=11 \lccode`\^^a8=`\^^a8 \uccode`\^^a8=`\^^a8
\catcode`\^^a9=11 \lccode`\^^a9=`\^^a9 \uccode`\^^a9=`\^^a9
\catcode`\^^aa=11 \lccode`\^^aa=`\^^aa \uccode`\^^aa=`\^^aa
\catcode`\^^ab=11 \lccode`\^^ab=`\^^ab \uccode`\^^ab=`\^^ab
\catcode`\^^ac=11 \lccode`\^^ac=`\^^ac \uccode`\^^ac=`\^^ac
\catcode`\^^ad=11 \lccode`\^^ad=`\^^ad \uccode`\^^ad=`\^^ad
\catcode`\^^ae=11 \lccode`\^^ae=`\^^ae \uccode`\^^ae=`\^^ae
\catcode`\^^af=11 \lccode`\^^af=`\^^af \uccode`\^^af=`\^^af
\catcode`\^^b0=11 \lccode`\^^b0=`\^^b0 \uccode`\^^b0=`\^^b0
\catcode`\^^b1=11 \lccode`\^^b1=`\^^b1 \uccode`\^^b1=`\^^b1
\catcode`\^^b2=11 \lccode`\^^b2=`\^^b2 \uccode`\^^b2=`\^^b2
\catcode`\^^b3=11 \lccode`\^^b3=`\^^b3 \uccode`\^^b3=`\^^b3
\catcode`\^^b4=11 \lccode`\^^b4=`\^^b4 \uccode`\^^b4=`\^^b4
\catcode`\^^b5=11 \lccode`\^^b5=`\^^b5 \uccode`\^^b5=`\^^b5
\catcode`\^^b6=11 \lccode`\^^b6=`\^^b6 \uccode`\^^b6=`\^^b6
\catcode`\^^b7=11 \lccode`\^^b7=`\^^b7 \uccode`\^^b7=`\^^b7
\catcode`\^^b8=11 \lccode`\^^b8=`\^^b8 \uccode`\^^b8=`\^^b8
\catcode`\^^b9=11 \lccode`\^^b9=`\^^b9 \uccode`\^^b9=`\^^b9
\catcode`\^^ba=11 \lccode`\^^ba=`\^^ba \uccode`\^^ba=`\^^ba
\catcode`\^^bb=11 \lccode`\^^bb=`\^^bb \uccode`\^^bb=`\^^bb
\catcode`\^^bc=11 \lccode`\^^bc=`\^^bc \uccode`\^^bc=`\^^bc
\catcode`\^^bd=11 \lccode`\^^bd=`\^^bd \uccode`\^^bd=`\^^bd
\catcode`\^^be=11 \lccode`\^^be=`\^^be \uccode`\^^be=`\^^be
\catcode`\^^bf=11 \lccode`\^^bf=`\^^bf \uccode`\^^bf=`\^^bf
\catcode`\^^c0=11 \lccode`\^^c0=`\^^c0 \uccode`\^^c0=`\^^c0
\catcode`\^^c1=11 \lccode`\^^c1=`\^^c1 \uccode`\^^c1=`\^^c1
\catcode`\^^c2=11 \lccode`\^^c2=`\^^c2 \uccode`\^^c2=`\^^c2
\catcode`\^^c3=11 \lccode`\^^c3=`\^^c3 \uccode`\^^c3=`\^^c3
\catcode`\^^c4=11 \lccode`\^^c4=`\^^c4 \uccode`\^^c4=`\^^c4
\catcode`\^^c5=11 \lccode`\^^c5=`\^^c5 \uccode`\^^c5=`\^^c5
\catcode`\^^c6=11 \lccode`\^^c6=`\^^c6 \uccode`\^^c6=`\^^c6
\catcode`\^^c7=11 \lccode`\^^c7=`\^^c7 \uccode`\^^c7=`\^^c7
\catcode`\^^c8=11 \lccode`\^^c8=`\^^c8 \uccode`\^^c8=`\^^c8
\catcode`\^^c9=11 \lccode`\^^c9=`\^^c9 \uccode`\^^c9=`\^^c9
\catcode`\^^ca=11 \lccode`\^^ca=`\^^ca \uccode`\^^ca=`\^^ca
\catcode`\^^cb=11 \lccode`\^^cb=`\^^cb \uccode`\^^cb=`\^^cb
\catcode`\^^cc=11 \lccode`\^^cc=`\^^cc \uccode`\^^cc=`\^^cc
\catcode`\^^cd=11 \lccode`\^^cd=`\^^cd \uccode`\^^cd=`\^^cd
\catcode`\^^ce=11 \lccode`\^^ce=`\^^ce \uccode`\^^ce=`\^^ce
\catcode`\^^cf=11 \lccode`\^^cf=`\^^cf \uccode`\^^cf=`\^^cf
\catcode`\^^d0=11 \lccode`\^^d0=`\^^d0 \uccode`\^^d0=`\^^d0
\catcode`\^^d1=11 \lccode`\^^d1=`\^^d1 \uccode`\^^d1=`\^^d1
\catcode`\^^d2=11 \lccode`\^^d2=`\^^d2 \uccode`\^^d2=`\^^d2
\catcode`\^^d3=11 \lccode`\^^d3=`\^^d3 \uccode`\^^d3=`\^^d3
\catcode`\^^d4=11 \lccode`\^^d4=`\^^d4 \uccode`\^^d4=`\^^d4
\catcode`\^^d5=11 \lccode`\^^d5=`\^^d5 \uccode`\^^d5=`\^^d5
\catcode`\^^d6=11 \lccode`\^^d6=`\^^d6 \uccode`\^^d6=`\^^d6
\catcode`\^^d7=11 \lccode`\^^d7=`\^^d7 \uccode`\^^d7=`\^^d7
\catcode`\^^d8=11 \lccode`\^^d8=`\^^d8 \uccode`\^^d8=`\^^d8
\catcode`\^^d9=11 \lccode`\^^d9=`\^^d9 \uccode`\^^d9=`\^^d9
\catcode`\^^da=11 \lccode`\^^da=`\^^da \uccode`\^^da=`\^^da
\catcode`\^^df=12 \lccode`\^^df=`\^^df \uccode`\^^df=`\^^df
\catcode`\^^e0=11 \lccode`\^^e0=`\^^e0 \uccode`\^^e0=`\^^e0
\catcode`\^^e1=11 \lccode`\^^e1=`\^^e1 \uccode`\^^e1=`\^^e1
\catcode`\^^e2=11 \lccode`\^^e2=`\^^e2 \uccode`\^^e2=`\^^e2
\catcode`\^^e3=11 \lccode`\^^e3=`\^^e3 \uccode`\^^e3=`\^^e3
\catcode`\^^e4=11 \lccode`\^^e4=`\^^e4 \uccode`\^^e4=`\^^e4
\catcode`\^^e5=11 \lccode`\^^e5=`\^^e5 \uccode`\^^e5=`\^^e5
\catcode`\^^e6=11 \lccode`\^^e6=`\^^e6 \uccode`\^^e6=`\^^e6
\catcode`\^^e7=11 \lccode`\^^e7=`\^^e7 \uccode`\^^e7=`\^^e7
\catcode`\^^e8=11 \lccode`\^^e8=`\^^e8 \uccode`\^^e8=`\^^e8
\catcode`\^^e9=11 \lccode`\^^e9=`\^^e9 \uccode`\^^e9=`\^^e9
\catcode`\^^ea=11 \lccode`\^^ea=`\^^ea \uccode`\^^ea=`\^^ea
\catcode`\^^eb=11 \lccode`\^^eb=`\^^eb \uccode`\^^eb=`\^^eb
\catcode`\^^ec=11 \lccode`\^^ec=`\^^ec \uccode`\^^ec=`\^^ec
\catcode`\^^ed=11 \lccode`\^^ed=`\^^ed \uccode`\^^ed=`\^^ed
\catcode`\^^ee=11 \lccode`\^^ee=`\^^ee \uccode`\^^ee=`\^^ee
\catcode`\^^ef=12 \lccode`\^^ef=`\^^ef \uccode`\^^ef=`\^^ef
\catcode`\^^f0=12 \lccode`\^^f0=`\^^f0 \uccode`\^^f0=`\^^f0
\catcode`\^^f1=12 \lccode`\^^f1=`\^^f1 \uccode`\^^f1=`\^^f1
\catcode`\^^f2=12 \lccode`\^^f2=`\^^f2 \uccode`\^^f2=`\^^f2
\catcode`\^^f3=12 \lccode`\^^f3=`\^^f3 \uccode`\^^f3=`\^^f3
\catcode`\^^f4=12 \lccode`\^^f4=`\^^f4 \uccode`\^^f4=`\^^f4
\catcode`\^^f5=12 \lccode`\^^f5=`\^^f5 \uccode`\^^f5=`\^^f5
\catcode`\^^f6=12 \lccode`\^^f6=`\^^f6 \uccode`\^^f6=`\^^f6
\catcode`\^^f7=12 \lccode`\^^f7=`\^^f7 \uccode`\^^f7=`\^^f7
\catcode`\^^f8=12 \lccode`\^^f8=`\^^f8 \uccode`\^^f8=`\^^f8
\catcode`\^^f9=12 \lccode`\^^f9=`\^^f9 \uccode`\^^f9=`\^^f9
\catcode`\^^fa=12 \lccode`\^^fa=`\^^fa \uccode`\^^fa=`\^^fa
\catcode`\^^fb=12 \lccode`\^^fb=`\^^fb \uccode`\^^fb=`\^^fb
\ldf@finish{\CurrentOption}
%</code>
%    \end{macrocode}
%
%%
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
\endinput
