\NeedsTeXFormat{LaTeX2e}

% Based originally on Young Ryu's txfonts.sty, v.1.1.1
%
% Changes from original txfonts.sty by Michael Sharpe, msharpe at ucsd dot edu;
% Text and math versions now separate. 
% Added scaled keyword and osf option;
% Text is now directed to an extension of TeXGyreTermes rather than tx.
% 1.7 is first version of newtxtext to support both [pdf]latex and unicode latex.
\def\ntx@showstate{0} % turn off state debugging---1 for on
%\def\ntx@state{0} % 0 for both, 1 for tutex only, 2 for legacy only
\def\setstate#1{\if 1\ntx@showstate\typeout{State=#1}\fi}

\def\fileversion{1.744}
\def\filedate{2024/04/01}
\ProvidesPackage{newtxtext}[%
\filedate\space v\fileversion (Michael Sharpe) latex and unicode latex support for TeXGyreTermesX]

\message{`newtxtext' v\fileversion, \filedate\space Text macros taking advantage of TeXGyre Termes and its extensions (msharpe)}
\RequirePackage{xpatch,xcolor} % need to patch \scshape
\RequirePackage{iftex,xkeyval,etoolbox,textcomp}
\RequirePackage{xstring,ifthen,scalefnt} % for \textfrac
\RequirePackage{mweights} % should be in latex kernel
\DeclareEncodingSubset{TS1}{ntx*}{0}
\input{binhex}

\newcount\ntx@tmpcnta
\newcount\ntx@cnt \ntx@cnt=0

%KOMA check
\newif\ifntx@KOMA
\@ifundefined{ftntm@font}{}{\ntx@KOMAtrue}
%\@ifundefined{ver@scrextend.sty}{}{\ntx@KOMAtrue}
%LTX check
\newif\ifntx@LTX
\@ifclassloaded{article}{\ntx@LTXtrue}{}
\@ifclassloaded{report}{\ntx@LTXtrue}{}
\@ifclassloaded{book}{\ntx@LTXtrue}{}
\newif\ifntx@AMSloaded
\@ifclassloaded{amsart}{\ntx@AMSloadedtrue}{}
\@ifclassloaded{amsproc}{\ntx@AMSloadedtrue}{}
\@ifclassloaded{amsbook}{\ntx@AMSloadedtrue}{}

% Engine check
\newif\ifntx@newtx
\@ifundefined{ntx@otfmathtrue}{\newif\ifntx@otfmath}{}
 % default is type1 math
%
\@ifundefined{ntx@otftexttrue}{% not run from newtx.sty
   \newif\ifntx@otftext
   \iftutex\ntx@otftexttrue\fi %  otf text for unicode 
%iftex's \iftutex returns true for LuaTeX and XeTeX, not [pdf]latex
  %\newif\ifntx@nofontspec 
  % default is to load fontspec iftutex  
  % might have been defined in newtx.sty
  \def\ntx@id{0} % make it appear that newtxtext was chosen in newtx
}{% run from newtx.sty
  \ntx@newtxtrue%
  %\ifntx@otftext\typeout{ntx@otftexttrue.}\else\typeout{ntx@otftextfalse.}\fi
}
%%%%%%%%%%%%%%%%%%
%
\newif\ifntx@nomath\ntx@nomathfalse % load fontspec with no-math

\newif\ifntx@lining\ntx@liningtrue
\newif\ifntx@tabular\ntx@tabulartrue % default is tabular lining figures
%\@ifundefined{ver@newtxmath.sty}{\iftutex\ntx@otfmathtrue\fi}{}

\DeclareOptionX{type1}[true]{}
%{\def\ntx@tmpx{\@nameuse{if#1}\ntx@otftextfalse\fi}
\DeclareOptionX{type1text}[true]{}

\DeclareOptionX{nofontspec}[true]{}

\DeclareOptionX{no-math}[true]{%
\@nameuse{if#1}\ntx@nomathtrue\fi
}
\DeclareOptionX{otfmath}[true]{%
\@nameuse{if#1}\ntx@otfmathtrue\fi
}
\def\ntx@defaultfeatures{}
\DeclareOptionX{defaultfeatures}{\def\ntx@defaultfeatures{#1}}

%Figure style options
\def\ntx@dfltfigurestyle{lf}
\def\ntx@dfltfigurealign{t}

\DeclareOptionX{lining}[true]{%
\@nameuse{ntx@lining#1}
}
\DeclareOptionX{lf}[true]{%
\@nameuse{ntx@lining#1}
}
\DeclareOptionX{oldstyle}[true]{%
\@nameuse{if#1}%
  \ntx@liningfalse
\else
  \ntx@liningtrue
\fi
}
\DeclareOptionX{osf}[true]{%
\@nameuse{if#1}%
  \ntx@liningfalse
\else
  \ntx@liningtrue
\fi
}
\DeclareOptionX{tabular}[true]{%
\@nameuse{ntx@tabular#1}
}
\DeclareOptionX{t}[true]{%
\@nameuse{ntx@tabular#1}
}
\DeclareOptionX{proportional}[true]{%
\@nameuse{if#1}%
  \ntx@tabularfalse
\else
  \ntx@tabulartrue
\fi
}
\DeclareOptionX{p}[true]{%
\@nameuse{if#1}%
  \ntx@tabularfalse
\else
  \ntx@tabulartrue
\fi
}

%Scale
\newcommand*{\ntx@scale}{1}  % actual scaled command in \ntx@scaled
\DeclareOptionX{scale}{\renewcommand*{\ntx@scale}{#1}}
\DeclareOptionX{scaled}{\renewcommand*{\ntx@scale}{#1}}
%\newdimen\ntxsups@scale
%\newdimen\ntxsups@raise
%\def\ntxsups@raised{\z@}
%\def\ntxsups@Lspaced{.03em}
%\def\ntxsups@Rspaced{.03em}
%\def\ntxsups@scaledval{1}
%\def\ntxsups@scalefactor{1}
%\newcommand*{\ntxsups@color}{black}
\newif\ifntx@supsflag
\DeclareOptionX{supscaled}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@scaledval}{#1}}%added 2023/09/16
\DeclareOptionX{supscale}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@scaledval}{#1}}%added 2023/09/16
\DeclareOptionX{supsraised}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@raised}{#1}}%added 2023/09/16
\DeclareOptionX{supLspaced}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@Lspaced}{#1}}%added 2023/09/16
\DeclareOptionX{supRspaced}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@Rspaced}{#1}}%added 2023/09/16
\DeclareOptionX{supspaced}{\ntx@supsflagtrue}%
\DeclareOptionX{supscolor}{\ntx@supsflagtrue}%{\renewcommand*{\ntxsups@color}{#1}} %added 2023/09/16

\newif\ifntx@defaultsups
%\newif\ifntx@adobesc % we don't use this exc. in fd, but may in future
\newif\ifntx@scosf
\newif\ifntx@largesc 
\newif\ifntx@helv \ntx@helvtrue % default is to load helvetica as sans serif font
\newif\ifntx@tt \ntx@tttrue % default is to load ntxtt as typewriter font
\newif\ifntx@thrm
\newif\ifntx@sl 
\newif\ifntx@oldsu\ntx@oldsutrue %use old versions of sups/subs
\def\ntx@suver{alt} % sups version, alt (the old version) or {} for new version
% Other options
\DeclareOptionX{defaultsups}[true]{%
\@nameuse{ntx@defaultsups#1}
}
\DeclareOptionX{babel}[true]{%
\@nameuse{ntx@babel#1}% for backward compatibility
}
\DeclareOptionX{largesc}[true]{%
\@nameuse{ntx@largesc#1}
}
\DeclareOptionX{nohelv}[true]{%
\@nameuse{if#1}\ntx@helvfalse\fi}%

\DeclareOptionX{nott}[true]{%
\@nameuse{if#1}\ntx@ttfalse\fi}%

\def\ntx@helvratio{.94}
\DeclareOptionX{helvratio}[0.94]{%
\def\ntx@helvratio{#1}
}
\def\ntx@spcmag{1}
\def\ntx@spc{.25em}
\def\ntx@stretch{.2em}
\def\ntx@shrink{.1em}
\def\ntx@nospc{T}

\DeclareOptionX{spcfactor}{%
\def\ntx@spcmag{#1}\def\ntx@nospc{F}
}
\DeclareOptionX{spacing}{%
\def\ntx@spc{#1}
}
\DeclareOptionX{stretch}{%
\def\ntx@stretch{#1}
}
\DeclareOptionX{shrink}{%
\def\ntx@shrink{#1}
}
\def\ntx@thmshape{i} % other option is slanted
\def\ntx@defaultfeatures{}
\newif\ifntx@zeroslash
\def\ntx@thmlin{X} % default is to not change to lining
\def\ntx@noti{T}\def\ntx@nolo{T}
\DeclareOptionX{newsu}{\def\ntx@suver{}\ntx@oldsufalse}% added 2023/09/16
\DeclareOptionX{defaultfeatures}{\def\ntx@defaultfeatures{#1}}
\DeclareOptionX{oldSS}{\advance\ntx@cnt 32 }
\DeclareOptionX{slashedzero}{\ntx@zeroslashtrue}
\DeclareOptionX{theoremfont}{\ntx@thrmtrue}
\DeclareOptionX{thmslshape}{\def\ntx@thmshape{s}}
\DeclareOptionX{thmlining}{\edef\ntx@thmlin{l}}
\DeclareOptionX{trueslanted}{\ntx@sltrue}
\DeclareOptionX{scosf}{\ntx@scosftrue}
\DeclareOptionX{tighter}{% 
\gdef\ntx@spc{.25em}\gdef\ntx@stretch{.125em}%
\gdef\ntx@shrink{.1em}\def\ntx@noti{F}}
\DeclareOptionX{looser}{%
\gdef\ntx@spc{.3em}\gdef\ntx@stretch{.2em}%
\gdef\ntx@shrink{.1em}\def\ntx@nolo{F}}
%\traceon
%\DeclareOptionX{sfracvcenter}{%
%\def\ntx@sfrVC{#1}}
%\DeclareOptionX{sfracvspacing}{%
%\def\ntx@sfrVS{#1}}
%\DeclareOptionX{sfracbarthick}{%
%\def\ntx@sfrTH{#1}}
%\DeclareOptionX{sfracscaling}{%
%\def\ntx@sfrSC{#1}}
\ProcessOptionsX\relax

\ifntx@supsflag%
\PackageWarningNoLine{newtxtext}{sups options no longer handled---use superiors package (v2) instead.}\fi

\ifthenelse{\not\equal{\sfdefault}{cmss}}%
  {\ntx@helvfalse}{}
\ifthenelse{\not\equal{\ttdefault}{cmtt}}%
  {\ntx@ttfalse}{}
\ifntx@helv
  \renewcommand*{\sfdefault}{qhv}
  \renewcommand*{\sfdefault}{qhv}
  \@tempdima=\ntx@scale\p@
  \@tempdima=\ntx@helvratio\@tempdima%
  \edef\qhv@scale{\strip@pt\@tempdima}
\fi
\ifntx@tt\renewcommand*{\ttdefault}{ntxtt}\fi

% otfmath beats no-math
\ifntx@otfmath\ntx@nomathfalse\fi
%\setlength{\ntxsups@raise}{\ntxsups@raised}
%\setlength{\ntxsups@scale}{\ntxsups@scaledval\p@}
%\ntxsups@scale=\ntx@scale\ntxsups@scale %
%\def\ntxsups@scalefactor{\strip@pt\ntxsups@scale}
%\def\ntxsups@scaled{s*[\ntxsups@scalefactor]}

%\ifntx@otftext \typeout{ntx@otftexttrue}\else\typeout{ntx@otftextfalse}\fi
\ifntx@newtx\let\ntx@scale=\ntxtext@scale\fi
% following lines required by newtxmath
% They must follow the definition of \ntx@scaled
\edef\ntx@scaled{s*[\ntx@scale]}% used in fd files
\providecommand*{\ntx@minfam}{minntx} % won't override value from newtx

%\RequirePackage[T1]{fontenc} % do we need this?
\renewcommand{\rmdefault}{\ntx@minfam} % the default until end of preamble
\renewcommand{\familydefault}{\rmdefault} % Cause the fd files to load

\def\re@DeclareTextSymbol#1#2#3{%
   \let#1=\undefined
   \DeclareTextSymbol{#1}{#2}{#3}}

\iftutex
\setstate1
 \@ifpackageloaded{fontspec}{}{%
  \ifntx@nomath
    \RequirePackage[no-math]{fontspec}
  \else
    \RequirePackage{fontspec}
  \fi%
 } 
  %\ifxetex\XeTeXtracingfonts=1\fi % causing unwanted warnings in Tectonic system
  \ifluatex  % avoid possibly uncorrected "system" fonts
    \directlua{fonts.names.set_location_precedence{
        "local", "texmf", "system" }}
  \fi
\fi

\ifntx@otftext   
\setstate1
  \def\ntx@base{TeXGyreTermesX}
  \ifntx@scosf
  	\AtBeginDocument{%
	    \xpretocmd{\scshape}
  	    {\addfontfeatures{Numbers=OldStyle}}
  	    {}{}%
	}%
  \fi
  \newcommand{\textfrac}[3][]{{%\normalfont%
  \mbox{%
    \ifthenelse{\not\equal{#1}{}}%Test for integer portion [optional #1]
      {{\lfstyle#1}\kern.06em}{}%
  \addfontfeatures{RawFeature=+numr}#2\char"2044% numerator
  \addfontfeatures{RawFeature=-numr;+dnom;+ss20}\char"2044{}#3% 
}}}
  
\else % type1
\setstate2

  \ifntx@scosf
  	\AtBeginDocument{%
	    \xpretocmd{\scshape}
  	     {\fontfamily{ntx\ntx@figurealign osf}}
  	     {}{}%
	}%
  \fi%
 % for possible use in footnote markers
%  \DeclareFontFamily{OT1}{zsups}{}%
%  \DeclareFontShape{OT1}{zsups}{m}{n}{%
%    <-> s*[\ntxsups@scalefactor] ntxsups-Regular-ot1
%}{}
%  \DeclareFontFamily{T1}{zsups}{}%
%  \DeclareFontShape{T1}{zsups}{m}{n}{%
%    <-> s*[\ntxsups@scalefactor] ntxsups-Regular-t1
%}{}
%\AtEndPreamble{%
%  \@ifundefined{T@LY1}{}% true branch
%  {%false branch---LY1 encoding has been declared
%  \DeclareFontFamily{LY1}{zsups}{}%
%  \DeclareFontShape{LY1}{zsups}{m}{n}{%
%    <-> s*[\ntxsups@scalefactor] ntxsups-Regular-ly1
%}{}
%}}
%  \DeclareFontFamily{TS1}{zsups}{}%
%  \DeclareFontShape{TS1}{zsups}{m}{n}{%
%    <-> s*[\ntxsups@scalefactor] ntxsups-Regular-ts1
%}{}
%\DeclareRobustCommand{\supstyle}{%
%  \not@math@alphabet\supstyle\relax
%  \usefont{\f@encoding}{zsups}{m}{n}}
%
%\ifdim\ntxsups@raise=\z@
%	\DeclareRobustCommand{\textsups}[1]{{\supstyle#1}}
%\else
%	\DeclareRobustCommand{\textsups}[1]{%
%	\raisebox{\ntxsups@raised}{\hbox{{\supstyle#1}}}}
%\fi
\RequirePackage{fontaxes}
    \fa@naming@exception{figures}{{superior}{proportional}}{sups}
    \fa@naming@exception{figures}{{superior}{tabular}}{sups}
   \DeclareRobustCommand{\sustyle}{%
   \not@math@alphabet\sustyle\relax
   \fontfamily{ntxsups\ntx@suver}\selectfont}
 \NewCommandCopy { \sufigures } { \sustyle }
%   \let\sufigures=\sustyle%%%%%
 
   \fa@naming@exception{figures}{{inferior}{proportional}}{inf}
   \fa@naming@exception{figures}{{inferior}{tabular}}{inf}
%    \def\infigures{\@nomath\infigures
%        \fontfigurestyle{inferior}\selectfont}
%     \let\instyle=\infigures
%    \DeclareTextFontCommand{\textinf}{\infigures}
%    \let\textinferior\textinf
   \DeclareRobustCommand{\infstyle}{%
   \not@math@alphabet\infstyle\relax
   \fontfamily{ntxinf\ntx@suver}\selectfont}
 \NewCommandCopy { \infigures } { \infstyle }
 \NewCommandCopy { \instyle } { \infstyle }
%   \let\infigures=\infstyle%%%%%
%	\let\instyle\infstyle
	
   \fa@naming@exception{figures}{{numerators}{proportional}}{numr}
   \fa@naming@exception{figures}{{numerators}{tabular}}{numr}

   \DeclareRobustCommand{\nustyle}{%
   \not@math@alphabet\nustyle\relax
   \fontfamily{ntxnumr}\selectfont}
 \NewCommandCopy { \nufigures } { \nustyle }
%   \let\nufigures=\nustyle
 
   \fa@naming@exception{figures}{{denominators}{proportional}}{dnom}
   \fa@naming@exception{figures}{{denominators}{tabular}}{dnom}
%    \def\defigures{\@nomath\defigures
%        \fontfamily{defigures}\selectfont}
%     \let\destyle=\defigures
%    \DeclareTextFontCommand{\textde}{\defigures}
%    \let\textdenominators\textde
   \DeclareRobustCommand{\destyle}{%
   \not@math@alphabet\destyle\relax
   \fontfamily{ntxdnom}\selectfont}
 \NewCommandCopy { \defigures } { \defstyle }
   %\let\defigures=\destyle

%    \fa@naming@exception{figures}{{numerators}{proportional}}{numr}
%    \fa@naming@exception{figures}{{numerators}{tabular}}{numr}
%    \def\defigures{\@nomath\nufigures
%        \fontfigurestyle{numerators}\selectfont}
%     \let\nustyle=\nufigures
%    \DeclareTextFontCommand{\textnum}{\nufigures}
%    \let\textnumerators\textnum
%  \DeclareRobustCommand{\nustyle}{%
%   \not@math@alphabet\nustyle\relax
%   \fontfamily{ntxnumr}\selectfont}
%   \let\nufigures=\nustyle

\newcommand{\textfrac}[3][]{% like \textfrac[1]{7}{8}
  \mbox{%
    \ifthenelse{\not\equal{#1}{}}%Test for integer portion [optional #1]
      {\textlf{#1}\kern.1em}{}%         
    {\nustyle#2}% numerator
    \StrRight{#2}{1}[\@digit@]%
    % look for  1, 2, 6, 7
    \ifcase\@digit@% 0
    \or%\kern.03em% 1
    \or\kern.04em% 2
    \or%\kern-.01em% 3
    \or%\kern.02em% 4
    \or%\kern.01em% 5
    \or\kern.01em% 6
    \or\kern-.06em% 7
     \or%\kern-.11em% 8
    \or\kern-.02em% 9
   \fi%
%    \kern\ntx@foresolidus{\fontencoding{TS1}\selectfont\char"2F}%
%    \kern\ntx@aftsolidus%
    {\fontencoding{TS1}\selectfont\char"2F}%
	\StrLeft{#3}{1}[\@digit@]%
    % look for 1, 4, 7
    \ifcase\@digit@% 0
    \or%\kern -.02em% 1
    \or% 2
    \or% 3
    \or\kern -.05em% 4
    \or% 5
    \or\kern-.02em% 6
    \or\kern .01em% 7
    \fi%
    {\destyle #3}%       denominator [#3]
  }%mbox
} % textfrac
\fi
\setstate0

%\let\orig@footnote=\thefootnote
%\let\orig@makefnmark=\@makefnmark
\def\bfseries@rm{b}
\def\mdseries@rm{m}

\def\useosf{\ntx@liningfalse}
\@onlypreamble\useosf
\def\useproportional{\ntx@tabularfalse}
\@onlypreamble\useproportional

\ifntx@otftext
\setstate1
  \def\ntx@dflt{}
  \ifntx@zeroslash\edef\ntx@dflt{Numbers = SlashedZero , 
  }\fi
  \if T\ntx@nospc
    \if F\ntx@noti\def\ntx@spcmag{{1,.625,1}}\def\ntx@nospc{F}\fi
    \if F\ntx@nolo\def\ntx@spcmag{{1.2,1,1}}\def\ntx@nospc{F}\fi  
  \fi
  \if F\ntx@nospc
  	\edef\ntx@dflt{\ntx@dflt WordSpace = \ntx@spcmag ,
	}
  \fi
  \def\ntx@scraw{pcap}
  \ifntx@largesc\def\ntx@scraw{smcp}\fi
  
 %\ntx@cnt will be 0..2^8-1 
  \edef\ntx@lst{}
  \ifnum\ntx@cnt>127 \edef\ntx@lst{8}\advance\ntx@cnt -128 \fi
  \ifnum\ntx@cnt>63 \edef\ntx@lst{7,\ntx@lst}\advance\ntx@cnt -64 \fi
  \ifnum\ntx@cnt>31 \edef\ntx@lst{6,\ntx@lst}\advance\ntx@cnt -32 \fi
  \ifnum\ntx@cnt>15 \edef\ntx@lst{5,\ntx@lst}\advance\ntx@cnt -16 \fi
  \ifnum\ntx@cnt>7 \edef\ntx@lst{4,\ntx@lst}\advance\ntx@cnt -8 \fi
  \ifnum\ntx@cnt>3 \edef\ntx@lst{3,\ntx@lst}\advance\ntx@cnt -4 \fi
  \ifnum\ntx@cnt>1 \edef\ntx@lst{2,\ntx@lst}\advance\ntx@cnt -2 \fi
  \ifnum\ntx@cnt>0 \edef\ntx@lst{1,\ntx@lst}\fi
  \ifthenelse{\not\equal{\ntx@lst}{}}
	{\edef\ntx@dflt{\ntx@dflt StylisticSet={\ntx@lst} , 
	}}{}
  
   \defaultfontfeatures{
     Ligatures = TeX ,
	Scale = \ntx@scale ,
     Extension = .otf ,
     SmallCapsFeatures={\ifntx@scosf Numbers = OldStyle ,
     \fi RawFeature = +\ntx@scraw } ,
     \ifluatex Renderer=Basic ,\fi
     \ntx@dflt ,
     RawFeature=-ss05 ,   
     \ntx@defaultfeatures ,
    }
 
 \else % type1
 \setstate2

  \def\ntx@spacing{%
	\fontdimen2\font=\ntx@spc
	\fontdimen3\font=\ntx@stretch
	\fontdimen4\font=\ntx@shrink}
  %\show\ntx@scaled
  %\def\oldstylenums#1{{\fontencoding{TS1}\selectfont #1}}

    \DeclareRobustCommand{\lfstyle}{%
      \not@math@alphabet\lfstyle\relax
      \fontfamily{ntxlf}\selectfont %
    }
    \DeclareRobustCommand{\tlfstyle}{%
      \not@math@alphabet\tlfstyle\relax
      \fontfamily{ntxtlf}\selectfont %
    }
    \DeclareRobustCommand{\osfstyle}{%
      \not@math@alphabet\osfstyle\relax
      \fontfamily{ntxosf}\selectfont %
    }
    \DeclareRobustCommand{\tosfstyle}{%
      \not@math@alphabet\tosfstyle\relax
      \fontfamily{ntxtosf}\selectfont %
    }    
    \ifntx@defaultsups %
    \else\fi
%    \ExplSyntaxOn
%	%\@textsuperscript is defined in latex.ltx to render text at about 70% size, at math superscript position
%	\DeclareDocumentCommand \textsuperscript {m} {
%	    \hbox_set:Nn \l_tmpa_box {{\sustyle #1}}
%		\box_scale:Nnn \l_tmpa_box { \ntxsups@scalefactor } { \ntxsups@scalefactor } 
%	    \hbox_set:Nn \l_tmpa_box
%    { \box_move_up:nn \ntxsups@raised {\box_use:N \l_tmpa_box} }
%	    \hspace*{\ntxsups@Lspaced}{\color{\ntxsups@color}\box_use:N \l_tmpa_box} \hspace*{\ntxsups@Rspaced}}%  
%	
%
%	\cs_set:Npn \@makefnmark {
%		\mbox{%
%		\footnotemarkfont\textsuperscript{\@thefnmark}}
%	  }
%	\cs_if_exist:NTF \ftntm@font % will exist only for KOMA
%	  {
%	\cs_new:Npn \footnotemarkfont {\normalfont \ftntm@font}
%	  }
%	  {
%	\cs_new:Npn \footnotemarkfont {\normalfont}
%	  }
%	    \ExplSyntaxOff
%    \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\normalfont{\textsuperscript\@thefnmark}}{}{}
%    \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\normalfont{\textsuperscript\@thefnmark}}{}{}
%%    \patchcmd{\maketitle}     
%%{\hb@xt@1.8em}{\hb@xt@1.8em}{}{} %2.18?
%    \ifntx@KOMA
%      \setkomafont{footnotereference}{\normalfont\sustyle}
%      \setkomafont{footnotelabel}{\normalfont\sustyle}
%    \fi
%  \fi
%      \ifntx@KOMA
%      	%\let\textsuperscript\sustyle
%      \else % NOT KOMA
%        \def\f@@tn@te{footnote}
%        \def\@makefnmark{%
%          \ifx\@mpfn\f@@tn@te%
%            \ifx\thefootnote\orig@footnote%
%              \hbox{\sustyle\hspace*{.05em}\@thefnmark\hspace*{.05em}}%
%            \else%
%              \orig@makefnmark%
%            \fi
%          \else%
%            \orig@makefnmark%
%          \fi}%
%        \fi
%    \fi
  \DeclareRobustCommand\thfamily % new family for theorem font
        {\not@math@alphabet\thfamily\relax
         \fontfamily\thdefault\selectfont}
\fi % type1
\setstate0

\ifntx@thrm
	%\RequirePackage{amsmath,amsthm} % in case of theoremfont
	\ifdefined\newtheoremstyle %amsthm and ntheorem, not theorem.sty
		\@ifpackageloaded{ntheorem}\relax{%
		\newtheoremstyle{plain}
		{\medskipamount}   % ABOVESPACE, was \topsep
    	{\medskipamount}   % BELOWSPACE, was \topsep
    	{\thfamily}  % BODYFONT--\thfamily is italic with upright figures and punctuation
    	{}       % INDENT (empty value is the same as 0pt)
    	{\bfseries} % HEADFONT--you may want to add \tlfstyle so the number is tab lining, regardless of text figure selection
    	{.}         % HEADPUNCT
    	{5pt plus 1pt minus 1pt} % HEADSPACE
    	{}          % CUSTOM-HEAD-SPEC\newtheorem{thm}{Theorem}[section]
    	} % end @ifpackageloaded
    \fi%
\fi%}		

\ifntx@otftext
\setstate1

%	\def\ntx@nft{\if t\ntx@thmtab Monospaced%
%	\else Proportional\fi}
   \DeclareRobustCommand{\lfstyle}{%
      \not@math@alphabet\lfstyle\relax
      \addfontfeatures{Numbers={Proportional,Lining}}%
    }
    
   \DeclareRobustCommand{\tlfstyle}{%
      \not@math@alphabet\tlfstyle\relax
      \addfontfeatures{Numbers={Monospaced,Lining}}%
    }
   \DeclareRobustCommand{\osfstyle}{%
      \not@math@alphabet\osfstyle\relax
      \addfontfeatures{Numbers={Proportional,OldStyle}}%
    }
   \DeclareRobustCommand{\tosfstyle}{%
      \not@math@alphabet\tosfstyle\relax
      \addfontfeatures{Numbers={Monospaced,OldStyle}}%
    }
   \DeclareRobustCommand{\oldstylenums}{%
      \not@math@alphabet\oldstylenums\relax
      \addfontfeatures{Numbers=OldStyle}%
    }
   \DeclareRobustCommand{\liningnums}{%
      \not@math@alphabet\liningnums\relax
      \addfontfeatures{Numbers=Lining}%
    }
   \DeclareRobustCommand{\tabularnums}{%
      \not@math@alphabet\tabularnums\relax
      \addfontfeatures{Numbers=Monospaced}%
    }
   \DeclareRobustCommand{\proportionalnums}{%
      \not@math@alphabet\proportionalnums\relax
      \addfontfeatures{Numbers=Proportional}%
    }
   \DeclareRobustCommand{\destyle}{%
      \not@math@alphabet\destyle\relax
      \addfontfeatures{RawFeature=-pnum;-onum;+dnom}%
    }
   \DeclareRobustCommand{\nustyle}{%
      \not@math@alphabet\nustyle\relax
      \addfontfeatures{RawFeature=-pnum;-onum;+numr}%
    }
   \DeclareRobustCommand{\sustyle}{%
      \not@math@alphabet\sustyle\relax
      \addfontfeatures{RawFeature=-pnum;-onum;+sups}%
    }
   \DeclareRobustCommand{\infstyle}{%
      \not@math@alphabet\infstyle\relax
      \addfontfeatures{RawFeature=-pnum;-onum;+subs}%
    }
\ifntx@oldsu
   \NewCommandCopy { \sustyle } { \nustyle }
   \NewCommandCopy { \infstyle } { \destyle }
\fi
\NewCommandCopy { \sufigures } { \sustyle }
\NewCommandCopy { \nufigures } { \nustyle }
\NewCommandCopy { \infigures } { \infstyle }
\NewCommandCopy { \defigures } { \destyle }
%   \let\sufigures\sustyle
%   \let\nufigures\nustyle
%   \let\infigures\infstyle
%   \let\defigures\destyle

   \ifntx@defaultsups %
   \else
   \fi
%      \ExplSyntaxOn
%    \RenewDocumentCommand \textsuperscript {m} {
%	    \hbox_set:Nn \l_tmpa_box {\textsu{\tlfstyle #1}}
%		\box_scale:Nnn \l_tmpa_box { \ntxsups@scalefactor } { \ntxsups@scalefactor } 
%	    \hbox_set:Nn \l_tmpa_box
%    { \box_move_up:nn \ntxsups@raised {\box_use:N \l_tmpa_box} }
%	    \hspace*{\ntxsups@Lspaced}{\color{\ntxsups@color}\box_use:N \l_tmpa_box} \hspace*{\ntxsups@Rspaced}}	
%	    
%\cs_set:Npn \@makefnmark {
%  \mbox{\footnotemarkfont\textsuperscript{\@thefnmark}}
%}
%\cs_if_exist:NTF \ftntm@font % KOMA only
%{
%  \cs_new:Npn \footnotemarkfont {\ftntm@font}
%}
%{
%  \cs_new:Npn \footnotemarkfont {\normalfont}
%}
%          \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\normalfont{\textsuperscript\@thefnmark}}{}{}
%     \patchcmd{\maketitle}{\@textsuperscript{\normalfont\@thefnmark}}{\normalfont{\textsuperscript\@thefnmark}}{}{}
%%         \patchcmd{\maketitle}     
%%{\hb@xt@1.8em}{\hb@xt@1.8em}{}{} % 2.18?
%    \ExplSyntaxOff
%    \ifntx@KOMA
%      \setkomafont{footnotereference}{\normalfont\sustyle}
%      \setkomafont{footnotelabel}{\normalfont\sustyle}
%    \fi
%   \fi% end of tutex footnote handling
   %   \else % tutex+LTX (article,book,report)
%     \patchcmd[]{\maketitle}{\normalfont\@thefnmark}{\@thefnmark}{\message{Successfully patched maketitle command}}{\message{Unable to patch maketitle command}}
%     \patchcmd[]{\maketitle}{\normalfont\@thefnmark}{\@thefnmark}{\message{Successfully patched maketitle command}}{\message{Unable to patch maketitle command}}
%    \def\@makefnmark%
%       {\hbox{\@textsuperscript{\@thefnmark}}}
%     \DeclareRobustCommand*\textsuperscript[1]{%
%       \@textsuperscript{#1}}
%     \def\@textsuperscript#1{%
%       {\normalfont{\sustyle#1}}
%   \fi
   \renewcommand*{\textcircled}[1]{% 
	\StrLen{#1}[\ntx@tmpa]\ntx@tmpcnta=\ntx@tmpa\relax
	\ifnum\ntx@tmpcnta>2\relax#1%
	\else
	  \ifnum\ntx@tmpcnta=\@ne%
\mbox{\normalfont\addfontfeatures{RawFeature=+ss08}#1}%	
	  \else % argument has length 2
	    \IfInteger{#1}{%
	      \ntx@tmpcnta=#1\relax%
	    %\ifnum\ntx@tmpcnta<10\relax--must be >=10
	    %  #1
	    %\else % >=10
	      \ifnum\ntx@tmpcnta>20\relax#1%
	      \else % between 10 and 20
	        \advance\ntx@tmpcnta 9311\relax%
	        \symbol{"\nhex{4}{\the\ntx@tmpcnta}}
	      \fi
	    %\fi %\ifnum
	    }{#1}  % \IfInteger
	  \fi %ifnum
	\fi
	}
	  %\ntx@tmpcnta=9321 % decimal value of "2469 (uni2469 is circled{10})
	  %\ntx@cnt=#1 \advance\count@\the\count1 \advance\count@ -10\relax
%	  \symbol{"\nhex{4}{\the\count@}}
%    \renewcommand*{\textcircled}[1]{\hmode@bgroup\ooalign{%
%    \hfil \raise -.08ex\hbox{{%
%    \normalfont\addfontfeatures{Numbers={Monospaced, Lining}}%
%    \scalefont{.8}{\sustyle#1}}}% end hbox
%    \hfil \crcr \symbol{"25EF}}%
%    \egroup}
\else % type1
\setstate2

    \renewcommand*{\textcircled}[1]{\hmode@bgroup\ooalign{%
    \hfil \raise .3ex\hbox{{%
    \normalfont
    {\scalefont{.6}\tlfstyle#1}}}% end hbox
    \hfil \crcr {\usefont{TS1}{ntxtlf}{m}{n}\char"4F}}%\textbigcircle}%
    \egroup}
\fi

% applies to both
\setstate0
\DeclareTextFontCommand{\textosf}{\osfstyle}
\DeclareTextFontCommand{\texttosf}{\tosfstyle}
\DeclareTextFontCommand{\texttlf}{\tlfstyle}
\DeclareTextFontCommand{\textlf}{\lfstyle}
%\show\textlf
\DeclareTextFontCommand{\textsu}{\sustyle}
%    \let\textsuperior\textsu
\NewCommandCopy { \textsup } { \textsu }
\NewCommandCopy { \textsups } { \textsu }
%    \let\textsup\textsu
%   \let\textsups\textsu
\DeclareTextFontCommand{\textnum}{\nustyle}
 \NewCommandCopy { \textnu } { \textnum }
 \NewCommandCopy { \textnumerator } { \textnum }
%   \let\textnu\textnum
%    \let\textnumerator\textnum
\DeclareTextFontCommand{\textde}{\destyle}
 \NewCommandCopy { \textdenominator } { \textde }
 %   \let\textdenominator\textde
\DeclareTextFontCommand{\textinf}{\infstyle}
 \NewCommandCopy { \textinferior } { \textinf }
 \NewCommandCopy { \textsub } { \textinf }
%\let\textinferior\textinf
%    \let\textsub\textinf
  %  \let\infigures\instyle
\AtEndPreamble{%
\ifntx@otftext
\setstate1
  \ifntx@lining
    \def\ntx@figurestyle{Lining}
  \else
    \def\ntx@figurestyle{OldStyle}
  \fi
  \ifntx@tabular
    \def\ntx@figurealign{Monospaced} 
  \else
    \def\ntx@figurealign{Proportional} 
  \fi
  %\ifntx@scosf
     \setmainfont
          [ Numbers = {\ntx@figurealign,\ntx@figurestyle},
           ] {\ntx@base}
% \else
%     \setmainfont
%          [ Numbers = {\ntx@figurealign,\ntx@figurestyle},
%          ] {\ntx@base}
% \fi
  \let\ntx@sty\ntx@figurestyle
  \if l\ntx@thmlin\def\ntx@sty{Lining}\fi
  \newcommand{\thfamily}%{\@nameuse{ntx\ntx@figurestyle}%
    {\@nameuse{\if i\ntx@thmshape itshape\else slshape\fi}\addfontfeatures{%
    Numbers={\ntx@figurealign,\ntx@sty} ,
    RawFeature=+ss05}
    } % for theorems
    \ifntx@sl\else\let\slshape=\thfamily\fi   % need xpatch? 
\else % type1
\setstate2

  \ifntx@lining
    \def\ntx@figurestyle{lf} 
  \else
    \def\ntx@figurestyle{osf} 
  \fi
  \ifntx@tabular
    \def\ntx@figurealign{t} 
  \else
    \def\ntx@figurealign{} 
  \fi
  \renewcommand{\rmdefault}{ntx\ntx@figurealign%
  \ntx@figurestyle}
  \ifthenelse{\equal{\familydefault}{\sfdefault}\OR \equal{\familydefault}{\ttdefault}}%
  {}{\renewcommand{\familydefault}{\rmdefault}}

  \def\ntx@thfigurestyle{\ntx@figurestyle}
  \ifx\ntx@figurestyle\ntx@dfltfigurestyle
    \def\ntx@thfigurestyle{tlf}
  \else
    \def\ntx@thfigurestyle{osf}
  \fi % there just two possible figure styles for the theorem font
  \newcommand*{\thdefault}{ntxth-\ntx@thfigurestyle}
  \normalfont
\fi
}
%both
\setstate0

\@ifundefined{tx@xht}{\def\tx@xht{450}}{} % lowercase letters
\@ifundefined{tx@deht}{\def\tx@deht{406}}{} % denominator figures ht
\@ifundefined{tx@nubase}{\def\tx@nubase{270}}{} % baseline of numerators
\@ifundefined{tx@debase}{\def\tx@debase{0}}{} % baseline of denoms
\@ifundefined{tx@sfrVC}{\def\tx@sfrVC{331}}{}% vertical center of sfrac
\@ifundefined{tx@sfrVS}{\def\tx@sfrVS{106}}{} % space above/below bar
\@ifundefined{tx@sfrTH}{\def\tx@sfrTH{54}}{} % bar thickness
\@ifundefined{tx@sfrSC}{\def\tx@sfrSC{.9}}{} % scale 

\@ifundefined{textsfrac}{
\newdimen\tx@sixem\tx@sixem6pt % for checking if a number refers to ex
\newdimen\tx@y \newdimen\tx@x 

% convert numerical entry that may be multiple of ex (<6) or em value
\def\tx@toex#1{\tx@x#1pt \ifdim\tx@x<\z@\tx@y-\tx@x\else\tx@y\tx@x\fi \ifdim\tx@y>\tx@sixem \tx@x\dimexpr 1pt * (\dimexpr #1pt\relax) / \dimexpr \tx@xht pt\relax\relax\fi}

\newdimen\tx@tmpdima \newdimen\tx@tmpdimb  \newdimen\tx@tmpdimc
\newdimen\tx@tmpdimd \newdimen\tx@tmpdime \newdimen\tx@tmpdimf
\newdimen\tx@dimA
\newdimen\tx@dimAA \newdimen\tx@dimB \newdimen\tx@dimBB 
\newdimen\tx@dimC % vertical center
% double cap letters for below bar, single cap for above
\tx@toex\tx@sfrVC\tx@dimC\tx@x %Vertical center of fraction
\tx@toex\tx@sfrTH\tx@tmpdimb\tx@x % bar thickness
\tx@tmpdime.5\tx@tmpdimb % half bar thickness
\tx@toex\tx@sfrVS\tx@tmpdimc\tx@x% vertical space
\tx@toex\tx@deht\tx@tmpdimd\tx@x % height of unscaled figures
\tx@tmpdimd\tx@sfrSC\tx@tmpdimd % height of scaled figures
\tx@toex\tx@debase\tx@tmpdimf\tx@x % unscaled baseline of de figures
\tx@toex\tx@nubase\tx@tmpdima\tx@x % unscaled baseline of nu figures
\tx@tmpdima\tx@sfrSC\tx@tmpdima % scaled nu baseline
\tx@tmpdimf\tx@sfrSC\tx@tmpdimf % scaled de baseline
\tx@dimA\tx@dimC
\advance\tx@dimA by \tx@tmpdime % top of bar
\tx@dimAA\tx@dimC
\advance\tx@dimAA by -\tx@tmpdime % bottom of bar
\tx@dimB\tx@dimA
\advance\tx@dimB by \tx@tmpdimc % top figure baseline
\advance\tx@dimB by -\tx@tmpdima
\tx@dimBB\tx@dimAA
\advance\tx@dimBB by -\tx@tmpdimc % 
\advance\tx@dimBB by -\tx@tmpdimd % bottom figure baseline
\advance\tx@dimBB by -\tx@tmpdimf
\newcommand*{\textsfrac}[3][]{% like \textsfrac[1]{23}{34}
\mbox{%
    \ifthenelse{\not\equal{#1}{}}%Test for integer portion [optional #1]
      {\textlf{#1}\kern.1em}{}%         
	{\hmode@bgroup%
\ooalign{%
	\setbox0=\hbox{\scalefont{\tx@sfrSC}\nustyle#2}%
	\hfil \raise\strip@pt\tx@dimB ex\box0\hfil \crcr%
	\leaders\hrule height \strip@pt\tx@dimA ex depth -\strip@pt\tx@dimAA ex\hfill \crcr%
	\setbox1=\hbox{\scalefont{\tx@sfrSC}\destyle#3}%
	\hfil\raise\strip@pt\tx@dimBB ex\box1\hfil}%
\egroup}}}
}{} % end of \@ifundefined{textsfrac}{

\ifntx@thrm
	\@ifundefined{theoremstyle}{}{%
    \theoremstyle{plain}
    \patchcmd{\th@plain}{\itshape}{\thfamily}{\message{Replaced itshape by thfamily in theorem style plain.}}{\message{Unable to replace itshape by thfamily in theorem style plain.}}
    }
\fi
%\DeclareRobustCommand{\textth}[1]{%
%    {\thfamily #1}%
%}
\DeclareTextFontCommand{\textth}{\thfamily}
\let\textthit\textth

% Define \oldstylenums, \liningnums ,\tabularnums and \proportionalnums
\ifntx@otftext
\else % type1
\setstate2

    \DeclareTextFontCommand{%
      \oldstylenums}{\fontfamily{ntx\ntx@figurealign osf}\selectfont }
    \DeclareTextFontCommand{%
      \liningnums}{\fontfamily{ntx\ntx@figurealign lf}\selectfont }
    \DeclareTextFontCommand{%
      \tabularnums}{\fontfamily{ntxt\ntx@figurestyle}\selectfont }
    \DeclareTextFontCommand{%
      \proportionalnums}{\fontfamily{ntx\ntx@figurestyle}\selectfont }
	\re@DeclareTextSymbol{\textdollar}{OT1}{36}
\fi

\endinput

