% +--------------------------------------------------+
% | Typeset accents.tex to get the documentation.   |
% +--------------------------------------------------+
%
%% Copyright (C) 1998, 1999, 2000, 2006 Javier Bezos
%% All Rights Reserved
%% http://www.tex-tipografia.com
%%
%% This work 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".
%%
%% This Current Maintainer of this work is Javier Bezos.
%%
%% This work consists of the files accents.tex and accents.sty.
%
% Changes
% ~~~~~~~
%
% 2006-05-12. TeX converts to ord any symbol with a math accent.
% Accents now emulates this behaviour with braces around the
% nucleous; otherwise, the original category is preserved and
% the accents could look misaligned. (Reported by Viet-TrungLuu.)
%
% Release
% ~~~~~~~

\ProvidesPackage{accents}[2006/05/12 v1.3 Math Accent Tools]

\let\cc@palette\mathpalette

\DeclareOption{nonscript}
  {\renewcommand\cc@palette[2]{#1\textstyle{#2}}}

\DeclareOption{single}
  {\renewcommand\cc@accent[1]{\mathaccent#1}}

\newdimen\cc@skew
\newdimen\cc@wd

\newcount\cc@code
\newcount\cc@group
\newcount\cc@skewchar

\newif\ifcc@more

% Code for real accents
% ~~~~~~~~~~~~~~~~~~~~~

\def\cc@accent#1#2{%
  \begingroup
    \cc@code=#1\relax
    \let\math@bgroup\@empty
    \def\math@egroup##1{\cc@setchar##1}%
    \cc@palette\cc@@accent{#2}%
    {\cc@nuc}%
  \endgroup}

\def\cc@iaccent#1#2{%
  \cc@fetch{#2}%
  \mathaccent#1{%
    \ifcc@more#2\else
	  \cc@phant{#2}%
      \gdef\cc@nuc{#2}%
	\fi
	\kern\cc@skew}%
  \kern-\cc@skew}%

\def\cc@@accent#1#2{%
  \let\cc@style=#1%
  \cc@fetch{#2}%
  \mathaccent\cc@code{%
    \ifcc@more#2\else
	  \cc@phant{#2}%
      \gdef\cc@nuc{#2}%
	\fi
	\kern\cc@skew}%
  \kern-\cc@skew
  \kern\cc@wd}%

% The accents are redefined:

\begingroup

\def\mathaccentV#1{\string"\noexpand\accentclass@}%
\let\protect\@empty
\let\mathaccent\@empty

\def\cc@set#1{%
  \xdef#1{\noexpand\protect\noexpand\cc@accent{#1}}}
  
\cc@set\grave   %{\protect\cc@accent{"7012\relax}}
\cc@set\acute   %{\protect\cc@accent{"7013\relax}}
\cc@set\check   %{\protect\cc@accent{"7014\relax}}
\cc@set\breve   %{\protect\cc@accent{"7015\relax}}
\cc@set\bar     %{\protect\cc@accent{"7016\relax}}
\cc@set\hat     %{\protect\cc@accent{"705E\relax}}
\cc@set\dot     %{\protect\cc@accent{"705F\relax}}
\cc@set\tilde   %{\protect\cc@accent{"707E\relax}}
\cc@set\ddot    %{\protect\cc@accent{"707F\relax}}
\cc@set\vec     %{\protect\cc@accent{"017E\relax}}
\gdef\ring{\protect\cc@accent{"7017\relax}}

\@ifundefined{mathring}{}{\cc@set\mathring}

\endgroup

% Faked accents
% ~~~~~~~~~~~~~

\newcommand\accentset[2]{%
  \begingroup
    \def\cc@a{#1}%
    \let\math@bgroup\@empty
    \def\math@egroup##1{\cc@setchar##1}%
    \cc@palette\cc@accentset{#2}%
    {\cc@nuc}%
  \endgroup}

\def\cc@accentset#1#2{%
  \let\cc@style#1%
  \cc@fetch{#2}%
  \sbox\z@{$\m@th\cc@style\mathgroup\cc@group
    \ifcc@more#2\else\cc@phant{#2}%
    \gdef\cc@nuc{#2}\fi$}%
  \cc@align{%
     \hidewidth\kern\cc@skew
     $\m@th\scriptscriptstyle\mathgroup\cc@group\cc@a$%
     \hidewidth\cr{\box\z@}}%
  \kern\cc@wd}%

\def\cc@iaccentset#1#2{%
  \cc@fetch{#2}%
  \sbox\z@{$\m@th\cc@style\ifcc@more#2\else\cc@phant{#2}%
    \gdef\cc@nuc{#2}\fi$}%
  \cc@align{%
     \hidewidth\kern\cc@skew
     $\m@th\scriptscriptstyle#1$\hidewidth\cr
    {\box\z@}}}%

\def\cc@align#1{%
  \vbox{%
    \baselineskip\z@skip
	\lineskip.25ex
    \ialign{##\crcr#1\crcr}}}

% Shared by real and faked accents
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

\def\cc@phant#1{%
  \v@true\h@true
  \finph@nt}% with \box0 from \cc@fetch

\def\cc@setchar{%
  \cc@skewchar\ifnum\mathgroup=\m@ne
      \skewchar\textfont\@ne
    \else\ifnum\skewchar\textfont\mathgroup=\m@ne
      \skewchar\textfont\@ne
    \else\skewchar\textfont\mathgroup\fi\fi
  \advance\cc@skewchar"7100 }

\def\cc@fetch#1{%
  \cc@group=\the\mathgroup
  \let\cc@accent\@gobble
  \let\accentset\@gobble
  \sbox\z@{$\m@th\mathgroup\cc@group\cc@setchar
     \cc@style#1{}\mathchar\cc@skewchar$}%
  \sbox\@ne{$\m@th\mathgroup\cc@group\cc@setchar
     \cc@style#1\mathchar\cc@skewchar$}%
  \cc@morefalse
  \def\cc@accent##1{\global\cc@moretrue}%
  \def\accentset##1{\global\cc@moretrue}%
  \global\cc@skew2\wd\@ne
  \global\advance\cc@skew-2\wd\z@
  \sbox\z@{$\m@th\mathgroup\cc@group\cc@style#1$}%
  \global\cc@wd-\wd\z@
  \let\cc@accent\cc@iaccent
  \let\accentset\cc@iaccentset}

% Two useful faked accents
% ~~~~~~~~~~~~~~~~~~~~~~~~

\def\dddot{\accentset{{\cc@style.\mkern-1.7mu\textstyle.\mkern-1.7mu.}}}
\def\ddddot{\accentset{{\cc@style.\mkern-2.1mu.\mkern-2.1mu.\mkern-2.1mu.}}}

% Underaccents
% ~~~~~~~~~~~~

\newcommand\underaccent[2]{%
  \begingroup
    \def\cc@a{#2}% Stores the nucleous...
    \cc@palette\cc@underaccent{#1}% ...and the accent is passed
    #2%
  \endgroup}%

\def\cc@underaccent#1#2{%
  \let\cc@style#1%
  \expandafter\in@\expandafter\cc@accent\expandafter{#2}%
  \ifin@
    \expandafter\cc@uaccent#2\@@%
  \else
    \cc@uaccentset{#2}%
  \fi}

\def\cc@uaccent#1#2#3\@@{% #1 and #2 gobbles \protect\cc@accent
  \cc@under{%
    \vbox to.2ex{%
      \hbox{%
        $\m@th
         \cc@style
         \mathgroup\cc@group
         \cc@shift
         \mathchar#3$}%
      \vss}}}

\def\cc@uaccentset#1{%
  \cc@under{%
    \hbox{%
      $\m@th
       \cc@style
       \mathgroup\cc@group
       \cc@shift
       \scriptscriptstyle#1$}}}

\def\cc@under#1{%
  \cc@group\the\mathgroup
  \sbox\z@{$\m@th\cc@style\mathgroup\cc@group\cc@a$}%
  \sbox\tw@{$\scriptspace\z@\m@th\cc@style\mathgroup\cc@group\cc@a_{}$}%
  \rlap{%
    \oalign{%
      \vrule \@depth\dp\z@ \@width\z@
      \kern\wd\tw@
     \crcr
      \hidewidth#1\hidewidth}}}

\def\cc@shift{%
   \dimen@=-.000008ex
   \multiply\dimen@\ifnum\mathgroup=\m@ne
     \fontdimen\@ne\textfont\@ne
   \else
     \fontdimen\@ne\textfont\cc@group\fi
   \kern\dimen@}

% And \undertilde
% ~~~~~~~~~~~~~~~

\newcommand\undertilde{\cc@palette\cc@undertilde}

\def\cc@undertilde#1#2{%
  \cc@group\the\mathgroup
  \sbox\z@{$\m@th\mathgroup\cc@group#1#2$}%
  \oalign{%
     $\m@th\mathgroup\cc@group#1#2$%
    \crcr
     \hidewidth 
     \vbox to.5ex{%
       \hbox{%
         $\hss
          #1%
          \mathgroup\cc@group
          \widetilde{\hbox to\wd\z@{\hss}}%
          \hss$}%
       \vss}%
     \hidewidth}}

% The following command is used by \DeclareMathAccent
% and it's the only part requiring modification

\def\set@mathaccent#1#2#3#4{%
  \xdef#2{\noexpand\protect\noexpand\cc@accent
              {"\mathchar@type#3\hexnumber@#1#4\relax}}}

\ProcessOptions

\endinput