summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/embrac/embrac.sty
blob: 55786b982af7caa404b0fb223b83cfa73bc93937 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
% --------------------------------------------------------------------------
% the EMBRAC package
%
%   Upright Brackets in Emphasized Text
% 
% --------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/embrac/
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2012--2019 Clemens Niederberger
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
% The embrac package consists of the files
%  - embrac.sty, embrac_en.tex, embrac_en.pdf, embrac_kerning_test.tex and
%    README
% --------------------------------------------------------------------------
% > this package is strongly based on an article by Dominik Waßenhoven in
% > “Die TeXnische Komödie” 2 (2012), pp. 51--53
% > which introduces code by Bruno Le Floch. Code parts and idea used with
% > their kind permission. Many thanks!
% --------------------------------------------------------------------------
\RequirePackage { expl3 , xparse , l3keys2e }
\ProvidesExplPackage
  {embrac}
  {2019/12/31}
  {0.9}
  {Upright Brackets in Emphasized Text}

% --------------------------------------------------------------------------
\bool_new:N \l__embrac_treat_biblatex_bool

\tl_new:N \l__embrac_tmpa_tl
\tl_new:N \l__embrac_treat_biblatex_tl

\int_new:N \l__embrac_penalty_int

\keys_define:nn {embrac}
  {
    biblatex         .choice: ,
    biblatex / true  .code:n =
      \bool_set_true:N \l__embrac_treat_biblatex_bool ,
    biblatex / on    .code:n =
      \bool_set_true:N \l__embrac_treat_biblatex_bool ,
    biblatex / parens .code:n =
      \bool_set_true:N \l__embrac_treat_biblatex_bool ,
    biblatex / false  .code:n =
      \bool_set_false:N \l__embrac_treat_biblatex_bool ,
    biblatex / off    .code:n =
      \bool_set_false:N \l__embrac_treat_biblatex_bool ,
    biblatex / none   .code:n =
      \bool_set_false:N \l__embrac_treat_biblatex_bool ,
    biblatex          .default:n = true
  }

\prg_new_conditional:Npnn \embrac_if_fontspec: { T,F,TF }
  {
    \@ifpackageloaded {fontspec}
      { \prg_return_true: }
      { \prg_return_false: }
  }

\cs_generate_variant:Nn \cs_generate_variant:Nn {c}

% preparations:
\cs_new_eq:NN \embrac_braces_format:n \textup
\cs_new_eq:NN \embrac_kern:n \skip_horizontal:n

