%% Written by Nova de Hi
%\ProvidesPackage{koredumath}[2022/09/22, v0.2]
% 2023/06/03, v0.4.1
\ProvidesPackage{koredumath}[2025/05/14, v0.5]

\RequirePackage{l3keys2e}
\RequirePackage{graphicx}
\RequirePackage{etoolbox}

\ExplSyntaxOn

\keys_define:nn { koredumath }
{
	varsupersub		.bool_set:N = \opt_varsupersub_bool,
	similarsignfont	.tl_set:N = \similar_sign_font,
	similarsignfig	.bool_set:N = \opt_similarsignfig_bool,
	eqnformat		.tl_set:N = \opt_eqnformat_tl,
	noamssymb		.bool_set:N = \opt_noamssymb_bool,
	unknown			.code:n = { \PassOptionsToPackage { \l_keys_key_str } { jpnedumathsymbols } }
}

\keys_set:nn { koredumath }
{
	similarsignfont = {NotoSerifKR-Regular.otf},
	eqnformat = none,
	varsupersub = false,
	similarsignfig = false,
	noamssymb = false,
}

\ProcessKeysOptions { koredumath }

\bool_if:NT \opt_noamssymb_bool
{
	\@ifpackageloaded{amssymb}{}{%
	%%%% force not to load amssymb.sty
		%\newcommand{\dontusepackage}[2][]{%
			%  \@namedef{ver@#2.sty}{9999/12/31}%
			%  \@namedef{opt@#2.sty}{#1}}
		\@namedef{ver@amssymb.sty}{9999/12/31}
		\@namedef{opt@amssymb.sty}{amssymb}
	}
}
\RequirePackage{jpnedumathsymbols}
\RequirePackage{iftex}

%%% \sqrt should be the original one:
\let\jpnsqrt\sqrt
\let\sqrt\originalsqrt

%%% SUPER and SUB
%%% https://tex.stackexchange.com/questions/436142/vertical-positioning-of-superscripts-and-subscripts-in-math-mode

\dim_new:N \l_varssstep_dim
\dim_set:Nn \l_varssstep_dim { 1ex }
\dim_new:N \l_varssstepdown_dim
\dim_set:Nn \l_varssstepdown_dim { 0.8ex }

\NewDocumentCommand \varsupersubstep { O{2} }
{
	\int_case:nn { #1 } 
	{
		{ 0 } { 
				\dim_set:Nn \l_varssstep_dim { .3ex } 
				\dim_set:Nn \l_varssstepdown_dim { 0ex }
			  }
		{ 1 } { 
				\dim_set:Nn \l_varssstep_dim { .5ex } 
				\dim_set:Nn \l_varssstepdown_dim { 0.2ex }
			  }
		{ 2 } {
				\dim_set:Nn \l_varssstep_dim { .9ex } 
				\dim_set:Nn \l_varssstepdown_dim { 0.45ex }
			  }
		{ 3 } { 
				\dim_set:Nn \l_varssstep_dim { 1.1ex } 
				\dim_set:Nn \l_varssstepdown_dim { 0.65ex }
			  }
		{ 4 } { 
				\dim_set:Nn \l_varssstep_dim { 1.3ex } 
				\dim_set:Nn \l_varssstepdown_dim { .8ex }
			  }
	}
}

\setbox0=\hbox{$
    \xdef\fdfourteen{\the\fontdimen14\textfont2}
    \xdef\fdfifteen{\the\fontdimen15\textfont2}
    \xdef\fdsixteen{\the\fontdimen16\textfont2}
    \xdef\fdseventeen{\the\fontdimen17\textfont2}
$}
\newcommand\varsupersub{
    \setbox0=\hbox{\(\)}
    \fontdimen14\textfont2=\l_varssstep_dim
    \fontdimen15\textfont2=\l_varssstep_dim
    \fontdimen16\textfont2=\l_varssstepdown_dim
    \fontdimen17\textfont2=\l_varssstepdown_dim
}
\newcommand\stdsupersub{\setbox0=\hbox{\(\)}%
    \fontdimen14\textfont2=\fdfourteen
    \fontdimen15\textfont2=\fdfifteen
    \fontdimen16\textfont2=\fdsixteen
    \fontdimen17\textfont2=\fdseventeen
}

\NewDocumentCommand \similarsymbol {}
{
	\ifx\ifXeTeX\ifLuaTeX
	\raisebox{0.2ex}{\includegraphics[width=\f@size pt]{similarsign.pdf}}
	\else
		\bool_if:NTF \opt_similarsignfig_bool
		{
			\raisebox{0.2ex}{\includegraphics[width=\f@size pt]{similarsign.pdf}}
		}
		{
			\fontspec{\similar_sign_font}\symbol{"223D}%∽
		}
	\fi
}

\let\similar\relax
\DeclareRobustCommand{\similar}[1][1.2]{
	\ensuremath{
		\mathrel{\lower.22ex\hbox{\scalebox{#1}{\similarsymbol}}}
	}
}

\NewDocumentCommand \eparen { s m }
{
	\IfBooleanTF { #1 }
	{
		\ensuremath { ( #2 ) }
	}
	{
 		\ensuremath {
			\left( #2 \right)
		}
	}
}

%%% pdv, ndv
\NewDocumentCommand \s_frac { m m }
{
	#1 / #2 
}

\NewDocumentCommand \pdv { s o m !g !g !d() }
{
	\IfBooleanTF { #1 }
	{
		\cs_set_eq:NN \_frac_pdv:nn \s_frac
	}
	{
		\cs_set_eq:NN \_frac_pdv:nn \frac
	}
	
	\IfValueTF { #5 }
	{
		\pdv_iii:nnn { #3 } { #4 } { #5 }
	}
	{
		\IfNoValueTF { #2 }
		{
			\IfValueTF { #4 }
			{
				\pdv_ii:nn { #3 } { #4 }
			}
			{
				\pdv_i:n { #3 }
			}
		}
		{
			\IfValueTF { #4 }
			{
				\pdv_nii:nnn { #2 } { #3 } { #4 }
			}
			{
				\pdv_ni:nn { #2 } { #3 }
			}
		}
	}
	
	\IfValueT { #6 }
	{
		\left( #6 \right)
	}
}

\cs_new:Npn \pdv_i:n #1
{
	\ensuremath {
		\_frac_pdv:nn { \partial } { \partial #1 }
	}
}

\cs_new:Npn \pdv_ni:nn #1 #2
{
	\ensuremath {
		\_frac_pdv:nn { \partial ^{#1} } { \partial #2 ^{#1} }
	}
}

\cs_new:Npn \pdv_ii:nn #1 #2
{
	\ensuremath {
		\_frac_pdv:nn { \partial #1 } { \partial #2 }
	}
}

\cs_new:Npn \pdv_nii:nnn #1 #2 #3
{
	\ensuremath {
		\_frac_pdv:nn { \partial ^ { #1 } #2 } { \partial #3 ^ { #1 } }
	}
}

\cs_new:Npn \pdv_iii:nnn #1 #2 #3
{
	\ensuremath {
		\_frac_pdv:nn { \partial ^2 { #1 } } { \partial #2 \partial #3 }
	}
}

\NewDocumentCommand \ndv { s o m !g !g !d() }
{
	\IfBooleanTF { #1 }
	{
		\cs_set_eq:NN \_frac_ndv:nn \s_frac
	}
	{
		\cs_set_eq:NN \_frac_ndv:nn \frac
	}
	
	\IfValueTF { #5 }
	{
		\ndv_iii:nnn { #3 } { #4 } { #5 }
	}
	{
		\IfNoValueTF { #2 }
		{
			\IfValueTF { #4 }
			{
				\ndv_ii:nn { #3 } { #4 }
			}
			{
				\ndv_i:n { #3 }
			}
		}
		{
			\IfValueTF { #4 }
			{
				\ndv_nii:nnn { #2 } { #3 } { #4 }
			}
			{
				\ndv_ni:nn { #2 } { #3 }
			}
		}
	}
	
	\IfValueT { #6 }
	{
		\left( #6 \right)
	}
}

\cs_new:Npn \ndv_i:n #1
{
	\ensuremath {
		\_frac_ndv:nn { d } { d #1 }
	}
}

\cs_new:Npn \ndv_ni:nn #1 #2
{
	\ensuremath {
		\_frac_ndv:nn { d ^{#1} } { d #2 ^{#1} }
	}
}

\cs_new:Npn \ndv_ii:nn #1 #2
{
	\ensuremath {
		\_frac_ndv:nn { d #1 } { d #2 }
	}
}

\cs_new:Npn \ndv_nii:nnn #1 #2 #3
{
	\ensuremath {
		\_frac_ndv:nn { d ^ { #1 } #2 } { d #3 ^ { #1 } }
	}
}

\cs_new:Npn \ndv_iii:nnn #1 #2 #3
{
	\ensuremath {
		\_frac_ndv:nn { d ^2 { #1 } } { d #2 d #3 }
	}
}

%%% dsum
\NewDocumentCommand \dsum { s t\limits e{_^} }
{
	\IfBooleanF { #2 } { \bool_set_true:N \l_tmpa_bool }
	\IfValueT { #3 } { \bool_set_true:N \l_tmpa_bool }
	\IfValueT { #4 } { \bool_set_true:N \l_tmpa_bool }
	\IfBooleanTF { #1 }
	{
		\displaystyle \sum 
	}
	{
		\sum \bool_if:NT \l_tmpa_bool { \limits }
	}
%	\IfBooleanT { #2 } { \limits }
	\IfValueT { #3 } { _{#3} }
	\IfValueT { #4 } { ^{#4} } 
}

\NewDocumentCommand \dprod { s t\limits e{_^} }
{
	\IfBooleanF { #2 } { \bool_set_true:N \l_tmpa_bool }
	\IfValueT { #3 } { \bool_set_true:N \l_tmpa_bool }
	\IfValueT { #4 } { \bool_set_true:N \l_tmpa_bool }
	\IfBooleanTF { #1 }
	{
		\displaystyle \prod
	}
	{
		\prod \bool_if:NT \l_tmpa_bool { \limits }
	}
%	\IfBooleanT { #2 } { \limits }
	\IfValueT { #3 } { _{#3} }
	\IfValueT { #4 } { ^{#4} } 
}

%%%%

\bool_if:NT \opt_varsupersub_bool
{
	\AtBeginDocument { 
		\varsupersubstep
	}
}

%%% inverse
\NewDocumentCommand \invrs {}
{
	\ensuremath { 
		^{-1}
	}
}

%%%
%%% version 0.3
%%% eqnformat: dotted or circled
%%% see http://www.ktug.org/xe/index.php?mid=KTUG_open_board&document_srl=260097
%%%
\bool_new:N \l_circeqn_bool
\bool_new:N \l_doteqn_bool
\str_case_e:nnF { \opt_eqnformat_tl } 
{
	{ circ }     { \bool_set_true:N \l_circeqn_bool  \bool_set_false:N \l_doteqn_bool }
	{ dots }     { \bool_set_false:N \l_circeqn_bool \bool_set_true:N \l_doteqn_bool  }
	{ dotscirc } { \bool_set_true:N \l_circeqn_bool  \bool_set_true:N \l_doteqn_bool  }
	{ circdots } { \bool_set_true:N \l_circeqn_bool  \bool_set_true:N \l_doteqn_bool  }
	{ none }     { \bool_set_false:N \l_circeqn_bool \bool_set_false:N \l_doteqn_bool }
}
{
	\bool_set_false:N \l_circeqn_bool
	\bool_set_false:N \l_doteqn_bool
}

\bool_if:NT \l_circeqn_bool
{
	\RequirePackage[tikzsmall]{ kocircnum }
}

\newdimen\koredu@eqndots

\bool_if:NTF \l_circeqn_bool
{
	\AtBeginDocument{
		\counterwithout{equation}{chapter}
		\counterwithout{equation}{section}
    	\cs_if_exist:NF \koredu_eqnmodifier
    	{
    		\def\koredu_eqnmodifier{\tikzcircnum[raise=0.1ex]}
    	}
	}
}
{
	\def\koredu_eqnmodifier#1{(#1)}
}

\bool_if:NTF \l_doteqn_bool
{
	\if@fleqn
		\RequirePackage{dotseqn}
		\renewcommand\EqnDots{\leaders\hbox{\kern1.5\p@ .\kern1.5\p@}\hfill}
		\def\tagform@#1{\maketag@@@{\ignorespaces\koredu_eqnmodifier{#1}\unskip\@@italiccorr}}
	\else
		\RequirePackage{linegoal}
		\def\tagform@#1{
			\maketag@@@{
				\llap{\hspace*{1em}\raise.5ex\hbox to \dimexpr\koredu@eqndots-2em\relax{\dotfill}}
				\ignorespaces\ \koredu_eqnmodifier{#1}\unskip\@@italiccorr
			}
		}
	\fi
}
{
	\bool_if:NTF \l_circeqn_bool
	{
		\def\tagform@#1{\maketag@@@{\ignorespaces\koredu_eqnmodifier{#1}\unskip\@@italiccorr}}
	}
	{
		\def\tagform@#1{\maketag@@@{\ensuremath{(}\ignorespaces{#1}\unskip\@@italiccorr}\ensuremath{)}}
	}
}

\NewDocumentCommand \keeqndots {}
{
	\dim_set:Nn \koredu@eqndots { \linegoal }
}

\AtEndPreamble{
	\cs_if_exist:NF \linegoal { \newdimen\linegoal }
}

\NewDocumentCommand \reseteqn {}
{
	\setcounter{equation}{0}
}

%%%% interval
\NewDocumentCommand \intvl { t[ t( m m t] t) }
{
	\IfBooleanT{#1} { \left[ \mkern2mu}
	\IfBooleanT{#2} { \left ( }
	#3\comma #4
	\IfBooleanT{#5} { \mkern2mu \right] }
	\IfBooleanT{#6} { \right) }
}

%%%% braket
\ProvideDocumentCommand \braket { t1 t2 t3 m }
{
	\IfBooleanT{#1} {\left( #4 \right) }
	\IfBooleanT{#2} {\left\{ #4 \right\} }
	\IfBooleanT{#3} {\left[ #4 \right] }
}

\NewDocumentCommand \setbrk { m }
{
	\braket2 { #1 }
}

%%% matices
\NewDocumentCommand\melem{mmm}{
    \int_step_inline:nn { #3 }
    {
        \ensuremath { #1 \c_math_subscript_token {#2 ##1} }
        \int_compare:nT { ##1 < #3 }
        { \c_alignment_token }
    }
}
\NewDocumentCommand\melemi{mmm}{
    \int_step_inline:nn {#2}
    {
        \ensuremath { #1 \c_math_subscript_token { ##1 #3 } }
        \int_compare:nT { ##1 < #2 }
        { \c_alignment_token }
    }
}
\cs_new:Npn \my_for:w i=#1 to #2 #3
{
    \int_step_inline:nnn { #1 } { #2 }
    { #3 }
}
\cs_set_eq:NN \myfor \my_for:w

\NewDocumentCommand \matI { mO{2} }
{
	\begin{#1matrix}
	\int_step_inline:nn { #2 }
	{
		\int_step_inline:nn { #2 }
		{
			\int_compare:nTF { ##1 == ####1 }
			{
				1 %(##1, ####1)
			}
			{
				0 %(##1, ####1)
			}
			\int_compare:nTF { ####1 == #2 }
			{
				\int_compare:nF { ##1 == #2 } { \\ }
			}
			{ \c_alignment_token }
		}
	}
	\end{#1matrix}
}

\NewDocumentCommand \thrthrmatrix { O{p} m }
{
	\begin{#1matrix}
	#2\c_math_subscript_token {11} & #2\c_math_subscript_token {12} & #2\c_math_subscript_token {13} \\
	#2\c_math_subscript_token {21} & #2\c_math_subscript_token {22} & #2\c_math_subscript_token {23} \\
	#2\c_math_subscript_token {31} & #2\c_math_subscript_token {32} & #2\c_math_subscript_token {33}
	\end{#1matrix}
}


%%%% \dint, redefined
\let\jpndint\dint
\cs_undefine:N \dint

\ExplSyntaxOff

\NewDocumentCommand{\dint}{e{_^}}{%
  \mathop{
    \displaystyle\int
    \IfValueT{#1}{_{#1}}
    \IfValueT{#2}{^{#2}}
  }%
}

%%% repeatedpermutation
\DeclareRobustCommand{\repeatedpermutation}[2]{\ensuremath{{}_{#1}{\Pi}_{#2}}}
%
%%%% ecases etc.
\DeclareDocumentEnvironment{ecases}{O{align*} m b}%
{\@ifpackageloaded{ob-mathleading}{}{\vspace{0.2\baselineskip}}\begin{empheq}[left={#2=\empheqlbrace\,}]{#1}{#3}\end{empheq}}%
{\par\vspace{0.2\baselineskip}\noindent}

%%%% \begin{simul}{<style>}
\DeclareDocumentEnvironment{simul}{O{gather*} b}%
{\@ifpackageloaded{ob-mathleading}{\begin{obMathLeading}[1.25]}{\vspace{0.25\baselineskip}}\begin{empheq}[left=\empheqbiglbrace\,]{#1}{#2}\end{empheq}%
\@ifpackageloaded{ob-mathleading}{\end{obMathLeading}}{}
}%
{\par\vspace{0.25\baselineskip}\noindent}

%%% from Kriss' intothemath
\newcommand{\xcirc}[1]{\vcenter{\hbox{$#1\circ$}}}%% for define \comp and \COMP
\newcommand{\ccirc}{\mathbin{\mathchoice{\xcirc\scriptstyle}{\xcirc\scriptstyle}{\xcirc\scriptscriptstyle}{\xcirc\scriptscriptstyle}}}

%%% \emptyset
\let\originalemptyset\emptyset
\let\emptyset\varnothing

%%% \inpbracket
\DeclareRobustCommand{\inpbracket}
{\@ifstar{\@jpnedu@symbol@inpbracket@star}{\@jpnedu@symbol@inpbracket@nostar}}
\DeclareRobustCommand{\@jpnedu@symbol@inpbracket@star}[2]{\langle #1\relax,#2\rangle}
\DeclareRobustCommand{\@jpnedu@symbol@inpbracket@nostar}[2]{\left\langle #1\relax,#2\right\rangle}

\endinput
