% mystyletau.sty
% Copyright 2026 Antonello Meccariello & Francesco Meccariello
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyletau}[2026/04/04 v2.0 Custom Tau glyphs for topology]

% Check for TeX engine compatibility
\RequirePackage{iftex}
\ifPDFTeX
  \PackageError{mystyletau}{This package requires LuaLaTeX or XeLaTeX}{%
  Please switch your engine to LuaLaTeX or XeLaTeX to use the custom fonts.}
\fi

\RequirePackage{fontspec}
\RequirePackage{amsmath}

\newfontfamily{\mytaufont}{mytautwo.ttf}[Scale=MatchLowercase]

\makeatletter

% --- HELPER PER ESTRARRE LA PRIMA LETTERA ---
% Questo trucco isola il primo carattere (es. X in X|Y) per calcolare il kerning corretto
% senza "sputare" il resto dei caratteri nel documento!
\def\vtau@check@first#1#2\@nil#3{%
  \ifx X#1_{\mkern -2.2mu #3}%
  \else\ifx A#1_{\mkern -2.5mu #3}%
  \else\ifx B#1_{\mkern -2mu #3}%
  \else\ifx D#1_{\mkern -2mu #3}%
  \else\ifx E#1_{\mkern -2mu #3}%
  \else\ifx F#1_{\mkern -2mu #3}%
  \else\ifx H#1_{\mkern -2mu #3}%
  \else\ifx I#1_{\mkern -2mu #3}%
  \else\ifx L#1_{\mkern -2mu #3}%
  \else\ifx M#1_{\mkern -2mu #3}%
  \else\ifx N#1_{\mkern -2mu #3}%
  \else\ifx P#1_{\mkern -2mu #3}%
  \else\ifx R#1_{\mkern -2mu #3}%
  \else\ifx S#1_{\mkern -2mu #3}%
  \else\ifx T#1_{\mkern -2mu #3}%
  \else\ifx Z#1_{\mkern -1.8mu #3}%
  \else\ifx \beta#1_{\mkern -1.8mu #3}%
  \else\ifx \rho#1_{\mkern -1.8mu #3}%
  \else\ifx \alpha#1_{\mkern -1.5mu #3}%
  \else_{#3}\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}

\def\atau@check@first#1#2\@nil#3{%
  \ifx X#1_{\mkern -2.2mu #3}%
  \else\ifx A#1_{\mkern -2.5mu #3}%
  \else\ifx B#1_{\mkern -2mu #3}%
  \else\ifx D#1_{\mkern -2mu #3}%
  \else\ifx E#1_{\mkern -2mu #3}%
  \else\ifx F#1_{\mkern -2mu #3}%
  \else\ifx H#1_{\mkern -2mu #3}%
  \else\ifx I#1_{\mkern -2mu #3}%
  \else\ifx L#1_{\mkern -2mu #3}%
  \else\ifx M#1_{\mkern -2mu #3}%
  \else\ifx N#1_{\mkern -2mu #3}%
  \else\ifx P#1_{\mkern -2mu #3}%
  \else\ifx R#1_{\mkern -2mu #3}%
  \else\ifx S#1_{\mkern -2mu #3}%
  \else\ifx T#1_{\mkern -2mu #3}%
  \else\ifx Z#1_{\mkern -1.8mu #3}%
  \else\ifx \beta#1_{\mkern -1.8mu #3}%
  \else\ifx \rho#1_{\mkern -1.8mu #3}%
  \else\ifx \alpha#1_{\mkern -1.5mu #3}%
  \else_{#3}\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}

% --- LOGICA DI SPAZIATURA ---
\newcommand{\vtau@kern@check}[1]{\vtau@check@first#1\relax\relax\@nil{#1}}
\newcommand{\atau@kern@check}[1]{\atau@check@first#1\relax\relax\@nil{#1}}

% --- DEFINIZIONE \vtau (E900) CON XPARSE ---
\newcommand{\vtau@base}{\text{\raisebox{-0.3ex}{\mytaufont\symbol{"E900}}}}

% Il comando e{_^} cattura automaticamente pedici e apici in qualsiasi ordine!
\NewDocumentCommand{\vtau}{ e{_^} }{%
  \ensuremath{%
    \vtau@base
    \IfValueT{#1}{\vtau@kern@check{#1}}% Se c'è un pedice, applica il nostro kerning
    \IfValueT{#2}{^{\mkern 3mu #2}}% <--- Se c'è un apice, lo sposta a DESTRA di 4mu
  }%
}

% --- DEFINIZIONE \atau (E901) CON XPARSE ---
\newcommand{\atau@base}{\text{\raisebox{-0.3ex}{\mytaufont\symbol{"E901}}}}

\NewDocumentCommand{\atau}{ e{_^} }{%
  \ensuremath{%
    \atau@base
    \IfValueT{#1}{\atau@kern@check{#1}}%
    \IfValueT{#2}{^{\mkern 3mu #2}}% <--- Spostamento a destra anche per atau
  }%
}
\makeatother

\endinput