% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
nevelok --- LaTeX package for automatic definite articles for Hungarian
Author:  Zalan Bodo
E-mail:  zbodo@cs.ubbcluj.ro
License: Released under the LaTeX Project Public License v1.3c or later
See:     http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

LaTeX package for automatic definite articles for Hungarian
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
nevelok --- LaTeX package for automatic definite articles for Hungarian
Author:  Zalan Bodo
E-mail:  zbodo@cs.ubbcluj.ro
License: Released under the LaTeX Project Public License v1.3c or later
See:     http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2015 by Zalan Bodo <zbodo@cs.ubbcluj.ro>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Zalan Bodo.

This work consists of the file nevelok.dtx and a Makefile.
Running make generates the derived files README.txt, nevelok.pdf and nevelok.sty.
Running make inst installs the files in the user's TeX tree.
Running make install installs the files in the local TeX tree.

\endpostamble

\usedir{tex/latex/nevelok}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/nevelok}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/nevelok}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{nevelok.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{nevelok}
%<*package>
    [2015/11/17 v1.01 LaTeX package for automatic definite articles for Hungarian]
%</package>
%<package>\RequirePackage{xstring}
%<*driver>
\documentclass{ltxdoc}
\usepackage[a4paper,margin=25mm,left=50mm,nohead]{geometry}
\usepackage[numbered]{hypdoc}
\usepackage{\jobname}
%\EnableCrossrefs
%\CodelineIndex
\RecordChanges
\newtheorem{example}{Example}[section]
\begin{document}
  \sloppy
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.dtx}
% \DoNotIndex{\newcommand,\newenvironment}
%
%\title{\textsf{nevelok} --- \LaTeX{} package for automatic definite articles for Hungarian\thanks{This file 
%   describes version \fileversion, last revised \filedate.}
%}
%\author{Zal\'an Bod\'o\thanks{E-mail: zbodo@cs.ubbcluj.ro}}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v1.00}{2015/09/10}{First public release}
%\changes{v1.01}{2015/11/13}{Added support for referencing in captions (e.g. in figures), added extra commands
% for extended functionality, etc.}
%
% \begin{abstract}
% \LaTeX{} package for automatic definite articles for Hungarian.
% \end{abstract}
%
%\section{Introduction}
%
% In Hungarian, the definite articles ``a'' and ``az'' are determined by the
% pronounciation of the subsequent word. If the first phoneme of the pronounced word
% is a vowel, then the article is ``az'', otherwise ``a'' must be used. 
% The simple commands help the \LaTeX{} user to automatically adjust the
% definite article according to chapter, equation, page, etc. number.
%
% \section{Usage}
%
% \DescribeMacro{\aaz}
% \DescribeMacro{\Aaz}
% The \textsf{nevelok} package provides the following \emph{main} commands: \cmd{\aaz}\marg{reference}
% and \cmd{\Aaz}\marg{reference},
% where \emph{reference} is a reference to a label of a \LaTeX{} object, or simply a 
% defined label (section, equation, figure, etc.).
% The difference stands in letter casing: the \cmd{\aaz} is the lower case command
% producing ``a'' and ``az'', while \cmd{\Aaz} is the upper case version, producing
% ``A'' and ``Az'' for the beginning of a sentence.
% The commands also work if the reference is put in round parantheses.
%
% \DescribeMacro{\aazx}
% \DescribeMacro{\Aazx}
% The \cmd{\aazx}\marg{reference}\marg{s1}\marg{s2} and
% \cmd{\Aazx}\marg{reference}\marg{s1}\marg{s2} commands are the extended versions of the base \cmd{\aaz}
% and \cmd{\Aaz}
% commands, which simply put the reference between the symbols \emph{s1} and \emph{s2}.
% This is useful for example when referencing equations, where the equations' numbers are often put
% in round parantheses. 
% 
% \DescribeMacro{\aazt}
% \DescribeMacro{\Aazt}
% The \cmd{\aazt}\marg{reference} and \cmd{\Aazt}\marg{reference} commands can be used for 
% simple text, i.e. for text representing non-labels. 
%
% \textsf{Note}: The reference argument---in contrast to v1.00---must be a label, not 
% a \cmd{\ref} command. For example: \cmd{\aaz\{eq:some\_equation\}}
% 
% \textsf{Requirements}: The package requires/uses the \textsf{xstring} package.
%
% \section{Limitations}
% \subsection{Using with \textsf{babel}}
%
% The package can be used with \textsf{babel}, however, one cannot use
% \emph{shorthands}. Therefore, one has to load \textsf{babel} with the 
% \texttt{safe=none} option:
%
% \cmd{\usepackage[magyar,safe=none]\{babel\}}
%
% \subsection{Other limitations}
% 
% The numeric labels are handled correctly by the package if they are lower or equal to
% 9999. Larger numbers are planned to be handled in future versions of the package.
%
%\StopEventually{^^A
%  \PrintChanges
%}
%
% \section{Implementation}
%
% \begin{macro}{\ifUnDefinedCs}
% The \cmd{\ifUnDefinedCs} macro checks whether a command is defined, and
% is used to check for undefined labels. It is taken from \cite{Eijkhout92}.
%    \begin{macrocode}
%%
\def\ifUnDefinedCs#1{\expandafter\ifx\csname#1\endcsname\relax}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aaz}
% The \cmd{\aaz} command outputs ``a'' or ``az'', depending on the argument.
% To be used inside the sentence.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\aaz}[1]{%
  \ifUnDefinedCs{r@#1}%
    \PackageWarning{nevelok}{Reference not (yet) defined!}%
    a(z) ??%
  \else%
    \z@aaz{\ref{#1}}{a}{az}{}{}%
  \fi%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Aaz}
% The \cmd{\Aaz} command outputs ``A'' or ``Az'', depending on the argument.
% To be used when starting a new sentence.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\Aaz}[1]{%
  \ifUnDefinedCs{r@#1}%
    \PackageWarning{nevelok}{Reference not (yet) defined!}%
    a(z) ??%
  \else%
    \z@aaz{#1}{A}{Az}{}{}%
  \fi%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aazx}
% The \cmd{\aazx} command is the extended version of \cmd{\aaz} taking 3 parameters:
% the second and third parameters will be put in the output before and after the
% reference.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\aazx}[3]{%
  \ifUnDefinedCs{r@#1}%
    \PackageWarning{nevelok}{Reference not (yet) defined!}%
    a(z) ??%
  \else%
    \z@aaz{\ref{#1}}{a}{az}{#2}{#3}%
  \fi%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Aazx}
% The equivalent of \cmd{\aazx} for the beginning of a sentence.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\Aazx}[3]{%
  \ifUnDefinedCs{r@#1}%
    \PackageWarning{nevelok}{Reference not (yet) defined!}%
    a(z) ??%
  \else%
    \z@aaz{#1}{A}{Az}{#2}{#3}%
  \fi%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aazt}
% The equivalent of \cmd{\aaz} for simple, non-label text.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\aazt}[1]{%
  \z@aaz{#1}{a}{az}{}{}%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Aazt}
% The equivalent of \cmd{\aazt} for the beginning of a sentence.
%    \begin{macrocode}
%%
\DeclareRobustCommand{\Aazt}[1]{%
  \z@aaz{#1}{A}{Az}{}{}%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\z@aaz}
% The main command of the package, used by the two provided commands:
% \cmd{\z@aaz}\marg{reference}\marg{art1}\marg{art2}\marg{sym1}\marg{sym2}.
% The \emph{art1} and \emph{art2} parameters stand for the definite articles used in case of
% consonants and vowels, respectively. Parameters \emph{sym1} and \emph{sym2} are used to 
% handle cases when the reference has to be put between some symbols, e.g. ``('' and ``)''.
%    \begin{macrocode}
%%
\newcount\nev@ind%
\newcount\nev@vege%
\newcount\nev@eppme%
\newcount\nev@az%
%%
\newcommand{\z@aaz}[5]{%
  \begingroup%
  \IfBeginWith{#1}{1}{% if the reference starts with 1,
                      % special care is required
    \StrLen{#1}[\nev@hossz]%
    \nev@ind=\nev@hossz%
    \nev@vege=0%
    \loop% loops while it does not find a digit at the end
         % (in \ref-s, strangely, an \hbox {} appears at the end;
         % this loop was actually written to remove these)
      \StrChar{#1}{\nev@ind}[\nev@kar]%
      \IfSubStr{1234567890}{\nev@kar}{\nev@vege=1}{}\relax%
      \ifnum\nev@vege=0%
        \advance\nev@ind by -1%
    \repeat%
    \StrLeft{#1}{\nev@ind}[\nev@paramuj]% \paramuj will contain the
                                        % ``cleaned'' parameter
    \StrPosition[1]{\nev@paramuj}{.}[\nev@elsopontpozicioja]%
    \StrLen{\nev@paramuj}[\nev@hossz]%
    \nev@eppme=\nev@elsopontpozicioja%
    \advance\nev@eppme by -1%
    \ifnum\nev@elsopontpozicioja=0%
      \StrLeft{\nev@paramuj}{\nev@hossz}[\nev@prefix]%
    \else%
      \StrLeft{\nev@paramuj}{\nev@eppme}[\nev@prefix]%
    \fi%
    \StrLen{\nev@prefix}[\nev@elsohossz]%
    \ifnum\nev@elsohossz=1% 1
      #3%
    \else\ifnum\nev@elsohossz=2% 10
      #2%
    \else\ifnum\nev@elsohossz=3% 100
      #2%
    \else\ifnum\nev@elsohossz=4% 1000
      #3%
    \fi\fi\fi\fi%
  }{%
    \StrChar{#1}{1}[\nev@kar]%
    \nev@az=0%
    \IfSubStr{2346789}{\nev@kar}{% digits that require ``a'';
                                 % this is redundant, but was kept for some reasons
      \nev@az=0%
    }{%
      \IfSubStr{5AEIOUFLMNRSaeiouflmnrs}{\nev@kar}{% digits and letters
                                                   % that require ``az''
        \nev@az=1%
      }{%
        \nev@az=0%
      }%
    }%
    \ifnum\nev@az=1%
      #3%
    \else%
      #2%
    \fi%
  }%
  \ #4#1#5%
  \endgroup%
}%
%%
%    \end{macrocode}
% \end{macro}
%
% \begin{thebibliography}{9}
%   \bibitem{Eijkhout92}
%     Victor Eijkhout,
%     \emph{TeX by Topic. A TeXnician's Reference},
%     Addison-Wesley, Wokingham, England, 1992.
% \end{thebibliography}
%
%\Finale
\endinput
