%%
%% LaTeX package `boldtensors'
%%
%% This program can be redistributed and/or modified under the
%% terms of the GNU Public License, version 2.
%%
%% Copyright 1995 by Werner Fink und Juergen Bachteler
%% Copyright 2007 by Werner Fink
%% Copyright 2026 by Werner Fink
%%
%% 1995/01/10
%% Authors : Werner Fink und Juergen Bachteler
%%           Institut fuer Theoretische und Angewandte Physik,
%%           Universitaet Stuttgart
%%           Pfaffenwaldring 57/vi, 7000 Stuttgart 80 
%%
%% 2007/06/29
%% Author: Werner Fink <werner@suse.de>
%%
%% 2026/05/12 (Modernization), 2026/05/13 (Bug fixing),
%% 2026/05/21 (renaming isodiff to bt-isodiff)
%% Author: Werner Fink <werner@suse.de>
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{boldtensors}[2026/05/21 v2.2 Modernized Bold Tensors]

\RequirePackage{iftex}

% Option switches for nabla and differential operators
\newif\ifbt@nabla\bt@nablafalse
\newif\ifbt@diffop\bt@diffopfalse
\DeclareOption{nabla}{\bt@nablatrue}
\DeclareOption{differential}{\bt@diffoptrue}
\ProcessOptions\relax

\ifluatex
  % ==========================================
  % LuaLaTeX Path (Modern OpenType Math)
  % ==========================================
  \RequirePackage{unicode-math}
  % Special cases for ISO-compliant bold (upright)
  \def\bt@special@d{\symbfup{d}}
  \def\bt@special@0{\symbfup{0}}
  \def\bt@special@1{\symbfup{1}}

  % Upper greek letters (as in pdfLaTeX cmoperator)
  \expandafter\def\csname bt@special@\string\Gamma\endcsname{\symbfup{\Gamma}}
  \expandafter\def\csname bt@special@\string\Delta\endcsname{\symbfup{\Delta}}
  \expandafter\def\csname bt@special@\string\Theta\endcsname{\symbfup{\Theta}}
  \expandafter\def\csname bt@special@\string\Lambda\endcsname{\symbfup{\Lambda}}
  \expandafter\def\csname bt@special@\string\Xi\endcsname{\symbfup{\Xi}}
  \expandafter\def\csname bt@special@\string\Pi\endcsname{\symbfup{\Pi}}
  \expandafter\def\csname bt@special@\string\Sigma\endcsname{\symbfup{\Sigma}}
  \expandafter\def\csname bt@special@\string\Upsilon\endcsname{\symbfup{\Upsilon}}
  \expandafter\def\csname bt@special@\string\Phi\endcsname{\symbfup{\Phi}}
  \expandafter\def\csname bt@special@\string\Psi\endcsname{\symbfup{\Psi}}
  \expandafter\def\csname bt@special@\string\Omega\endcsname{\symbfup{\Omega}}

  \def\bt@math#1{%
    \ifcsname bt@special@\string#1\endcsname \csname bt@special@\string#1\endcsname
    \else \symbfit{#1}\fi}
  \def\bb@math#1{\symbb{#1}}
  
  \ifbt@nabla 
    \let\oldnabla\nabla 
    \renewcommand{\nabla}{\symbfup{\oldnabla}} 
  \fi
\else
  % ==========================================
  % pdfLaTeX Path (Native mathords, no boxes)
  % ==========================================
  \DeclareSymbolFont{cmtensor}  {OML}{cmm}{b}{it}
  \DeclareSymbolFont{cmoperator}{OT1}{cmr}{bx}{n}
  \DeclareSymbolFont{cmsymbols} {OMS}{cmsy}{b}{n}
  \DeclareSymbolFont{cmbboard}  {U}{bbold}{m}{n}

  % Lists for Greek letters and variants.
  % IMPORTANT: No spaces or indentations here, otherwise they become part of the macro name!
  \def\bt@greeklst{%
alpha/"0B,beta/"0C,gamma/"0D,delta/"0E,epsilon/"0F,zeta/"10,%
eta/"11,theta/"12,iota/"13,kappa/"14,lambda/"15,mu/"16,%
nu/"17,xi/"18,pi/"19,rho/"1A,sigma/"1B,tau/"1C,upsilon/"1D,%
phi/"1E,chi/"1F,psi/"20,omega/"21,%
varepsilon/"22,vartheta/"23,varpi/"24,varrho/"25,varsigma/"26,%
varphi/"27,imath/"7B,jmath/"7C,partial/"40}
  
  \def\bt@Greeklst{%
Gamma/"00,Delta/"01,Theta/"02,Lambda/"03,Xi/"04,Pi/"05,%
Sigma/"06,Upsilon/"07,Phi/"08,Psi/"09,Omega/"0A}

  \def\bt@symblst{aleph/"40}

  \def\bt@romlst{d/`d,0/"30,1/"31}

  % Helper macros: properly expanding \csname BEFORE \DeclareMathSymbol sees it
  \def\bt@split#1/#2\@nil#3{%
    \expandafter\DeclareMathSymbol\csname bt@#1\endcsname{\mathord}{#3}{#2}%
    \expandafter\DeclareMathSymbol\csname bt@\expandafter\string\csname #1\endcsname\endcsname{\mathord}{#3}{#2}%
  }
  \def\bb@split#1/#2\@nil#3{%
    \expandafter\DeclareMathSymbol\csname bb@#1\endcsname{\mathord}{#3}{#2}%
    \expandafter\DeclareMathSymbol\csname bb@\expandafter\string\csname #1\endcsname\endcsname{\mathord}{#3}{#2}%
  }

  % Process lowercase Greek and variants (Bold & Blackboard)
  \@for\bt@entry:=\bt@greeklst\do{%
    \expandafter\bt@split\bt@entry\@nil{cmtensor}%
    \expandafter\bb@split\bt@entry\@nil{cmbboard}%
  }
  
  % Process uppercase Greek (Bold)
  \@for\bt@entry:=\bt@Greeklst\do{%
    \expandafter\bt@split\bt@entry\@nil{cmoperator}%
  }

  % Process Hebrew and special symbols (Bold)
  \@for\bt@entry:=\bt@symblst\do{%
    \expandafter\bt@split\bt@entry\@nil{cmsymbols}%
  }

  % Process the upright list
  \@for\bt@entry:=\bt@romlst\do{%
    \expandafter\bt@split\bt@entry\@nil{cmoperator}%
  }

  % Individual assignments
  \ifbt@nabla \expandafter\DeclareMathSymbol\csname bt@nabla\endcsname{\mathord}{cmsymbols}{"72} \fi
  \expandafter\DeclareMathSymbol\csname bt@1\endcsname{\mathord}{cmoperator}{"31}
  \expandafter\DeclareMathSymbol\csname bt@0\endcsname{\mathord}{cmoperator}{"30}

  % Core assignment logic
  \def\bt@math#1{%
    \ifcsname bt@\string#1\endcsname \csname bt@\string#1\endcsname 
    \else \mathord{\btensor#1}\fi}
  \def\bb@math#1{%
    \ifcsname bb@\string#1\endcsname \csname bb@\string#1\endcsname 
    \else \mathord{\bboard#1}\fi}
  
  \DeclareSymbolFontAlphabet{\btensor}{cmtensor}
  \DeclareSymbolFontAlphabet{\bboard}{cmbboard}
\fi

% Load the new ISO differential style if requested
\ifbt@diffop
  \RequirePackage{bt-isodiff}
\fi

% ==========================================
% Active Character Implementation ('~' and '"')
% ==========================================
\def\outer@boldtensor{\catcode`\"=13 \catcode`\~=13 }
\def\inner@boldtensor{\mathcode`\"="8000 \mathcode`\~="8000 }

\begingroup
  \outer@boldtensor
  \gdef\boldtensor{%
     \let\bt@text=~ \let\bb@text="
     \def~##1{\ifmmode\bt@math{##1}\else\bt@text##1\fi}
     \def"##1{\ifmmode\bb@math{##1}\else\bb@text##1\fi}
     \inner@boldtensor}
  \AtBeginDocument{\boldtensor}
\endgroup
%
% Just the end
%
\endinput
