% \iffalse meta-comment
%
% Copyright 1999 - 2000 Andris Lasis, 2014-2018 Javier Bezos and any
% individual authors listed elsewhere in this file.  All rights
% reserved.
% 
% This file is part of the Babel system.
% --------------------------------------
% 
% 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 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Javier Bezos.
%
% This works is heavily based on the previous work by:
% 
% Copyright (C) 1999 - 2000
%           by Andris Lasis, Faculty of Physics and Mathematics,
%                            University of Latvia
% 
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
% \fi
% \iffalse
%    Tell the \LaTeX\ system who we are and write an entry on the
%    transcript.
%<*dtx>
\ProvidesFile{latvian.dtx}
%</dtx>
%<code>\ProvidesLanguage{latvian}
% \fi
% \ProvidesLanguage{latvian}
        [2018/02/19 v2.0b Latvian support from the babel system]
% \iffalse
%<*filedriver>
\documentclass{ltxdoc}
\usepackage[utf8]{inputenc}
\usepackage[L7x,T1]{fontenc}
\title{The Latvian style for babel}
\author{Andris Lasis, Javier Bezos} 
\newcommand*\babel{\textsf{babel}}
\DeclareTextCompositeCommand{\=}{T1}{i}{\={\i}}
\begin{document}
 \maketitle
 \DocInput{latvian.dtx}
