%%
%% This is file `smart-eqn.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% smart-eqn.dtx  (with options: `package')
%% 
%% This is a generated file. This file is part of the smart-eqn package.
%% 
%% Copyright (C) 2021 by Ziyue "Alan" Xiang <ziyue.alan.xiang@gmail.com>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% 28 or (at your option) any later version. The latest version of this
%% 29 license is in:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{smart-eqn}{08/05/2021}{1.0}{Automatic Math Symbol Styling}































\RequirePackage{fancyvrb}
\RequirePackage{expl3, xparse}

\makeatletter
\ExplSyntaxOn

\tl_new:N \g_sme_op_arg_tl
\ior_new:N \g_sme_tmpa_ior
\iow_new:N \g_sme_tmpa_iow
\prop_new:N \g_sme_symbols_prop
\clist_new:N \l_sme_tmpa_clist
\tl_new:N \l_sme_tmpa_tl
\tl_new:N \l_sme_tmpb_tl
\tl_new:N \l_sme_tmpc_tl
\tl_new:N \l_sme_cur_math_tl

\def\smeDefineVerbatimEnvironment#1#2#3{%
  \@namedef{#1}{\smeFV@Environment{#3}{#2}}%
  \@namedef{end#1}{\@nameuse{FVE@#2}}%
}

\def\smeFV@Environment#1#2{%
  \def\FV@KeyValues{#1}%
  \catcode`\^^M=\active
  \tl_gclear:N \g_sme_op_arg_tl % clear optional arguments from previous calls
  \@ifnextchar[%
    {\catcode`\^^M=5 \smeFV@GetKeyValues{\@nameuse{FVB@#2}}}%
    {\catcode`\^^M=5 \@nameuse{FVB@#2}}}

\def\smeFV@GetKeyValues#1[#2]{%
  \tl_gset:Nn \g_sme_op_arg_tl {#2}#1}

\cs_set:Npn \sme_declare_math_env:n #1 {
    \exp_args:Nc \def{sme#1}{\smeFV@Environment{}{sme#1}}

    % this is a modified version of VerbatimOut environment which does not take any argument
    \exp_args:Nc \def{FVB@sme#1}{%
      \@bsphack
      \begingroup
        \FV@UseKeyValues
        \FV@DefineWhiteSpace
        \def\FV@Space{\space}%
        \FV@DefineTabOut
        \def\FV@ProcessLine{\immediate\write\FV@OutFile}%
        \immediate\openout\FV@OutFile \jobname-sme.vrb\relax
        \let\FV@FontScanPrep\relax
    %% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
        \let\@noligs\relax
    %% DG/SR modification end
        \FV@Scan}

    \exp_args:Nc\def{FVE@sme#1}{\immediate\closeout\FV@OutFile\endgroup\@esphack
        % call the function to process the content when the environment ends
        \sme_read_process_math:n {#1}
    }

    \smeDefineVerbatimEnvironment{sme#1}{sme#1}{}
}

\let\smenewenv\sme_declare_math_env:n

\cs_set:Npn \sme_read_process_math:n #1 {
    \ior_open:Nn \g_sme_tmpa_ior {\jobname-sme.vrb}
    \iow_open:Nn \g_sme_tmpa_iow {\jobname-sme-in.vrb}

    \iow_now:Nx \g_sme_tmpa_iow {\c_backslash_str begin{#1} \g_sme_op_arg_tl}
    \ior_map_inline:Nn \g_sme_tmpa_ior {
        \sme_process_math:n {##1}
        \exp_args:NNV \iow_now:Nn \g_sme_tmpa_iow \l_sme_cur_math_tl
    }
    \iow_now:Nx \g_sme_tmpa_iow {\c_backslash_str end{#1}}
    \iow_close:N \g_sme_tmpa_iow
    \ior_close:N \g_sme_tmpa_ior
    \input{\jobname-sme-in.vrb}
}

\newcommand{\smesetsym}[2]{
    \clist_set:Nn \l_sme_tmpa_clist {#2}
    \clist_map_inline:Nn \l_sme_tmpa_clist {
        \prop_gput:Nnn \g_sme_symbols_prop {##1} {#1}
    }
}

\newcommand{\smeclearsym}{
    \prop_gclear:N \g_sme_symbols_prop
}

\newcommand{\smeraw}[1]{#1}

\cs_set:Npn \__sme_grouped:n #1 {
    \exp_not:n {#1}
}
\cs_generate_variant:Nn \__sme_grouped:n {e}
\cs_generate_variant:Nn \__sme_grouped:n {V}

\cs_set:Npn \sme_construct_bm:Nn #1#2 {
    \tl_if_head_is_group:nTF {#2} {
        % if head is group, apply the algorithm recursively
        %\tl_put_right:Nx #1 {{\tl_head:n {#2}}}
        \tl_put_right:NV #1 \c_left_brace_str
        \exp_args:NNx \sme_construct_bm:Nn #1 {\tl_head:n {#2}}
        \tl_put_right:NV #1 \c_right_brace_str
        \tl_if_empty:nF {#2} {\exp_args:NNx \sme_construct_bm:Nn #1 {\tl_tail:n {#2}}}
    }{
        \tl_if_head_is_space:nTF {#2} {
            % ignore spaces
            \exp_args:NNx \sme_construct_bm:Nn #1 {\tl_trim_spaces:n {#2}}
        } {
            \tl_if_head_eq_meaning:nNTF {#2} \smeraw {
                % for \smeraw, ignore the next group
                \tl_put_right:Nn #1 {\smeraw}
                \tl_set:Nx \l_sme_tmpa_tl {\tl_tail:n {#2}}
                \tl_put_right:Nx #1 {\tl_head:N \l_sme_tmpa_tl}
                \tl_set:Nx \l_sme_tmpa_tl {\tl_tail:N \l_sme_tmpa_tl}
                \exp_args:NNV \sme_construct_bm:Nn #1 \l_sme_tmpa_tl
            } {
                % common case
                % extract head
                \exp_args:NNx \prop_get:NnNTF \g_sme_symbols_prop {\tl_head:n {#2}} \l_sme_tmpc_tl {
                    % this symbol needs to be styled
                    \tl_set:No \l_sme_tmpa_tl {\csname\l_sme_tmpc_tl\endcsname}
                    \tl_put_right:Nx \l_sme_tmpa_tl {{\__sme_grouped:e {\tl_head:n {#2}}}}
                    \tl_put_right:Nx #1 {{\__sme_grouped:V \l_sme_tmpa_tl}}
                } {
                    % otherwise, use the original symbol
                    \tl_put_right:Nx #1 {\tl_head:n {#2}}
                }
                \tl_if_empty:nF {#2} {\exp_args:NNx \sme_construct_bm:Nn #1 {\tl_tail:n {#2}}}
            }
        }
    }
}

\cs_set:Npn \sme_process_math:n #1 {
    \tl_clear:N \l_sme_cur_math_tl
    \sme_construct_bm:Nn \l_sme_cur_math_tl {#1}
}

\begingroup
\catcode`@=\active
\gdef\makeatmath{%    note the global \gdef
  \catcode`@=\active
  \def@##1@{
    \sme_process_math:n{##1}
    $\exp_args:NnV \tl_rescan:nn {} \l_sme_cur_math_tl$
  }%
}
\endgroup

\makeatother
\ExplSyntaxOff

\endinput
%%
%% End of file `smart-eqn.sty'.