\prg_new_conditional:Npnn \embrac_empty_or_no_value:n #1 { F,TF }
  {
    \IfNoValueTF {#1}
      { \prg_return_true: }
      {
        \tl_if_blank:nTF {#1}
          { \prg_return_true: }
          { \prg_return_false: }
      }
  }

\cs_new_protected:Npn \embrac_nobreak:
  { \tex_penalty:D 10000 \scan_stop: }

\cs_new_protected:Npn \embrac_allow_break:
  { \tex_penalty:D 0 \scan_stop: }

% --------------------------------------------------------------------------
% THE MAIN PART:
% storage of the tokens to be replaced:
\prop_new:N \l__embrac_emph_obrackets_symbol_prop
\prop_new:N \l__embrac_emph_obrackets_inner_prop
\prop_new:N \l__embrac_emph_obrackets_outer_prop
\prop_new:N \l__embrac_emph_cbrackets_symbol_prop
\prop_new:N \l__embrac_emph_cbrackets_inner_prop
\prop_new:N \l__embrac_emph_cbrackets_outer_prop

\bool_new:N \l__embrac_opening_bool

% #1: tl macro
% #2: kerning before
% #3: symbol
% #4: kerning after
% #5: code after
\cs_new_protected:Npn \__embrac_replace:Nnnnn #1#2#3#4#5
  {
    \tl_replace_all:Nnn #1 {#3}
      {
        \mode_if_math:TF
          {#4}
          {
            \embrac_nobreak:
            \embrac_kern:n {#2}
            \embrac_nobreak:
            \embrac_braces_format:n {#3}
            \embrac_nobreak:
            \embrac_kern:n {#4}
            #5
          }
      }
  }
\cs_generate_variant:Nn \__embrac_replace:Nnnnn {Nxx,Nnxx}

% do the replacing:
% #1: tl macro
\cs_new_protected:Npn \embrac_replace_brackets:N #1
  {
    \prop_map_inline:Nn \l__embrac_emph_obrackets_inner_prop
      {
        \__embrac_replace:Nxxnn #1
          { \prop_item:Nn \l__embrac_emph_obrackets_outer_prop {##1} }
          { \prop_item:Nn \l__embrac_emph_obrackets_symbol_prop {##1} }
          {##2}
          { \embrac_nobreak: }
      }
    \prop_map_inline:Nn \l__embrac_emph_cbrackets_inner_prop
      {
        \__embrac_replace:Nnxxn #1
          {##2}
          { \prop_item:Nn \l__embrac_emph_cbrackets_symbol_prop {##1} }
          { \prop_item:Nn \l__embrac_emph_cbrackets_outer_prop {##1} }
          {
            \peek_charcode:NTF \c_space_tl
              { \embrac_allow_break: }
              { \embrac_nobreak: }
          }
      }
  }

\cs_new_protected:Npn \embrac_enparen:nnn #1#2#3
  {
    \embrac_nobreak:
    \embrac_kern:n
      { \prop_item:Nn \l__embrac_emph_obrackets_outer_prop {#1} }
    \embrac_nobreak:
    \embrac_braces_format:n
      { \prop_item:Nn \l__embrac_emph_obrackets_symbol_prop {#1} }
    \embrac_nobreak:
    \embrac_kern:n
      { \prop_item:Nn \l__embrac_emph_obrackets_inner_prop {#1} }
    #3
    \embrac_nobreak:
    \embrac_kern:n
      { \prop_item:Nn \l__embrac_emph_cbrackets_inner_prop {#2} }
    \embrac_nobreak:
    \embrac_braces_format:n
      { \prop_item:Nn \l__embrac_emph_cbrackets_symbol_prop {#2} }
    \embrac_nobreak:
    \embrac_kern:n
      { \prop_item:Nn \l__embrac_emph_cbrackets_outer_prop {#2} }
    \peek_charcode:NTF \c_space_tl
      { \embrac_allow_break: }
      { \embrac_nobreak: }
  }

% --------------------------------------------------------------------------
% biblatex compatibility:
\cs_new_protected:Npn \embrac_treat_bibparens:
  {
    \bool_if:NT \l__embrac_treat_biblatex_bool
      {
        \embrac_replace_brackets:N \bibleftbracket
        \embrac_replace_brackets:N \bibrightbracket
        \embrac_replace_brackets:N \bibleftparen
        \embrac_replace_brackets:N \bibrightparen
      }
  }

\cs_new_protected:Npn \embrac_treat_bibemph:
  {
    \bool_if:NT \l__embrac_treat_biblatex_bool
      {
        \patchcmd[\protected\long]\blx@imc@mkbibemph
          {\emph}{\emph*}
          {}{}
        \patchcmd[\protected\long]\blx@imc@mkbibitalic
          {\textit}{\textit*}
          {}{}
      }
  }

% --------------------------------------------------------------------------
% redefine \emph and friends:
\seq_new:N \l__embrac_changed_macros_seq

% #1: name of macro to be treated
\cs_new_protected:Npn \embrac_new_replacement_macro:n #1
  {
    \seq_put_right:Nn \l__embrac_changed_macros_seq {#1}
    \cs_new_eq:cc {embrac_orig_#1:n} {#1~}
    \cs_generate_variant:cn {embrac_orig_#1:n} {V}
    \cs_new_protected:cpn {__embrac_#1:n} ##1
      {
        \tl_set:Nn \l__embrac_tmpa_tl {##1}
        \embrac_replace_brackets:N \l__embrac_tmpa_tl
        \use:c {embrac_orig_#1:V} \l__embrac_tmpa_tl
      }
    \cs_new_protected:cpn {embrac_#1:nn} ##1##2
      {
        \group_begin:
          \embrac_treat_bibparens:
          \tl_if_eq:nnTF {##1} {*}
            { \use:c {embrac_orig_#1:n} {##2} }
            { \use:c {__embrac_#1:n} {##2} }
        \group_end:
      }
    \exp_args:Nc \RenewDocumentCommand {#1} {sm}
      {
        \IfBooleanTF {##1}
          { \use:c {embrac_#1:nn} {*} {##2} }
          { \use:c {embrac_#1:nn} { } {##2} }
      }
  }

\NewDocumentCommand \EmbracMakeKnown {m}
  { \embrac_new_replacement_macro:n {#1} }
  
\EmbracMakeKnown {emph}
\EmbracMakeKnown {textit}
\EmbracMakeKnown {textsl}
\AtBeginDocument
  { \embrac_if_fontspec:T { \EmbracMakeKnown {textsi} } }

% --------------------------------------------------------------------------
% TURNING EMBRAC OFF AND ON:
% turning embrac off:
\NewDocumentCommand \EmbracOff {}
  {
    \seq_map_inline:Nn \l__embrac_changed_macros_seq
      {
        \exp_args:Nc \RenewDocumentCommand {##1} {sm}
          { \use:c {embrac_orig_##1:n} {####2} }
      }
  }

% turning embrac on:
\NewDocumentCommand \EmbracOn {}
  {
    \seq_map_inline:Nn \l__embrac_changed_macros_seq
      {
        \exp_args:Nc \RenewDocumentCommand {##1} {sm}
          {
            \IfBooleanTF {####1}
              { \use:c {embrac_##1:nn} {*} {####2} }
              { \use:c {embrac_##1:nn} { } {####2} }
          }
      }
  }

% --------------------------------------------------------------------------
% ADDING AND REMOVING BRACKETS:
% internal add commands:
\cs_new_protected:Npn \embrac_add_op_to_emph:nnn #1#2#3
  {
    \prop_put:Nnn \l__embrac_emph_obrackets_symbol_prop {#1} {#1}
    \embrac_empty_or_no_value:nTF {#2}
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_inner_prop {#1} {0pt} }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_inner_prop {#1} {#2} }
    \embrac_empty_or_no_value:nTF {#3}
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_outer_prop {#1} {0pt} }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_outer_prop {#1} {#3} }
  }

\cs_new_protected:Npn \embrac_add_cl_to_emph:nnn #1#2#3
  {
    \prop_put:Nnn \l__embrac_emph_cbrackets_symbol_prop {#1} {#1}
    \embrac_empty_or_no_value:nTF {#2}
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_inner_prop {#1} {0pt} }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_inner_prop {#1} {#2} }
    \embrac_empty_or_no_value:nTF {#3}
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_outer_prop {#1} {0pt} }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_outer_prop {#1} {#3} }
  }

\cs_new_protected:Npn \embrac_add_to_emph:nnnnnn #1#2#3#4#5#6
  {
    \embrac_add_op_to_emph:nnn {#1} {#2} {#3}
    \embrac_add_cl_to_emph:nnn {#4} {#5} {#6}
  }

% internal delete commands:
\cs_new_protected:Npn \embrac_remove_op_from_emph:n #1
  {
    \prop_remove:Nn \l__embrac_emph_obrackets_inner_prop {#1}
    \prop_remove:Nn \l__embrac_emph_obrackets_outer_prop {#1}
  }

\cs_new_protected:Npn \embrac_remove_cl_from_emph:n #1
  {
    \prop_remove:Nn \l__embrac_emph_cbrackets_inner_prop {#1}
    \prop_remove:Nn \l__embrac_emph_cbrackets_outer_prop {#1}
  }
  
\cs_new_protected:Npn \embrac_remove_from_emph:nn #1#2
  {
    \embrac_remove_op_from_emph:n {#1}
    \embrac_remove_cl_from_emph:n {#2}
  }

% internal renew commands:
\cs_new_protected:Npn \embrac_renew_op_emph:nnn #1#2#3
  {
    \embrac_empty_or_no_value:nTF {#2}
      { \prop_put:Nnn \l__embrac_emph_obrackets_inner_prop {#1} {0pt} }
      { \prop_put:Nnn \l__embrac_emph_obrackets_inner_prop {#1} {#2} }
    \embrac_empty_or_no_value:nTF {#3}
      { \prop_put:Nnn \l__embrac_emph_obrackets_outer_prop {#1} {0pt} }
      { \prop_put:Nnn \l__embrac_emph_obrackets_outer_prop {#1} {#3} }
  }

\cs_new_protected:Npn \embrac_renew_cl_emph:nnn #1#2#3
  {
    \embrac_empty_or_no_value:nTF {#2}
      { \prop_put:Nnn \l__embrac_emph_cbrackets_inner_prop {#1} {0pt} }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_inner_prop {#1} {#2} }
    \embrac_empty_or_no_value:nTF {#3}
      { \prop_put:Nnn \l__embrac_emph_cbrackets_outer_prop {#1} {0pt} }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_outer_prop {#1} {#3} }
  }

\cs_new_protected:Npn \embrac_renew_emph:nnnnnn #1#2#3#4#5#6
  {
    \embrac_renew_op_emph:nnn {#1} {#2} {#3}
    \embrac_renew_cl_emph:nnn {#4} {#5} {#6}
  }

% internal change commands:
\cs_new_protected:Npn \embrac_change_op_emph:nnn #1#2#3
  {
    \prop_if_in:NnT \l__embrac_emph_obrackets_inner_prop {#1}
      {
        \embrac_empty_or_no_value:nF {#2}
          { \prop_put:Nnn \l__embrac_emph_obrackets_inner_prop {#1} {#2} }
        \embrac_empty_or_no_value:nF {#3}
          { \prop_put:Nnn \l__embrac_emph_obrackets_outer_prop {#1} {#3} }
      }
  }

\cs_new_protected:Npn \embrac_change_cl_emph:nnn #1#2#3
  {
    \prop_if_in:NnT \l__embrac_emph_cbrackets_inner_prop {#1}
      {
        \embrac_empty_or_no_value:nF {#2}
          { \prop_put:Nnn \l__embrac_emph_cbrackets_inner_prop {#1} {#2} }
        \embrac_empty_or_no_value:nF {#3}
          { \prop_put:Nnn \l__embrac_emph_cbrackets_outer_prop {#1} {#3} }
      }
  }

\cs_new_protected:Npn \embrac_change_emph:nnnnnn #1#2#3#4#5#6
  {
    \embrac_change_op_emph:nnn {#1} {#2} {#3}
    \embrac_change_cl_emph:nnn {#4} {#5} {#6}
  }

% --------------------------------------------------------------------------
% user commands:
\NewDocumentCommand \AddEmph
  {
    m > { \SplitArgument {1} {,} } O{,}
    m > { \SplitArgument {1} {,} } O{,}
  }
  {
    \embrac_add_to_emph:nnnnnn {#1} #2 {#3} #4
    \ignorespaces
  }

\NewDocumentCommand \AddOpEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_add_op_to_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \AddClEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_add_cl_to_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \DeleteEmph { mm }
  {
    \embrac_remove_from_emph:nn {#1} {#2}
    \ignorespaces
  }

\NewDocumentCommand \DeleteOpEmph { mm }
  {
    \embrac_remove_op_from_emph:n {#1}
    \ignorespaces
  }

\NewDocumentCommand \DeleteClEmph { mm }
  {
    \embrac_remove_cl_from_emph:n {#1}
    \ignorespaces
  }

\NewDocumentCommand \RenewEmph
  {
    m > { \SplitArgument {1} {,} } O{,}
    m > { \SplitArgument {1} {,} } O{,}
  }
  {
    \embrac_renew_emph:nnnnnn {#1} #2 {#3} #4
    \ignorespaces
  }

\NewDocumentCommand \RenewOpEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_renew_op_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \RenewClEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_renew_cl_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \ChangeEmph
  {
    m > { \SplitArgument {1} {,} } O{,}
    m > { \SplitArgument {1} {,} } O{,}
  }
  {
    \embrac_change_emph:nnnnnn {#1} #2 {#3} #4
    \ignorespaces
  }

\NewDocumentCommand \ChangeOpEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_change_op_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \ChangeClEmph
  { m > { \SplitArgument {1} {,} } O{,} }
  {
    \embrac_change_cl_emph:nnn {#1} #2
    \ignorespaces
  }

\NewDocumentCommand \embparen {+m}
  { \embrac_enparen:nnn {(} {)} {#1} }

\NewDocumentCommand \embbracket {+m}
  { \embrac_enparen:nnn {[} {]} {#1} }

\NewDocumentCommand \emb {mm+m}
  { \embrac_enparen:nnn {#1} {#2} {#3} }

% --------------------------------------------------------------------------
% add some defaults and finalize package:
\AddEmph{[}{]}[.04em,-.12em]
\AddEmph{(}[-.04em]{)}[,-.15em]

\ProcessKeysOptions {embrac}

\AtBeginDocument { \embrac_treat_bibemph: }

\file_input_stop:

% --------------------------------------------------------------------------
% HISTORY
2012/06/29 - v0.1  - first public release
2012/06/29 - v0.1a - renamed \RenewEmph => \ChangeEmph and added new \RenewEmph
2012/07/24 - v0.1b - adapted to deprecated functions in l3kernel and l3packages
2012/11/04 - v0.2  - extended `biblatex' option: parens/full
                   - changed buggy definition of \EmbracOff and \EmbracOn
2013/03/22 - v0.3  - made definitions robust where appropriate
                   - added support for `fontspec's \textsi
2013/04/04 - v0.3a - bug fix in \EmbracOn and \EmbracOff
2013/05/13 - v0.4  - added versions of \AddEmph, \RenewEmph, \DeleteEmph and
                     \ChangeEmph that allow setting opening or closing parts
                     separately
2014/05/07 - v0.5  - renaming of some internal commands
                   - leave brackets unchanged if in math mode
2014/06/24 - v0.6  - add support for \textsl
2014/07/03 - v0.6a - bugfix: remove unwanted (and unnecessary) expansion in
                     \__embrac_emph:n
2015/09/06 - v0.6b - fix https://bitbucket.org/cgnieder/embrac/issues/5
2015/11/13 - v0.6c - avoid code duplication
2016/01/07 - v0.6d - \prop_get:Nn => \prop_item:Nn
2017/07/04 - v0.7  - implement issue #8 (now treatment of symbols with catcode
                     other than 12 is possible)
2019/10/01 - v0.8  - fix issue #9
                   - new macros \embparen, \embbracket and \emb
2019/12/31 - v0.9  - new: \EmbracMakeKnown
                   - change penalties