\end{document}
%</filedriver>
%\fi
%
% \section{The Latvian Language}
%
% The file \textsf{latvian.ldf}\footnote{The file described in this
% section has version number 2.0a and was last revised on 2018/02/19.}
% defines the language definition macros for the Latvian language. It
% is heavily based on the work by Andris Lasis, and this file just
% cleans the code up and adapts it to babel 3.9.
%
% Since the |T1| encoding doesn't contain all the characters required
% for Latvian, you should use |L7x| instead (note the lowercase
% |x|). The |dfu| file (ie, |utf8| for \textsf{fontenc}) for this
% encoding is included in Lithuanian. Unfortunately, not many fonts
% come in this encoding. Of course, you can use either \textsf{luatex} or
% \textsf{xetex}, too, because Unicode strings are also defined.
% 
% Actually, you can use the |T1| encoding, but hyphenation will not be
% correct, and oddly it doesn't work with \textsf{inputenc} and |utf8|
% (future releases will fix this issue), except if you request the
% |L7x| encoding before |T1| (this is, of course, just a trick, which 
% may not work; note some fonts render macron-i with the dot, because
% |l7xenc.def| defines it as |\=i|). If |T1| is used, you must load
% \textsf{fontenc} before \textsf{babel} and set the modifier
% |t1composite| (because it redefines some `composite commands' which
% may affect other languages). A complete example is:
%\begin{verbatim}
%\documentclass{article}
%\usepackage[utf8]{inputenc}
%\usepackage[L7x,T1]{fontenc}
%\usepackage{combelow} % Optional, see below
%\usepackage[latvian.t1composite]{babel}
%\usepackage{fourier}
%
%\begin{document} 
%Aa, Āā, Bb, Cc, Čč, Dd, Ee, Ēē, Ff, Gg, Ģģ, Hh, Ii, Īī, Jj, Kk, Ķķ,
%Ll, Ļļ, Mm, Nn, Ņņ, Oo, Pp, Rr, Ss, Šš, Tt, Uu, Ūū, Vv, Zz, Žž
%\end{document}
%\end{verbatim}
% By loading also the package \textsf{combelow} you get much better
% results (it is detected automatically), but you should use |T1| only
% as a last resort and only for small chuncks of Latvian text. |OT1|
% is not directly supported.
% 
% There are two commands for the date: |\datumaa| (locative) and
% |\datums| (nominative); |\today| is defined as |\datumaa|, but you
% can change it with |\renewcommand{\latviantoday}{\datums}|. By
% default \textit{g.} is used, but you can change it with, say,
% |\renewcommand\latviangada{gada}|.
%
% |\alph| and |\Alph| exclude the letter \textit{q}.
%
% \StopEventually{}
%
%  \subsection*{The code}
%
%    \begin{macrocode}
%<*code>
\LdfInit{latvian}\captionslatvian
\ifx\l@latvian\@undefined
  \@nopatterns{Latvian}
  \adddialect\l@latvian0
\fi
%    \end{macrocode}
%
% Captions and date.
%
%    \begin{macrocode}
\StartBabelCommands*{latvian}{captions}
  [unicode, charset=utf8, fontenc=EU1 EU2]

\SetString\prefacename{Priekšvārds}
\SetString\refname{Literatūra}
\SetString\abstractname{Anotācija}
\SetString\bibname{Bibliogrāfija}
\SetString\chaptername{nodaļa}
\SetString\listfigurename{Attēlu rādītājs}
\SetString\listtablename{Tabulu rādītājs}
\SetString\indexname{Priekšmetu rādītājs}
\SetString\figurename{zīm.\@}
\SetString\partname{daļa}
\SetString\enclname{Pielikumā}
\SetString\alsoname{skat.\@ arī}

\StartBabelCommands*{latvian}{date}
  [unicode, charset=utf8, fontenc=EU1 EU2]

\SetStringLoop{month#1name}{janvārī,februārī,martā,aprīlī,maijā,%
  jūnijā,jūlijā,augustā,septembrī,oktobrī,novembrī,decembrī}
\SetStringLoop{nommonth#1name}{janvāris,februāris,marts,aprīlis,maijs,%
  jūnijs,jūlijs,augusts,septembris,oktobris,novembris,decembris}

\StartBabelCommands*{latvian}{captions}

\SetString\prefacename{Priek\v sv\=ards}%
\SetString\refname{Literat\=ura}
\SetString\abstractname{Anot\=acija}
\SetString\bibname{Bibliogr\=afija}
\SetString\chaptername{noda\c la}
\SetString\appendixname{Pielikums}
\SetString\contentsname{Saturs}
\SetString\listfigurename{Att\=elu r\=ad\={\i}t\=ajs}
\SetString\listtablename{Tabulu r\=ad\={\i}t\=ajs}
\SetString\indexname{Priek\v smetu r\=ad\={\i}t\=ajs}
\SetString\figurename{z\={\i}m.\@}
\SetString\tablename{tabula}
\SetString\partname{da\c la}
\SetString\enclname{Pielikum\=a}
\SetString\ccname{Kopija(s)}
\SetString\headtoname{}
\SetString\pagename{lpp.\@}
\SetString\seename{skat.\@}
\SetString\alsoname{skat.\@ ar\={\i}}

\StartBabelCommands*{latvian}{date}

\SetStringLoop{month#1name}{janv\=ar\={\i},febru\=ar\={\i},mart\=a,%
  apr\={\i}l\={\i},maij\=a,j\=unij\=a,j\=ulij\=a,august\=a,%
  septembr\={\i},oktobr\={\i},novembr\={\i},decembr\={\i}}
\SetStringLoop{nommonth#1name}{janv\=aris,febru\=aris,marts,%
  apr\={\i}lis,maijs,j\=unijs,j\=ulijs,augusts,septembris,%
  oktobris,novembris,decembris}

\SetString\datumaa{\number\year.~\latviangada\ \number\day.~%
  \csname month\romannumeral\month name\endcsname}
\SetString\datums{\number\year.~\latviangada\ \number\day.~%
  \csname nommonth\romannumeral\month name\endcsname}

\SetString\today{\datumaa}

\AfterBabelCommands{\newcommand\latviangada{g.}}

\EndBabelCommands
%    \end{macrocode}
%
% Section numbering.
%
%    \begin{macrocode}
\gdef\the@chapter{%
  \ifx\chapter\undefined\relax\else
  \ifnum\c@chapter>\z@\thechapter\fi\fi}
\def\thechapter{%
  \ifx\chapter\undefined\relax\else
  \arabic{chapter}.\fi}
\def\thesection{\the@chapter\arabic{section}.}
\def\thesubsection{\thesection\arabic{subsection}.}
\def\thesubsubsection{\thesubsection\arabic{subsubsection}.}
\def\theparagraph{\thesubsubsection\arabic{paragraph}.}
\def\thesubparagraph{\theparagraph\arabic{subparagraph}.}
%    \end{macrocode}
%
% Part numbering
%
%    \begin{macrocode}
\def\depth@{\ifx\chapter\undefined\m@ne\else-2\relax\fi}
\def\@part[#1]#2{%
  \relax
  \ifnum\c@secnumdepth>\depth@
    \refstepcounter{part}
    \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  \else
    \addcontentsline{toc}{part}{#1}\fi
    \ifx\chapter\undefined
      \bgroup\parindent\z@\raggedright
    \else
      \markboth{}{}\bgroup\centering
    \fi
    \interlinepenalty\@M
    \ifnum\c@secnumdepth>\depth@
      \normalfont\bfseries\thepart~\partname  %  adapted to Latvian order
      \par
      \ifx\chapter\undefined\nobreak\else\vskip 20\p@\fi
    \fi
    \ifx\chapter\undefined\huge\else\Huge\fi\normalfont\bfseries#2%
    \ifx\chapter\undefined\markboth{}{}\fi
    \par\egroup
    \ifx\chapter\undefined\nobreak
    \vskip3ex \@afterheading\else\@endpart
  \fi}
%    \end{macrocode}
%
%  Chapter head
%
%    \begin{macrocode}
\def\@makechapterhead#1{%
  \vspace*{50pt}%
  {\parindent 0pt \raggedright
   \ifnum\c@secnumdepth>\m@ne
     \huge\normalfont\bfseries\thechapter\space\@chapapp{}%
     \par
     \vskip20pt
   \fi
   \Huge\normalfont\bfseries#1\par\nobreak\vskip40pt }}
%    \end{macrocode}
%
% First paragraph indent (of the text following a heading).
%
%    \begin{macrocode}
\let\@aifORI\@afterindentfalse
\def\bbl@latvianindent{%
  \let\@afterindentfalse\@afterindenttrue
  \@afterindenttrue}
\def\bbl@nonlatvianindent{%
  \let\@afterindentfalse\@aifORI
  \@afterindentfalse}
\addto\extraslatvian{\bbl@latvianindent}
\addto\noextraslatvian{\bbl@nonlatvianindent}
%    \end{macrocode}
%
% Float numbering.
%
%    \begin{macrocode}
\def\thefigure{\the@chapter\@arabic\c@figure.}
\def\thetable{\the@chapter\@arabic\c@table.}
\def\fnum@figure{\thefigure~\figurename}
\def\fnum@table{\thetable~\tablename}
\long\def\@makecaption#1#2{%
  \vskip10\p@
  \sbox\@tempboxa{#1\if\empty#2\else: #2\fi}%
  \ifdim\wd\@tempboxa>\hsize
    #1: #2\par
  \else
    \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
  \fi}
%    \end{macrocode}
%
% Theorems
%
%    \begin{macrocode}
\def\@thmcountersep{}
\gdef\@makethmnumber#1#2{%
  \ifx\thmno@left0{#1\ #2}\else {#2. #1}\fi}
\def\@begintheorem#1#2{%
  \normalfont\itshape\trivlist
  \item[\hskip\labelsep
    \hskip\parindent\normalfont\bfseries
    {\@makethmnumber{#1}{#2}}.]}
\def\@opargbegintheorem#1#2#3{%
  \normalfont\itshape\trivlist
  \item[\hskip\labelsep \hskip\parindent\normalfont\bfseries  
    {\@makethmnumber{#1}{#2} (#3).}]}
\gdef\thmnoleft{\let\thmno@left=1}
\gdef\thmnoright{\let\thmno@left=0}
\thmnoleft
%    \end{macrocode}
%
% Equations
%
%    \begin{macrocode}
\def\theequation{\the@chapter\arabic{equation}}
%    \end{macrocode}
%
%  Marks.
%
%    \begin{macrocode}
\ifx\@chapapp\undefined
  \if@twoside
  \def\sectionmark#1{%
    \markboth
    {\uppercase{\ifnum\c@secnumdepth>\z@ne\thesection\hskip1em\relax\fi#1}}%
    {}}
  \def\subsectionmark#1{%
    \markright
    {\uppercase{\ifnum\c@secnumdepth>\@ne\thesubsection\ \fi#1}}}
 \else
  \def\sectionmark#1{%
    \markright
    {\uppercase{\ifnum\c@secnumdepth>\z@\thesection\hskip1em\relax\fi#1}}}
 \fi%
\else
 \def\chaptermark#1{%
   \markboth
   {\uppercase{\ifnum\c@secnumdepth>\m@ne\thechapter~\@chapapp: \fi#1}}%
   {}}
 \def\sectionmark#1{%
   \markright{\uppercase{\ifnum\c@secnumdepth>\z@\thesection\ \fi#1}}}
\fi
%    \end{macrocode}
%
% Counters. Only locally.
%
%    \begin{macrocode}
\addto\extraslatvian{%
  \babel@save\@alph
  \def\@alph#1{%
    \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or k\or
    l\or m\or n\or o\or p\or r\or s\or t\or u\or v\or
    z\else\@ctrerr\fi}%
  \babel@save\@Alph
  \def\@Alph#1{%
    \ifcase#1\or A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or K\or
    L\or M\or N\or O\or P\or R\or S\or T\or U\or V\or Z\else\@ctrerr\fi}}
%    \end{macrocode}
%
% Hyphenmins and shorthands.
%
%    \begin{macrocode}
\def\latvianhyphenmins{\tw@\tw@}
\initiate@active@char{"}
\addto\extraslatvian{\languageshorthands{latvian}}
\addto\extraslatvian{\bbl@activate{"}}

\DeclareTextCompositeCommand{\=}{OT1}{i}{\@tabacckludge={\i}}
\DeclareTextCompositeCommand{\=}{T1}{i}{\@tabacckludge={\i}}

\@expandtwoargs\in@{,t1composite,}{,\BabelModifiers,}
\ifin@

  \DeclareTextCommand{\lv@cb}{T1}[1]%  same as \c
   {\leavevmode
    \setbox\z@\hbox{#1}%
    \ifdim\ht\z@=1ex\accent11 #1%
    \else{\ooalign{\unhbox\z@\crcr\hidewidth\char11\hidewidth}}%
    \fi}
  \let\lv@cbg\lv@cb

  \DeclareTextCompositeCommand{\c}{T1}{g}{\lv@cbg{g}}
  \DeclareTextCompositeCommand{\c}{T1}{G}{\lv@cb{G}}
  \DeclareTextCompositeCommand{\c}{T1}{r}{\lv@cb{r}}
  \DeclareTextCompositeCommand{\c}{T1}{R}{\lv@cb{R}}
  \DeclareTextCompositeCommand{\c}{T1}{k}{\lv@cb{k}}
  \DeclareTextCompositeCommand{\c}{T1}{K}{\lv@cb{K}}
  \DeclareTextCompositeCommand{\c}{T1}{l}{\lv@cb{l}}
  \DeclareTextCompositeCommand{\c}{T1}{L}{\lv@cb{L}}
  \DeclareTextCompositeCommand{\c}{T1}{n}{\lv@cb{n}}
  \DeclareTextCompositeCommand{\c}{T1}{N}{\lv@cb{N}}

  \def\lv@ifcombelow{\@ifpackageloaded{combelow}}
  \AtBeginDocument{%  because \@ifpackageloaded is 'only preamble'
    \@ifpackageloaded{combelow}%
      {\let\lv@ifcombelow\@firstoftwo}%
      {\let\lv@ifcombelow\@secondoftwo}}

  \addto\extraslatvian{%
    \babel@save\lv@cbg
    \lv@ifcombelow
      {\babel@save\lv@cb
       \let\lv@cb\cb
       \let\lv@cbg\cb}%
      {\let\lv@cbg\v}}

\fi

\declare@shorthand{latvian}{"a}{\textormath{\={a}}{\=a}}
\declare@shorthand{latvian}{"e}{\textormath{\={e}}{\=e}}
\declare@shorthand{latvian}{"u}{\textormath{\={u}}{\=u}}
\declare@shorthand{latvian}{"i}{\textormath{\={\i}}{\=\i}}
\declare@shorthand{latvian}{"o}{\textormath{\={o}}{\=o}}

\declare@shorthand{latvian}{"A}{\textormath{\={A}}{\=A}}
\declare@shorthand{latvian}{"E}{\textormath{\={E}}{\=E}}
\declare@shorthand{latvian}{"U}{\textormath{\={U}}{\=U}}
\declare@shorthand{latvian}{"I}{\textormath{\={I}}{\=I}}
\declare@shorthand{latvian}{"O}{\textormath{\={O}}{\=O}}

\declare@shorthand{latvian}{"r}{\textormath{\c{r}}{\c r}}
\declare@shorthand{latvian}{"s}{\textormath{\v{s}}{\v s}}
\declare@shorthand{latvian}{"g}{\textormath{\c{g}}{\c g}}
\declare@shorthand{latvian}{"k}{\textormath{\c{k}}{\c k}}
\declare@shorthand{latvian}{"l}{\textormath{\c{l}}{\c l}}
\declare@shorthand{latvian}{"z}{\textormath{\v{z}}{\v z}}
\declare@shorthand{latvian}{"c}{\textormath{\v{c}}{\v c}}
\declare@shorthand{latvian}{"n}{\textormath{\c{n}}{\c n}}

\declare@shorthand{latvian}{"R}{\textormath{\c{R}}{\c R}}
\declare@shorthand{latvian}{"S}{\textormath{\v{S}}{\v S}}
\declare@shorthand{latvian}{"G}{\textormath{\c{G}}{\c G}}
\declare@shorthand{latvian}{"K}{\textormath{\c{K}}{\c K}}
\declare@shorthand{latvian}{"L}{\textormath{\c{L}}{\c L}}
\declare@shorthand{latvian}{"Z}{\textormath{\v{Z}}{\v Z}}
\declare@shorthand{latvian}{"C}{\textormath{\v{C}}{\v C}}
\declare@shorthand{latvian}{"N}{\textormath{\c{N}}{\c N}}

\declare@shorthand{latvian}{"-}{\nobreak\-\bbl@allowhyphens}
\declare@shorthand{latvian}{"|}{%
  \textormath{\penalty\@M\discretionary{-}{}{\kern.03em}%
              \allowhyphens}{}}
\declare@shorthand{latvian}{""}{\hskip\z@skip}
\declare@shorthand{latvian}{"~}{\textormath{\leavevmode\hbox{-}}{-}}
\declare@shorthand{latvian}{"=}{\penalty\@M-\hskip\z@skip}

\ldf@finish{latvian}
%</code>
%    \end{macrocode}
%
% \Finale
% \endinput
%%
%% \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         \~}
%% 
