summaryrefslogtreecommitdiff
path: root/macros/xetex/latex/exam-zh/tex/exam-zh-math.sty
blob: e11281f07719562833056d36d512aac3ae817f79 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
%
% Copyright (c) 2022 Kangwei Xia
% Released under the LaTeX Project Public License v1.3c License.
% Repository: https://gitee.com/xkwxdyy/exam-zh
%

\NeedsTeXFormat{LaTeX2e}

\RequirePackage{expl3}

\ProvidesExplPackage {exam-zh-math} {2024-02-11} {v0.2.1}
  {exam-zh math module}

\RequirePackage { tabularray }
\RequirePackage { varwidth }
\RequirePackage { graphicx }
\RequirePackage { filehook }
\file_if_exist:nT { wrapstuff.sty }
  {
    \RequirePackage { wrapstuff }
    \AtEndOfPackageFile* { exam-zh-choices }
      {
        \AddToHook { env / choices / before }
          { \wrapstuffclear }
      }
  }


\keys_define:nn { exam-zh }
  { calculations .meta:nn = { exam-zh / calculations } {#1} }

%% calculations 环境 %%


% 图片相对于 label 的方位
\str_new:N \l__examzh_calculations_figure_position_str
% coffin type 的对齐方式
\str_new:N \l__examzh_calculations_coffin_align_str


\keys_define:nn { exam-zh / calculations }
  {
    % calculations 环境的 label 从几开始(但是其实是 index +  seq 函数的 ##1 才是最终的 index)
    index .int_set:N = \l__examzh_calculations_label_index_shift_int,
    % 每行多少个(等价于有多少列)
    columns .int_set:N = \l__examzh_calculations_column_int,
    % 图片相对于文字的位置(上下左右)
    fig-pos .choices:nn =
      { top, above, bottom, below, left, right, left-top }
      { \str_set:Nx \l__examzh_calculations_figure_position_str { \l_keys_choice_tl } },
    % 环境上方的额外距离
    top-sep .skip_set:N = \l__examzh_calculations_top_sep_skip,
    % 环境下方的额外距离
    bottom-sep .skip_set:N = \l__examzh_calculations_bottom_sep_skip,
    % 两列之间的水平间距
    hsep .skip_set:N = \l__examzh_calculations_hsep_skip,
    % 两行之间的垂直间距
    vsep .skip_set:N = \l__examzh_calculations_vsep_skip,
    % 整体的偏移量
    xshift .dim_set:N = \l__examzh_calculations_xshift_dim,
    hshift .dim_set:N = \l__examzh_calculations_xshift_dim,
    yshift .dim_set:N = \l__examzh_calculations_yshift_dim,
    vshift .dim_set:N = \l__examzh_calculations_yshift_dim,
    % label 的偏移量
    label-xshift .dim_set:N = \l__examzh_calculations_label_xshift_dim,
    label-hshift .dim_set:N = \l__examzh_calculations_label_xshift_dim,
    label-yshift .dim_set:N = \l__examzh_calculations_label_yshift_dim,
    label-vshift .dim_set:N = \l__examzh_calculations_label_yshift_dim,
    % 对齐方式
    align .choices:nn = 
      { t, m, b }
      { \str_set:Nx \l__examzh_calculations_coffin_align_str { \l_keys_choice_tl } }
  }
\keys_set:nn { exam-zh / calculations }
  {
    index        = 0,
    columns      = 2,
    fig-pos      = left-top,
    xshift       = 2em,
    hsep         = 2em,
    vsep         = 0pt,
    label-xshift = 0pt,
    align        = t,
    top-sep      = 1ex plus .5ex minus .5ex,
    bottom-sep   = 0pt,
  }

% item 的 index 指标(即第几个 item)
\int_new:N \l__examzh_calculations_item_index_int
% 将拼接后的 coffin 存到 seq 里
\seq_new:N \l__examzh_calculations_store_seq
% 上面的 seq 的 item 数
\int_new:N \l__examzh_calculations_store_seq_item_int
% 最终放在 tblr 里的内容
\tl_new:N \l__examzh_calculations_tblr_content_tl



% #1: 设置 calculations
% #2: 设置里面的 tblr
\NewDocumentEnvironment { calculations } { O{ } +O{ } }
  {
    \group_begin:
      \RenewDocumentCommand \item { O{ } }
        { \__examzh_calculations_item:n {##1} }
      \int_set:Nn \l__examzh_calculations_item_index_int {0}
      \seq_clear:N \l__examzh_calculations_store_seq
      \tl_clear:N \l__examzh_calculations_tblr_content_tl
      \keys_set:nn { exam-zh / calculations } {#1}
  }
  {
      % 结束收集
        \unskip
      \end{varwidth}
      % \end{minipage}
      \hcoffin_set_end:
      % 拼接 label 和 figure
      \__examzh_calculations_coffin_join:
      % 输出
      \__examzh_calculations_coffin_typeset:n {#2}
      % \par \int_use:N \l__examzh_calculations_item_index_int
    \group_end:
  }
% 拼接 label 和 figure
\cs_new:Npn \__examzh_calculations_coffin_join:
  {
    \int_step_inline:nn { \l__examzh_calculations_item_index_int }
      {
        \__examzh_calculations_coffin_join_position_set:n {##1}
        \seq_gput_right:Nn \l__examzh_calculations_store_seq
          {
            \__examzh_calculations_coffin_align_set:n {##1}
            \kern\l__examzh_calculations_hsep_skip
          }
      }
  }
\cs_new:Npn \__examzh_calculations_coffin_align_set:n #1
  {
    \str_case:Vn \l__examzh_calculations_coffin_align_str
      {
        {t} { \__examzh_calculations_coffin_align_set_t:n {#1} }
        {m} { \__examzh_calculations_coffin_align_set_m:n {#1} }
        {b} { \__examzh_calculations_coffin_align_set_b:n {#1} }
      }
  }
\cs_new:Npn \__examzh_calculations_coffin_align_set_t:n #1
  {
    \coffin_typeset:cnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n { #1 } _coffin }
      { l } { t }  % align = t
      { \l__examzh_calculations_xshift_dim } { \l__examzh_calculations_yshift_dim + 1em }
  }
\cs_new:Npn \__examzh_calculations_coffin_align_set_m:n #1
  {
    \coffin_typeset:cnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n { #1 } _coffin }
      { l } { vc }  % align = m
      { \l__examzh_calculations_xshift_dim } { \l__examzh_calculations_yshift_dim }
  }
\cs_new:Npn \__examzh_calculations_coffin_align_set_b:n #1
  {
    \coffin_typeset:cnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n { #1 } _coffin }
      { l } { b }   % align = b
      { \l__examzh_calculations_xshift_dim } { \l__examzh_calculations_yshift_dim }
  }
\cs_new:Npn \__examzh_calculations_coffin_join_position_set:n #1
  {
    \use:c { __examzh_calculations_coffin_join_position_set_ \l__examzh_calculations_figure_position_str :n } {#1}
  }
\cs_new:cpn { __examzh_calculations_coffin_join_position_set_left-top :n } #1
  {
    \coffin_join:cnncnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n {#1} _ coffin }
      { l } { t }
      { l__examzh_calculations_label_ \int_to_roman:n {#1} _ coffin }
      { r } { b }
      { \l__examzh_calculations_label_xshift_dim }
      { \l__examzh_calculations_label_yshift_dim -10.6pt}
  }
\cs_new:Npn \__examzh_calculations_coffin_join_position_set_top:n #1
  {
    \coffin_join:cnncnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n {#1} _ coffin }
      { hc } { b }
      { l__examzh_calculations_label_ \int_to_roman:n {#1} _ coffin }
      { hc } { t }
      { \l__examzh_calculations_label_xshift_dim }
      { \l__examzh_calculations_label_yshift_dim - 6pt }
  }
\cs_set_eq:NN 
  \__examzh_calculations_coffin_join_position_set_above:n 
  \__examzh_calculations_coffin_join_position_set_top:n
\cs_new:Npn \__examzh_calculations_coffin_join_position_set_bottom:n #1
  {
    \coffin_join:cnncnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n {#1} _ coffin }
      { hc } { t }
      { l__examzh_calculations_label_ \int_to_roman:n {#1} _ coffin }
      { hc } { b }
      { \l__examzh_calculations_label_xshift_dim }
      { \l__examzh_calculations_label_yshift_dim + 6pt }
  }
\cs_set_eq:NN 
  \__examzh_calculations_coffin_join_position_set_below:n 
  \__examzh_calculations_coffin_join_position_set_bottom:n
\cs_new:Npn \__examzh_calculations_coffin_join_position_set_left:n #1
  {
    \coffin_join:cnncnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n {#1} _ coffin }
      { l } { vc }
      { l__examzh_calculations_label_ \int_to_roman:n {#1} _ coffin }
      { r } { vc }
      { \l__examzh_calculations_label_xshift_dim - 6pt }
      { \l__examzh_calculations_label_yshift_dim }
  }
\cs_new:Npn \__examzh_calculations_coffin_join_position_set_right:n #1
  {
    \coffin_join:cnncnnnn
      { l__examzh_calculations_figure_ \int_to_roman:n {#1} _ coffin }
      { r } { vc }
      { l__examzh_calculations_label_ \int_to_roman:n {#1} _ coffin }
      { l } { vc }
      { \l__examzh_calculations_label_xshift_dim + 2pt }
      { \l__examzh_calculations_label_yshift_dim }
  }
\cs_new:Npn \__examzh_calculations_coffin_typeset:n #1
  {
    \__examzh_calculations_coffin_typeset_count:
    \seq_map_indexed_inline:Nn \l__examzh_calculations_store_seq
      % ##1: index
      % ##2: content
      {
        \int_compare:nNnTF { \int_mod:nn {##1} { \l__examzh_calculations_column_int } } = {0}
          {
            \tl_gput_right:Nn \l__examzh_calculations_tblr_content_tl 
              { ##2 \\[\l__examzh_calculations_vsep_skip] }
          }
          {
            \tl_gput_right:Nn \l__examzh_calculations_tblr_content_tl 
              { ##2 & }
          }
      }
    % 如果 seq 的 item 比 column 多且不整除 column 的话,要补 &
    \int_compare:nNnT { \l__examzh_calculations_store_seq_item_int } > { \l__examzh_calculations_column_int }
      {
        \int_compare:nNnF { \l__examzh_calculations_item_num_mod_column_left_int } = { 0 }
          {
            \tl_gput_right:Nx \l__examzh_calculations_tblr_content_tl
              {
                \prg_replicate:nn { \l__examzh_calculations_item_num_mod_column_left_int -1 } {&}
              }
            % \int_use:N \l__examzh_calculations_item_num_mod_column_left_int
          }
      }
    \par 
    \vspace*{ \l__examzh_calculations_top_sep_skip }
    \noindent 
    % \centering
    % \SetTblrInner
    %   {
    %     rowsep = 4pt,
    %     % colsep = 0pt
    %   }
    \begin{tblr}
      [ expand = \l__examzh_calculations_tblr_content_tl ]
      {
        width = \linewidth,
        columns = 
          {
            l,
            leftsep = 0em,
            rightsep = 0pt,
          },
        rows = 
          {
            m,
            ht=\baselineskip,
            abovesep = 0pt,
            belowsep = 0pt,
          },
        stretch=0,
        #1
      }
      \l__examzh_calculations_tblr_content_tl
    \end{tblr}
    \vspace*{ \l__examzh_calculations_bottom_sep_skip }
    \par
  }
\int_new:N \l__examzh_calculations_item_num_mod_column_left_int
\cs_new:Npn \__examzh_calculations_coffin_typeset_count:
  {
    % 计算 seq 有多少项
    \int_set:Nn \l__examzh_calculations_store_seq_item_int
      { \seq_count:N \l__examzh_calculations_store_seq }
    % seq 项数小于 column 的话,column 设置为 seq 项数
    \int_compare:nNnTF { \l__examzh_calculations_store_seq_item_int } < { \l__examzh_calculations_column_int }
      { \int_set_eq:NN \l__examzh_calculations_column_int \l__examzh_calculations_store_seq_item_int }
      {
        % 计算 \l__examzh_calculations_store_seq_item_int mod \l__examzh_calculations_column_int 的余数,用于补 &
        \int_set:Nn \l__examzh_calculations_item_num_mod_column_left_int { \int_mod:nn { \l__examzh_calculations_store_seq_item_int } { \l__examzh_calculations_column_int } }
      }
  }

\cs_new:Npn \__examzh_calculations_item:n #1
  {
    % 增加指标(g 是关键)
    \int_gincr:N \l__examzh_calculations_item_index_int
    % 新建 coffin
    \__examzh_calculations_item_new_coffin:
    % 储存 label(一直出不来的原因是因为没改成 gset)
    \hcoffin_gset:cn 
      { l__examzh_calculations_label_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin } 
      {
        % 加上 index 键值设置的偏移量才是最终的 index
        % (\int_eval:n { \l__examzh_calculations_item_index_int + \l__examzh_calculations_label_index_shift_int - 1} )
        % 如果 \item 的[] 中没内容,则需要把 label 升高
        % \tl_if_blank:nTF {#1} 
        %   {
        %     \box_move_up:nn  { 2.7pt } { \hbox{\__examzh_calculations_the_label:} }
        %     \space
        %     #1
        %   } 
        %   {
        %     \__examzh_calculations_the_label: #1
        %   }
        % \IfBooleanTF{#1}{#1 yes}{#1 no}
        % \IfNoValueTF{#1}
        %   {
        %     \dim_set:Nn \l__examzh_calculations_label_yshift_dim { 4pt }
        %   }{#1 yes}
      }
    % 储存 figure
    \int_compare:nNnF { \l__examzh_calculations_item_index_int } = {1}
      {
          \unskip
          \end{varwidth}
          % \end{minipage}
        % 结束上一个 item 的收集
        \hcoffin_set_end:
      }
    % 收集
    \hcoffin_set:cw { l__examzh_calculations_figure_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin }
      % \begin{varwidth}{\hsize}
    % 根据 columns 个数设置宽度
    \__examzh_calculations_item_varwidth_width_set:
      \begin{varwidth}{\l__examzh_calculations_item_varwidth_width_dim}
      % \begin{minipage}{8cm}
        \ignorespaces
        \makebox[0em]{\rule{0pt}{\baselineskip}}
        \__examzh_calculations_the_label:
  }
\dim_new:N \l__examzh_calculations_item_varwidth_width_dim
\cs_new:Npn \__examzh_calculations_item_varwidth_width_set:
  {
    \int_case:nnF { \l__examzh_calculations_column_int }
      {
        {1} { \dim_set:Nn \l__examzh_calculations_item_varwidth_width_dim {0.8 \textwidth} }
        {2} { \dim_set:Nn \l__examzh_calculations_item_varwidth_width_dim {0.3 \textwidth} }
        {3} { \dim_set:Nn \l__examzh_calculations_item_varwidth_width_dim {0.3 \textwidth} }
        {4} { \dim_set:Nn \l__examzh_calculations_item_varwidth_width_dim {0.2 \textwidth} }
      }
      { \dim_set:Nn \l__examzh_calculations_item_varwidth_width_dim { \hsize } }
  }

% 新建 coffin
\cs_new:Npn \__examzh_calculations_item_new_coffin:
  {
    % 放图片的 coffin
    \coffin_if_exist:cF { l__examzh_calculations_figure_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin }
      { \coffin_new:c { l__examzh_calculations_figure_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin } }
    % 放 label 的 coffin
    \coffin_if_exist:cF { l__examzh_calculations_label_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin }
      { \coffin_new:c { l__examzh_calculations_label_ \int_to_roman:n { \l__examzh_calculations_item_index_int } _coffin } }
  }

\tl_new:N \l__examzh_calculations_counters_commands_set_tl

\keys_define:nn { exam-zh / calculations }
  {
    label .tl_set:N = \l__examzh_calculations_label_tl
  }
\keys_set:nn { exam-zh / calculations }
  {
    label = \arabic*
  }
  
\cs_new:Npn \__examzh_calculations_the_label:
  {
    \group_begin:
      % 定义计数器相关的命令函数
      \l__examzh_calculations_counters_commands_set_tl
      % 输出处理后的 label
      \l__examzh_calculations_label_tl
    \group_end:
  }

\NewDocumentCommand \AddCalculationsCounter { m m }
  {
    % 生成用户层命令
    \tl_put_right:Nn \l__examzh_calculations_counters_commands_set_tl
      { \__examzh_process_counter:NNn #1 #2 { calculations } }
    % 把核心函数存起来
    \cs_set_eq:cN { __examzh_calculations_save_ \cs_to_str:N #1 : } #2
    \cs_set_eq:cN { __examzh_calculations_save_ \cs_to_str:N #2 : } #2
  }

\AddCalculationsCounter \arabic \@arabic
\AddCalculationsCounter \alph   \@alph
\AddCalculationsCounter \Alph   \@Alph
\AddCalculationsCounter \roman  \@roman
\AddCalculationsCounter \Roman  \@Roman

\cs_new:Npn \__examzh_calculations_process_counter_aux:Nn #1#2
  {
    \tl_if_eq:nnTF {#2} { * }
      {
        % \Alph*
        \use:c { __examzh_calculations_save_ \cs_to_str:N #1 : }
          { \int_eval:n { \l__examzh_calculations_item_index_int + \l__examzh_calculations_label_index_shift_int } }
      }
      {
        % \Alph{...}
        \use:c { __examzh_calculations_save_ \cs_to_str:N #1 : }
          {#2}
      }
  }