%
% Copyright (C) 1998-2019 Javier Bezos http://www.texnia.com
%
% This file may be distributed and/or modified under the conditions of
% the MIT License. A version can be found at the end of this file.
%
%
% Release
% ~~~~~~~

\ProvidesPackage{tensind}[1998/05/01 v1.1 Tensorial Indexes]

\@tempcnta=\the\catcode`^
\@tempcntb=\the\catcode`_
\catcode`^=7\catcode`_=8

\newdimen\tsi@dimen

\newif\iftsi@sp
\newif\iftsi@sb

\newif\iftsi@e
\newif\iftsi@b
\newif\iftsi@l
\newif\iftsi@r
\newif\iftsi@o
\newif\iftsi@c
\newif\iftsi@s
\newif\iftsi@nodot

\def\tsi@thedots{}
\def\tsi@theprimes{}
\def\tsi@thesb{}
\def\tsi@thesp{}
\def\tsi@lastloc{x}
\def\tsi@lastindex{}%
\def\tsi@addindex{\tsi@addpr}

\let\tsi@bl\relax
\let\tsi@br\relax
\let\tsi@bm\relax
\let\tsi@tl\relax
\let\tsi@tr\relax
\let\tsi@tm\relax

\let\tsi@choice\mathchoice

\newif\iftsi@top
\newif\iftsi@bot

\let\tsi@gap\relax

\def\tensorformat#1{\tsi@lfalse\tsi@rfalse\tsi@efalse
  \tsi@bfalse\tsi@ofalse\tsi@cfalse\tsi@sfalse
  \@tfor\tsi@a:=#1\do{\csname tsi@\tsi@a true\endcsname}}

\tensorformat{}
\def\indexdot{\cdot}

\def\tsi@appendto#1#2{\protected@edef#2{#2#1}}

