summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/smart-eqn/smart-eqn.sty
blob: d93752ec4981f5f4c1bf4569c49c01b923a24779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%%
%% 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'.