summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/embrac/embrac.sty
blob: 30ee698d037cf9b7d103432bd371055cf8d088f2 (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
% --------------------------------------------------------------------------
% the EMBRAC package
%
%   Upright Brackets in Emphasized Text
% 
% --------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/embrac/
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2012 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}
  {2012/11/04}
  {0.2}
  {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

\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
  }

% save original commands:
\cs_new_eq:Nc \__embrac_orig_emph:n { emph~ }
\cs_generate_variant:Nn \__embrac_orig_emph:n { V }

\cs_new_eq:Nc \__embrac_orig_textit:n { textit~ }
\cs_generate_variant:Nn \__embrac_orig_textit:n { V }

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

\cs_new:Npn \__embrac_empty_or_no_value:nTF #1#2#3
  {
    \IfNoValueTF { #1 } { #2 }
      { \tl_if_blank:nTF { #1 } { #2 } { #3 } }
  }
\cs_new:Npn \__embrac_empty_or_no_value:nF #1#2
  {
    \IfNoValueF { #1 }
      { \tl_if_blank:nF { #1 } { #2 } }
  }

% --------------------------------------------------------------------------
% THE MAIN PART:
% storage of the tokens to be replaced:
\prop_new:N \l__embrac_emph_obrackets_prop
\prop_new:N \l__embrac_emph_obrackets_before_prop
\prop_new:N \l__embrac_emph_cbrackets_prop
\prop_new:N \l__embrac_emph_cbrackets_after_prop

% do the replacing:
\cs_new:Npn \__embrac_replace_brackets:N #1
  {
    \prop_map_inline:Nn \l__embrac_emph_obrackets_prop
      {
        \tl_replace_all:Nnn #1 { ##1 }
          {
            \__embrac_kern:n
              { \prop_get:Nn \l__embrac_emph_obrackets_before_prop { ##1 } }
            \embrac_braces_format:n { ##1 \__embrac_kern:n { ##2 } }
            \nobreak
          }
      }
    \prop_map_inline:Nn \l__embrac_emph_cbrackets_prop
      {
        \tl_replace_all:Nnn #1 { ##1 }
          {
            \nobreak
            \embrac_braces_format:n
              { \__embrac_kern:n { ##2 } ##1 }
            \__embrac_kern:n
              { \prop_get:Nn \l__embrac_emph_cbrackets_after_prop { ##1 } }
          }
      }
  }

% internal \emph command:
\cs_new:Npn \__embrac_emph:n #1
  {
    \tl_set:Nx \l__embrac_tmpa_tl { #1 }
    \__embrac_replace_brackets:N \l__embrac_tmpa_tl
    \__embrac_orig_emph:V \l__embrac_tmpa_tl
  }

% internal \textit command:
\cs_new:Npn \__embrac_textit:n #1
  {
    \tl_set:Nn \l__embrac_tmpa_tl { #1 }
    \__embrac_replace_brackets:N \l__embrac_tmpa_tl
    \__embrac_orig_textit:V \l__embrac_tmpa_tl
  }

% biblatex compatibility:
\cs_new: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:Npn \__embrac_treat_bibemph:
  {
    \bool_if:NT \l__embrac_treat_biblatex_bool
      {
        \patchcmd[\protected\long]\blx@imc@mkbibemph
          {\emph}{\emph*}
          {}{}
      }
  }

% internal \emph command, second layer:
\cs_new_protected:Npn \__embrac_new_emph:w #1#2
  {
    \group_begin:
      \__embrac_treat_bibparens:
      \IfBooleanTF { #1 }
        { \__embrac_orig_emph:n { #2 } }
        { \__embrac_emph:n { #2 } }
    \group_end:
  }

% internal \textit command, second layer:
\cs_new_protected:Npn \__embrac_new_textit:w #1#2
  {
    \group_begin:
      \__embrac_treat_bibparens:
      \IfBooleanTF { #1 }
        { \__embrac_orig_textit:n { #2 } }
        { \__embrac_textit:n { #2 } }
    \group_end:
  }

% user command \emph:
\RenewDocumentCommand \emph { sm }
  { \__embrac_new_emph:w #1 { #2 } }

% user command \textit:
\RenewDocumentCommand \textit { sm }
  { \__embrac_new_textit:w #1 { #2 } }

% --------------------------------------------------------------------------
% TURNING EMBRAC OFF AND ON:
% turning embrac off:
\NewDocumentCommand \EmbracOff {}
  {
    \RenewDocumentCommand \emph { sm }
      { \__embrac_orig_emph:n { ##2 } }
    \RenewDocumentCommand \textit { sm }
      { \__embrac_orig_textit:n { ##2 } }
  }

% turning embrac on:
\NewDocumentCommand \EmbracOn {}
  {
    \RenewDocumentCommand \emph { sm }
      { \__embrac_new_emph:w ##1 { ##2 } }
    \RenewDocumentCommand \textit { sm }
      { \__embrac_new_textit:w ##1 { ##2 } }
  }

% --------------------------------------------------------------------------
% ADDING AND REMOVING BRACKETS:
% internal add command:
\cs_new:Npn \__embrac_add_to_emph:nnnnnn #1#2#3#4#5#6
  {
    \__embrac_empty_or_no_value:nTF { #2 }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_prop { #1 } { 0pt } }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_prop { #1 } { #2 } }
    \__embrac_empty_or_no_value:nTF { #3 }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_before_prop { #1 } { 0pt } }
      { \prop_put_if_new:Nnn \l__embrac_emph_obrackets_before_prop { #1 } { #3 } }
    \__embrac_empty_or_no_value:nTF { #5 }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_prop { #4 } { 0pt } }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_prop { #4 } { #5 } }
    \__embrac_empty_or_no_value:nTF { #6 }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_after_prop { #4 } { 0pt } }
      { \prop_put_if_new:Nnn \l__embrac_emph_cbrackets_after_prop { #4 } { #6 } }
  }

% internal delete command:
\cs_new:Npn \__embrac_remove_from_emph:nn #1#2
  {
    \prop_del:Nn \l__embrac_emph_obrackets_prop { #1 }
    \prop_del:Nn \l__embrac_emph_obrackets_before_prop { #1 }
    \prop_del:Nn \l__embrac_emph_cbrackets_prop { #2 }
    \prop_del:Nn \l__embrac_emph_cbrackets_after_prop { #2 }
  }

% internal renew command:
\cs_new:Npn \__embrac_renew_emph:nnnnnn #1#2#3#4#5#6
  {
    \__embrac_empty_or_no_value:nTF { #2 }
      { \prop_put:Nnn \l__embrac_emph_obrackets_prop { #1 } { 0pt } }
      { \prop_put:Nnn \l__embrac_emph_obrackets_prop { #1 } { #2 } }
    \__embrac_empty_or_no_value:nTF { #3 }
      { \prop_put:Nnn \l__embrac_emph_obrackets_before_prop { #1 } { 0pt } }
      { \prop_put:Nnn \l__embrac_emph_obrackets_before_prop { #1 } { #3 } }
    \__embrac_empty_or_no_value:nTF { #5 }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_prop { #4 } { 0pt } }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_prop { #4 } { #5 } }
    \__embrac_empty_or_no_value:nTF { #6 }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_after_prop { #4 } { 0pt } }
      { \prop_put:Nnn \l__embrac_emph_cbrackets_after_prop { #4 } { #6 } }
  }

% internal change command:
\cs_new:Npn \__embrac_change_emph:nnnnnn #1#2#3#4#5#6
  {
    \prop_if_in:NnT \l__embrac_emph_obrackets_prop { #1 }
      {
        \__embrac_empty_or_no_value:nF { #2 }
          { \prop_put:Nnn \l__embrac_emph_obrackets_prop { #1 } { #2 } }
        \__embrac_empty_or_no_value:nF { #3 }
          { \prop_put:Nnn \l__embrac_emph_obrackets_before_prop { #1 } { #3 } }
      }
    \prop_if_in:NnT \l__embrac_emph_cbrackets_prop { #4 }
      {
        \__embrac_empty_or_no_value:nF { #5 }
          { \prop_put:Nnn \l__embrac_emph_cbrackets_prop { #4 } { #5 } }
        \__embrac_empty_or_no_value:nF { #6 }
          { \prop_put:Nnn \l__embrac_emph_cbrackets_after_prop { #4 } { #6 } }
      }
  }

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

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

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

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

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

\ProcessKeysOptions { embrac }

\AtBeginDocument { \__embrac_treat_bibemph: }

\tex_endinput:D

% 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