\def\tensor{\catcode`_=8\catcode`^=7 %
  \def\tsi@end{\end}\tsi@ifnextchar[{\tsi@otensor}{\tsi@xtensor}}

\begingroup

\catcode`?=13
\gdef\tensordelimiter#1{\mathcode`#1="8000{\catcode`?=13\lccode`?=`#1
  \lowercase{%
    \gdef?{\begingroup\catcode`_=8 \catcode`^=7 %
        \def\tsi@end{#1}\def?{\endtensor\endgroup}%
        \@ifnextchar[{\tsi@otensor}{\tsi@xtensor}}}}}

\endgroup

\def\tsi@otensor[#1]{\tensorformat{#1}\tsi@xtensor}

\gdef\tsi@xtensor#1{%
   \def\tsi@nucl{#1}%
   \futurelet\nxt\tsi@process}

\def\tsi@process#1{%
  \def\next{\futurelet\nxt\tsi@process}%
  \def\tsi@theindex{#1}\def\tsi@a{_}%
  \ifx\tsi@theindex\tsi@a
    \if\tsi@lastloc x\let\tsi@first\@empty\fi
    \if\tsi@lastloc b\else\tsi@switchsb\fi
  \else
    \def\tsi@a{^}%
    \ifx\tsi@theindex\tsi@a
      \if\tsi@lastloc p\else\tsi@switchsp\fi
    \else
      \ifx\tsi@theindex\tsi@end
        \let\next\tsi@end
      \else
        \tsi@nodotfalse
        \ifx\nxt\bgroup
          \def\tsi@lastindex{}%
        \else
          \csname tsi@cmd\string#1\endcsname
          \def\tsi@lastindex{#1}%
        \fi
        \tsi@addindex
      \fi
    \fi
  \fi
  \next}

\def\tsi@switchsp{%
  \let\tsi@addindex\tsi@addsp
  \iftsi@top
    \def\tsi@gap{\tsi@tm}%
  \else
    \def\tsi@gap{\tsi@tl}%
  \fi
  \tsi@appendto{\tsi@thedots}\tsi@thesp
  \let\tsi@gap\relax
  \def\tsi@thedots{}}

\def\tsi@switchsb{%
  \let\tsi@addindex\tsi@addsb
  \iftsi@bot
    \def\tsi@gap{\tsi@bm}%
  \else
    \def\tsi@gap{\tsi@bl}%
  \fi
  \tsi@appendto{\tsi@thedots}\tsi@thesb  
  \let\tsi@gap\relax
  \def\tsi@thedots{}}

\def\tsi@addpr{%
  \tsi@appendto{\tsi@theindex}\tsi@theprimes}

\def\tsi@addsp{%
  \tsi@toptrue
  \tsi@appendto{\tsi@theindex}\tsi@thesp
  \iftsi@nodot
    \tsi@appendto{\protect\tsi@phantom{\tsi@theindex}}\tsi@thedots
  \else
    \tsi@appendto{\tsi@gap{\tsi@theindex}}\tsi@thedots
  \fi
  \def\tsi@lastloc{p}}

\def\tsi@addsb{%
  \tsi@bottrue
  \tsi@appendto{\tsi@theindex}\tsi@thesb
  \iftsi@nodot
    \tsi@appendto{\protect\tsi@phantom{\tsi@theindex}}\tsi@thedots
  \else
    \tsi@appendto{\tsi@gap{\tsi@theindex}}\tsi@thedots
  \fi
  \def\tsi@lastloc{b}}

\def\tsi@dot#1{%
  \setbox\z@\hbox{$\tsi@fam\m@th\tsi@index#1$}%
  \hbox to\wd\z@{\hss$\tsi@fam\m@th\tsi@index\indexdot$\hss}}

\newdimen\primes

\def\endtensor{%
  \if\tsi@lastloc p
    \def\tsi@gap{\tsi@br}%
    \tsi@appendto{\tsi@thedots}\tsi@thesb
  \else
    \def\tsi@gap{\tsi@tr}%
    \tsi@appendto{\tsi@thedots}\tsi@thesp
  \fi
  \let\tsi@bl\tsi@phantom  \let\tsi@br\tsi@phantom
  \let\tsi@bm\tsi@phantom  \let\tsi@tm\tsi@phantom
  \def\tsi@tl##1{\tsi@phantom{##1}\global\advance\tsi@dimen\wd\z@}%
  \iftsi@top
    \let\tsi@tr\tsi@phantom
  \else
    \let\tsi@tr\tsi@tl
  \fi
  \iftsi@bot
    \iftsi@l\let\tsi@bl\tsi@dot\let\tsi@bm\tsi@dot\fi
    \iftsi@r\let\tsi@br\tsi@dot\let\tsi@bm\tsi@dot\fi
  \else
    \iftsi@e\let\tsi@br\tsi@dot\tsi@bottrue\fi
  \fi
  \iftsi@b\else
    \iftsi@top
      \iftsi@l\let\tsi@tl\tsi@dot\let\tsi@tm\tsi@dot\fi
      \iftsi@r\let\tsi@tr\tsi@dot\let\tsi@tm\tsi@dot\fi
    \else
      \iftsi@e\let\tsi@tr\tsi@dot\tsi@toptrue\fi
    \fi
  \fi
  \ifx\tsi@theprimes\@empty\else\tsi@toptrue\fi
  \edef\tsi@fam{\fam\the\fam}%
  \tsi@choice
    {\tsi@endtensor\displaystyle\scriptstyle}%
    {\tsi@endtensor\textstyle\scriptstyle}%
    {\tsi@endtensor\scriptstyle\scriptscriptstyle}%
    {\tsi@endtensor\scriptscriptstyle\scriptscriptstyle}}

\def\tsi@endtensor#1#2{%
  \let\tsi@text#1\let\tsi@index#2%
  \setbox\z@ \hbox{$\tsi@fam\m@th#1\tsi@nucl_{x}$}%
  \setbox\@ne\hbox{$\tsi@fam\m@th#1\tsi@nucl{}_{x}$}%
  \tsi@dimen\wd\z@   \advance\tsi@dimen-\wd\@ne  
  \@tempdimb-\tsi@dimen
  \setbox\z@ \hbox{$\kern\tsi@dimen\tsi@fam\m@th#2\tsi@thesp$}%
  \setbox\@ne\hbox{$\tsi@fam\m@th#2\tsi@thesb$}%
  \setbox\tw@\hbox{$\tsi@fam\m@th#2{\tsi@theprimes}$}%
  \dp\z@\z@
  \ifdim\tsi@dimen>\wd\tw@
    \tsi@dimen\z@
  \else
    \advance\tsi@dimen-\wd\tw@
  \fi
  \wd\tw@\z@
  \iftsi@s
    \ifx\tsi@text\displaystyle
      \tsi@otrue\tsi@cfalse
    \else
      \tsi@ofalse\tsi@ctrue
    \fi
  \fi
  \let\tsi@a\@empty
  \tsi@nucl
  \iftsi@bot_{\kern-\tsi@dimen\copy\@ne}%
    \iftsi@o\tsi@toptrue\else\iftsi@c\tsi@toptrue
       \ifx\tsi@text\displaystyle\else
         \def\tsi@a{\raise.1ex}%
       \fi
       \kern-\wd\@ne\kern\tsi@dimen
       \kern\@tempdimb\kern-\scriptspace
    \fi\fi
  \fi
  \iftsi@top^{\tsi@a\box\tw@\kern-\tsi@dimen\tsi@a\box\z@}\fi}

\def\tsi@phantom#1{%
  \setbox\tw@\null
  \setbox\z@\hbox{$\tsi@fam\m@th\tsi@index#1$}%
  \wd\tw@\wd\z@
  \box\tw@}%

\def\whenindex#1#2#3{%
  \expandafter\def\csname tsi@cmd\expandafter\string#1\endcsname{%
    \def\tsi@theindex{#2}#3}}

\whenindex{'}{\prime}{}

\def\spadjust#1#2{%
  \def\tsi@a{#1}%
  \ifx\tsi@a\tsi@lastindex
   \iftsi@l\else
    \iftsi@r\else
     \ifx\tsi@addindex\tsi@addsp
      \if\tsi@lastloc b%
       \protected@edef\tsi@theindex{\mskip#2\thinmuskip\tsi@theindex}%
      \fi
     \fi
    \fi
   \fi
  \fi}

\def\sbadjust#1#2{%
  \def\tsi@a{#1}%
  \ifx\tsi@a\tsi@lastindex
   \iftsi@l\else
    \iftsi@r\else
     \ifx\tsi@addindex\tsi@addsb
      \if\tsi@lastloc p%
       \protected@edef\tsi@theindex{\mskip#2\thinmuskip\tsi@theindex}%
      \fi
     \fi
    \fi
   \fi
  \fi}

\def\omitdot{\tsi@nodottrue}
\def\finishdots{\tsi@nodottrue\let\tsi@nodotfalse\relax}

\catcode`^=\@tempcnta\catcode`_=\@tempcntb

\endinput

MIT License
-----------

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
