summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/keytheorems/tex/class-support/keythms-beamer-support.tex
blob: 8aef1cab3fd191fb14866ea194709dc399886d5e (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
% Maintained by Matthew Bertucci, 2024-present
% Please report all issues and feature requests at https://github.com/mbertucci47/keytheorems
% This work is licensed under the LPPL version 1.3c or later: https://www.latex-project.org/lppl.txt
\ProvidesExplFile{keythms-beamer-support}{\@keythms@date}{\@keythms@version}
  {keytheorems~support~for~the~beamer~class}

% disable \listofkeytheorems, add warning
\msg_new:nnn { keytheorems } { beamer-listof }
  {
    \protect\listofkeytheorems\space not~supported~with~beamer
  }
\RenewDocumentCommand \listofkeytheorems { +o }
  {
    \msg_warning:nn { keytheorems } { beamer-listof }
  }

% besides above, do nothing if noamsthm loaded
\IfClassLoadedWithOptionsT{ beamer }{ noamsthm }{ \file_input_stop: }

\tl_new:N \g__keythms_thmstyle_example_metakeys_tl
\tl_gset:Nn \g__keythms_thmstyle_example_metakeys_tl
  {
    bodyfont = \normalfont,
    headfont = \def\inserttheoremblockenv{exampleblock},
  }

\cs_set_protected:Npn \__keythms_thm_tcbcode:nn #1#2
  { % #1 = name, #2 = tcolorbox keys
    \__keythms_thm_tcbshared:nn { #1 } { #2 }
    \tl_gput_left:cn { g__keythms_thm_preheadfromkeys_#1_tl }
      {
        \cs_set:Npn \inserttheoremblockenv { keythms_beamer_tcb }
      }
  }
\cs_set_protected:Npn \__keythms_thm_tcbnotitlebarcode:nn #1#2
  { % #1 = name, #2 = tcolorbox keys
    \__keythms_thm_tcbshared:nn { #1 } { #2 }
    \tl_gput_left:cn { g__keythms_thm_preheadfromkeys_#1_tl }
      {
        \cs_set:Npn \inserttheoremblockenv { keythms_beamer_tcbnotitlebar }
      }
  }
\cs_set_protected:Npn \__keythms_thm_tcbshared:nn #1#2
  {
    \RequirePackage{tcolorbox}
    \AddToHook{env/#1/begin}
      { % fix labels adding extra space
        \keys_define:nn { keytheorems/thmuse }
          { label .code:n = \tcbset{ phantom = { \label{##1} } } }
      }
    \tcbset
      {
        keythms_tcb_#1/.style =
          {
            savedelimiter=#1,
            #2
          }
      }
      \tl_if_empty:NTF \l__keythms_thm_style_tl
        {
          \tl_set_eq:NN \l__keythms_thm_tempstyle_tl
            \l__keythms_thm_currentthmstyle_tl
        }
        { \tl_set_eq:NN \l__keythms_thm_tempstyle_tl \l__keythms_thm_style_tl }
      \tl_gput_right:ce { g__keythms_thm_preheadfromkeys_#1_tl }
        { % don't mess with tcolorbox defaults unless explicitly asked to in style
          \exp_not:N \tl_put_right:cn { th@ \l__keythms_thm_tempstyle_tl }
            { \thm@preskip=0pt \thm@postskip=0pt }
        }
      \tl_if_exist:cT
        { g__keythms_thmstyle_ \l__keythms_thm_tempstyle_tl _tcbspaceabove_skip }
        {
          \exp_args:Ne \tcbset
            {
              keythms_tcb_#1/.prefix~style =
                {
                  before~skip =
                    \skip_use:c { g__keythms_thmstyle_ \l__keythms_thm_tempstyle_tl _tcbspaceabove_skip }
                }
            }
        }
      \tl_if_exist:cT
        { g__keythms_thmstyle_ \l__keythms_thm_tempstyle_tl _tcbspacebelow_skip }
        {
          \exp_args:Ne \tcbset
            {
              keythms_tcb_#1/.prefix~style =
                {
                  after~skip =
                    \skip_use:c { g__keythms_thmstyle_ \l__keythms_thm_tempstyle_tl _tcbspacebelow_skip }
                }
            }
        }
  }
\NewDocumentEnvironment{ keythms_beamer_tcb }{ m d<> }
  {
    \tl_if_novalue:nF { #2 } { \begin{actionenv}<#2> }
    \begin{tcolorbox}[
      title={\usebeamerfont*{block title} #1},
      keythms_tcb_\l_keythms_thmuse_envname_tl
      ]
  }
  {
    \end{tcolorbox}
    \tl_if_novalue:nF { #2 } { \end{actionenv} }
  }
\NewDocumentEnvironment{ keythms_beamer_tcbnotitlebar }{ m d<> }
  {
    \tl_if_novalue:nF { #2 } { \begin{actionenv}<#2> }
    \begin{tcolorbox}[ keythms_tcb_\l_keythms_thmuse_envname_tl ]
      \group_begin:
      \usebeamerfont*{block title} #1
      \thmheadnl
      \hskip\thm@headsep
      \group_end:
  }
  {
    \end{tcolorbox}
    \tl_if_novalue:nF { #2 } { \end{actionenv} }
  }

\cs_set_protected:Npn \keythms_keyify_theorem:n #1
  { % #1 = theorem name
    \DeclareEnvironmentCopy { keythms_orig_#1 } { #1 }
    \DeclareDocumentEnvironment { keythms_beamer_grab_#1 } { m m m +b }
      { % ##1 = keys, ##2 = note, ##3 = action spec, ##4 = theorem body
        \__keythms_thm_prehead_code:n { #1 }
        \tl_if_empty:nTF { ##3 }
          { \begin{keythms_orig_#1}[{##2}] }
          { \begin{keythms_orig_#1}[{##2}]<##3> }
        \clist_map_inline:Nn \g__keythms_restatecounters_clist
          {
            \prop_gput:Nne \g__keythms_thmuse_othercounters_prop { ####1 }
              { \the\value{####1} }
          }
        \__keythms_thm_posthead_code:n { #1 }
        % below needs to come after posthead so that correct \@currentHref
        % is stored for tcolorbox theorems
        \__keythms_thm_addcontentsdata:nnnn { #1 }
          { \prop_to_keyval:N \g__keythms_thmuse_othercounters_prop }
          { ##1 } { ##4 }
        \__keythms_thm_tempstorerestatedata:nnn { #1 } { ##1 } { ##4 }
        ##4
        \__keythms_thm_prefoot_code:n { #1 }
        \end{keythms_orig_#1}
        \__keythms_thm_postfoot_code:n { #1 }
      }
      {}
    \DeclareDocumentEnvironment { keythms_beamer_grabreversed_#1 } { m m m +b }
      { % ##1 = keys, ##2 = note, ##3 = action spec, ##4 = theorem body
        \tl_if_exist:cTF
          { c__keythms_storeatbegin_ \l__keythms_thmuse_storereversed_tl _restatecounters_tl }
          {
            \bool_set_true:N \l__keythms_thmuse_restating_bool
            \prop_set_from_keyval:Ne \l__keythms_restate_counters_prop
              { \tl_use:c { c__keythms_storeatbegin_ \l__keythms_thmuse_storereversed_tl _restatecounters_tl } }
            \prop_map_inline:Nn \l__keythms_restate_counters_prop
              {
                \tl_set:ce { l_keythms_restate_current_####1_tl }
                  { \the\value{####1} }
                \setcounter { ####1 } { ####2 }
                % ^ FIX: what if eq's numbered by section, theorem, etc.? The
                %        thmtools code is opaque.... Or maybe should be up to the
                %        user to say "restate-counters={section,chapter,...}".
                \cs_set:cpn { theH ####1 }
                  { \use:c { the ####1 } . \theHkeythms_restate_dummyctr }
              }
            \tl_if_empty:cTF
              { c__keythms_storeatbegin_ \l__keythms_thmuse_storereversed_tl _label_tl }
              { \refstepcounter{keythms_restate_dummyctr} } % for unnumbered theorems
              {
                \cs_set:cpn { the #1 }
                  { \tl_use:c { c__keythms_storeatbegin_ \l__keythms_thmuse_storereversed_tl _label_tl } }
                \cs_set_eq:cN { c@ #1 } \c@keythms_restate_dummyctr
                \cs_set_eq:cN { theH #1 } \theHkeythms_restate_dummyctr
                % ^ why are the last two lines here? We shouldn't be referencing
                %   restated theorems. Think it's a remnant of thmtools
                % WRONG: needed to make numbering correct after restated theorem.
                % not sure about theH. <- this is needed to prevent duplicate anchors
              }
           }
           {
             \msg_warning:nnV { keytheorems } { store-reversed-not-got }
               \l__keythms_thmuse_storereversed_tl
           }
        \__keythms_thm_prehead_code:n { #1 }
        \hook_use:n { keytheorems/#1/restated }
        \hook_use:n { keytheorems/allthms/restated }
        \tl_if_empty:nTF { ##3 }
          { \begin{keythms_orig_#1}[{##2}] }
          { \begin{keythms_orig_#1}[{##2}]<##3> }
        \__keythms_thm_posthead_code:n { #1 }
        % below needs to come after posthead so that correct \@currentHref
        % is stored for tcolorbox theorems
        \__keythms_thm_addstoredreverseddata:nnn { #1 } { ##1 } { ##4 }
        \__keythms_thm_tempstorerestatedatareversed:nnn { #1 } { ##1 } { ##4 }
        ##4
        \__keythms_thm_prefoot_code:n { #1 }
        \end{keythms_orig_#1}
        \__keythms_thm_postfoot_code:n { #1 }
        \prop_map_inline:Nn \l__keythms_restate_counters_prop
          {
            \exp_args:Nnc \setcounter { ####1 }
              { l_keythms_restate_current_####1_tl }
          }
      }
      { }
      % NOTE: have to do a lot of shenanigans to make sure the begin/end of grabbed
      %       theorem env captures only the body and no package code.
      %       This is the price of on-the-fly redefining the env to grab body
      \RenewDocumentEnvironment { #1 } { D<>{} ={note} O{} D<>{} }
        {
          \tl_set:Nn \l_keythms_thmuse_envname_tl { #1 }
          \keys_set:nn { keytheorems/thmuse } { ##2 }
          \tl_if_empty:NTF \l__keythms_thmuse_store_tl
            {
              \tl_if_empty:NTF \l__keythms_thmuse_storereversed_tl
                { \__keythms_thm_prehead_code:n { #1 } }
                {
                  % \bool_gset_true:N \g__keythms_listof_writefile_bool % disable writing to file
                  \cs_set_eq:NN \__keythms_beamer_withhooks_begin:nnnn \__keythms_beamer_grabreversed_begin:nnnn
                  \cs_set_eq:NN \__keythms_beamer_withhooks_end:n \__keythms_beamer_grabreversed_end:n
                }
            }
            {
              % \bool_gset_true:N \g__keythms_listof_writefile_bool % disable writing to file
              \cs_set_eq:NN \__keythms_beamer_withhooks_begin:nnnn \__keythms_beamer_grab_begin:nnnn
              \cs_set_eq:NN \__keythms_beamer_withhooks_end:n \__keythms_beamer_grab_end:n
            }
          \tl_if_empty:nTF { ##3 }
            { % if ##3 empty, just pass ##1 and withhooks_begin will check if empty
              \__keythms_beamer_withhooks_begin:nnVn { #1 } { ##2 }
                \l__keythms_thmuse_note_tl { ##1 }
            }
            { % if ##3 nonempty, always use it (beamer defaults to second <> if both given)
              \__keythms_beamer_withhooks_begin:nnVn { #1 } { ##2 }
                \l__keythms_thmuse_note_tl { ##3 }
            }
        }
        {
          \__keythms_beamer_withhooks_end:n { #1 }
          \tl_if_empty:NF \l__keythms_thmuse_store_tl
            {
              \cs_if_exist:cF
                { __keythms_getthm_ \l__keythms_thmuse_store_tl _theorem }
                {
                  \cs_new:cpe
                    { __keythms_getthm_ \l__keythms_thmuse_store_tl _theorem }
                    {
                      \exp_not:N \__keythms_getthm_theorem:nnnnn
                      \exp_not:o { \g__keythms_thmuse_temprestatedata_tl }
                    }
                  \cs_new:cpe
                    { __keythms_getthm_ \l__keythms_thmuse_store_tl _body }
                    {
                      \exp_not:N \__keythms_getthm_body:nnn
                      \exp_args:No \exp_not:o
                        {
                          \exp_after:wN \__keythms_use_i_iii_v_braced:nnnnn
                            \g__keythms_thmuse_temprestatedata_tl
                        }
                    }
                }
            }
          \tl_if_empty:NF \l__keythms_thmuse_storereversed_tl
            {
              \cs_if_exist:cF
                { __keythms_getthm_ \l__keythms_thmuse_storereversed_tl _theorem }
                {
                  \cs_new:cpe
                    { __keythms_getthm_ \l__keythms_thmuse_storereversed_tl _theorem }
                    {
                      \exp_not:N \__keythms_getthmreversed_theorem:nnn
                      \exp_not:o { \g__keythms_thmuse_temprestatedatareversed_tl }
                    }
                  \cs_new:cpn
                    { __keythms_getthm_ \l__keythms_thmuse_storereversed_tl _body }
                    {
                      \textbf{??}
                      \msg_warning:nnn { keytheorems } { restate-body-never-got }
                        { #1 }
                    }
                }
            }
        }
  }

\cs_new_protected:Npn \__keythms_beamer_withhooks_begin:nnnn #1#2#3#4
  { % #1 = theorem name, #2 = keys, #3 = note, #4 = action spec
    \tl_if_empty:nTF { #4 }
      { \begin{keythms_orig_#1}[{#3}] }
      { \begin{keythms_orig_#1}[{#3}]<#4> }
    \__keythms_thm_posthead_code:n { #1 }
    \__keythms_thm_addcontentsdata:nnnn { #1 } { } { #2 } { }
    \ignorespaces % I hope this is alright
  }
\cs_generate_variant:Nn \__keythms_beamer_withhooks_begin:nnnn { nnV }
\cs_new_eq:NN \__keythms_beamer_withhooks_end:n \__keythms_withhooks_end:n

\cs_new_protected:Npn \__keythms_beamer_grab_begin:nnnn #1#2#3#4
  { % #1 = theorem name, #2 = keys, #3 = note
    \begin{keythms_beamer_grab_#1}{#2}{#3}{#4}
  }
\cs_generate_variant:Nn \__keythms_beamer_grab_begin:nnnn { nnV }
\cs_new_protected:Npn \__keythms_beamer_grab_end:n #1 { \end{keythms_beamer_grab_#1} }

\cs_new_protected:Npn \__keythms_beamer_grabreversed_begin:nnnn #1#2#3#4
  { % #1 = theorem name, #2 = keys, #3 = note
    \begin{keythms_beamer_grabreversed_#1}{#2}{#3}{#4}
  }
\cs_generate_variant:Nn \__keythms_beamer_grabreversed_begin:nnnn { nnV }
\cs_new_protected:Npn \__keythms_beamer_grabreversed_end:n #1
  { \end{keythms_beamer_grabreversed_#1} }
  
% for now, just disable writing to file
\cs_set_eq:NN \__keythms_thm_addcontentsdata:nnnn \use_none:nnnn
\cs_set_eq:NN \__keythms_thm_addstoredreverseddata:nnn \use_none:nnn

\cs_new_protected:Npn \__keythms_beamer_reversedauxdata:nnnn #1#2#3#4
  {
    \iow_shipout:Ne \@auxout
      {
        \token_to_str:N \KeyThmsBeamerReversedData { \l__keythms_thmuse_storereversed_tl }
          { \@currentlabel }
          { #2 }
      }
  }
\cs_new_protected:Npn \KeyThmsBeamerReversedData #1#2#3
  {
    \tl_if_exist:cF { c__keythms_storeatbegin_#1_label_tl }
      {
        \tl_const:cn { c__keythms_storeatbegin_#1_label_tl } { #2 }
      }
    \tl_if_exist:cF { c__keythms_storeatbegin_#1_restatecounters_tl }
      {
        \tl_const:cn { c__keythms_storeatbegin_#1_restatecounters_tl } { #3 }
      }
    \bool_if_exist:cF { g__keythms_restate_#1_called_bool }
      { \bool_new:c { g__keythms_restate_#1_called_bool } }
  }
\cs_new_eq:NN \__keythms_ORIGgetthmreversed_theorem:nnn
  \__keythms_getthmreversed_theorem:nnn
\cs_set_protected:Npn \__keythms_getthmreversed_theorem:nnn #1#2#3
  {
    \group_begin:
    \cs_set_eq:NN \__keythms_thm_addcontentsdata:nnnn \__keythms_beamer_reversedauxdata:nnnn
    \__keythms_ORIGgetthmreversed_theorem:nnn { #1 } { #2 } { #3 }
    \group_end:
  }

% restatable and restatable* environments need redefinition
\bool_if:NT \g__keythms_thmtoolscompat_bool
  {
    \RenewDocumentEnvironment { restatable } { O{} m m }
      {
        % set store outside [] so keyless note is recognized
        \keys_set:nn { keytheorems/thmuse } { store=#3 }
        \begin{#2}[#1]
      }
      {
        \end{#2}
        \cs_gset_protected:cpn { #3 }
          { % make \foo and \foo* identical
            \peek_meaning_remove:NTF *
              { \getkeytheorem{ #3 } }
              { \getkeytheorem{ #3 } }
          }
      }
    \RenewDocumentEnvironment { restatable* } { O{} m m }
      {
        \cs_set_protected:Npn \__keythms_thm_tempstorerestatedatareversed:nnn ##1##2##3
          {
            \tl_gset:Ne \g__keythms_thmuse_temprestatedatareversed_tl % needs to be global to get out of env
              {
                { ##1 }
                {  store*=\l__keythms_thmuse_storereversed_tl,\exp_not:n { ##2 } }
                { \exp_not:n { ##3 } }
              }
          }
        % set store* outside [] so keyless note is recognized
        \keys_set:nn { keytheorems/thmuse } { store*=#3 }
        \begin{#2}[#1]
      }
      {
        \end{#2}
        \cs_gset_protected:cpn { #3 }
          { % make \foo and \foo* identical
            \peek_meaning_remove:NTF *
              { \getkeytheorem{ #3 } }
              { \getkeytheorem{ #3 } }
          }
      }
  }

\file_input_stop: