summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/crefthe/crefthe.sty
blob: ad752f87b5d762802e87cdcd87809f0b8e69921e (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
%%
%% Copyright (C) 2021-2022 by Jinwen XU
%% -------------------------------
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c 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
%%
\NeedsTeXFormat{LaTeX2e}[2020-10-01]
\ProvidesExplPackage
  {crefthe}
  {2022/01/29}
  {}
  {Cross referencing with proper definite articles}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{cleveref}}
\ProcessOptions\relax
\RequirePackage{cleveref}

\tl_gset:Nn \g_crefthe_prepf_tl {}
\tl_gset:Nn \g_crefthe_prep_tl {}
\tl_gset:Nn \g_crefthe_sep_tl {}
\cs_new:Nn \crefthe_case:n {#1}
\cs_new:Nn \crefthe_nochange:n {#1}
\cs_new:Nn \crefthe_atbegindocument:n {#1}

\cs_new:Nn \crefthe_set_status_atbegindocument: {
    \cs_if_eq:NNTF \@onlypreamble \@notprerr {
        \cs_gset_eq:NN \crefthe_atbegindocument:n \crefthe_nochange:n
    }{
        \cs_gset_eq:NN \crefthe_atbegindocument:n \AtBeginDocument
    }
}

\NewDocumentCommand{\crefthe}{st-t+O{}m}{
    \IfBooleanTF{ #2 }{
        \tl_gset:Nn \g_crefthe_prepf_tl { #4 }
    }{
        \IfBooleanTF{ #3 }{
            \tl_gset:Nn \g_crefthe_prep_tl { #4 }
        }{
            \str_case_e:nn { \crefthe_prep_mode:nn } {
                { - } { \tl_gset:Nn \g_crefthe_prepf_tl { #4 } }
                { + } { \tl_gset:Nn \g_crefthe_prep_tl  { #4 } }
            }
        }
    }
    \IfBooleanTF{ #1 } { \cref*{#5} } { \cref{#5} }
    \tl_gset:Nn \g_crefthe_prep_tl {}
}

\NewDocumentCommand{\Crefthe}{st-t+O{}m}{
    \cs_gset_eq:NN \crefthe_case:n \text_titlecase:n
    \IfBooleanTF{ #2 }{
        \tl_gset:Nn \g_crefthe_prepf_tl { #4 }
    }{
        \IfBooleanTF{ #3 }{
            \tl_gset:Nn \g_crefthe_prep_tl { #4 }
        }{
            \str_case_e:nn { \crefthe_prep_mode:nn } {
                { - } { \tl_gset:Nn \g_crefthe_prepf_tl { #4 } }
                { + } { \tl_gset:Nn \g_crefthe_prep_tl  { #4 } }
            }
        }
    }
    \IfBooleanTF{ #1 } { \Cref*{#5} } { \Cref{#5} }
    \tl_gset:Nn \g_crefthe_prep_tl {}
}

\NewDocumentCommand{\crefthename}{mO{}mO{}m}{
    \str_if_eq:eeTF {#2} {} {
        \crefname{#1}{#3}{#5}
        \cs_set:cn {cref_#1_format:nnn} {##2#3~##1##3}
        \cs_set:cn {cref_#1_format_first:nnn} {##2#5~##1##3}
    }{
        \crefname{#1}{\crefthemark{#2} \g_crefthe_sep_tl #3}{\crefthemark{#4} \g_crefthe_sep_tl #5}
        \cs_set:cn {cref_#1_format:nnn} {\crefthemark{#2} \g_crefthe_sep_tl ##2#3~##1##3}
        \cs_set:cn {cref_#1_format_first:nnn} {\crefthemark{#4} \g_crefthe_sep_tl ##2#5~##1##3}
    }
    \crefthe_set_status_atbegindocument:
    \crefthe_atbegindocument:n {
        \cs_set_eq:cc {cref@#1@format} {cref_#1_format:nnn}
        \cs_set_eq:cc {cref@#1@format@first} {cref_#1_format_first:nnn}
    }
}

\NewDocumentCommand{\Crefthename}{mO{}mO{}m}{
    \str_if_eq:eeTF {#2} {} {
        \Crefname{#1}{#3}{#5}
        \cs_set:cn {Cref_#1_format:nnn} {##2#3~##1##3}
        \cs_set:cn {Cref_#1_format_first:nnn} {##2#5~##1##3}
    }{
        \Crefname{#1}{\crefthemark{#2} \g_crefthe_sep_tl #3}{\crefthemark{#4} \g_crefthe_sep_tl #5}
        \cs_set:cn {Cref_#1_format:nnn} {\crefthemark{#2} \g_crefthe_sep_tl ##2#3~##1##3}
        \cs_set:cn {Cref_#1_format_first:nnn} {\crefthemark{#4} \g_crefthe_sep_tl ##2#5~##1##3}
    }
    \crefthe_set_status_atbegindocument:
    \crefthe_atbegindocument:n {
        \cs_set_eq:cc {Cref@#1@format} {Cref_#1_format:nnn}
        \cs_set_eq:cc {Cref@#1@format@first} {Cref_#1_format_first:nnn}
    }
}

\NewDocumentCommand{\crefthemark}{m}{
    \crefthe_contraction:nn { \crefthe_case:n \g_crefthe_prep_tl }{
        \crefthe_contraction:nn { \crefthe_case:n \g_crefthe_prepf_tl }{#1}
    }
    \tl_gset:Nn \g_crefthe_prepf_tl {}
    \tl_gset:Nx \g_crefthe_prep_tl { \text_lowercase:n \g_crefthe_prep_tl }
    \str_case_e:nnF {\str_tail:n {#1}} {
        {'} { \tl_gset:Nn \g_crefthe_sep_tl {} }
    }
    { \tl_gset:Nn \g_crefthe_sep_tl {~} }
    \cs_gset_eq:NN \crefthe_case:n \crefthe_nochange:n
    % \cs_gset_eq:NN \crefthe_case:n \text_lowercase:n
}

\cs_set:Npn \crefthe_prep_mode:nn {
    \str_case_e:nn { \languagename }
    {
        {french}        { + }
        {italian}       { + }
        {spanish}       { - }
        {portuguese}    { + }
        {brazilian}     { + }
    }
}

\cs_set:Npn \crefthe_contraction:nn #1#2 {
    \str_if_eq:eeTF {#1} {} {#2} {
        \str_case_e:nn { \languagename }
        {
            {french}{
                \str_case_e:nnF {#1~\text_lowercase:n{#2}} {
                    {à~le}      {au}
                    {à~les}     {aux}
                    {de~le}     {du}
                    {de~les}    {des}
                    {À~le}      {Au}
                    {À~les}     {Aux}
                    {De~le}     {Du}
                    {De~les}    {Des}
                }{#1~\text_lowercase:n{#2}}
            }
            {italian}{
                \str_case_e:nnF {#1~\text_lowercase:n{#2}} {
                    {a~il}      {al}
                    {a~lo}      {allo}
                    {a~l'}      {all'}
                    {a~la}      {alla}
                    {di~il}     {del}
                    {di~lo}     {dello}
                    {di~l'}     {dell'}
                    {di~la}     {della}
                    {da~il}     {dal}
                    {da~lo}     {dallo}
                    {da~l'}     {dall'}
                    {da~la}     {dalla}
                    {in~il}     {nel}
                    {in~lo}     {nello}
                    {in~l'}     {nell'}
                    {in~la}     {nella}
                    {su~il}     {sul}
                    {su~lo}     {sullo}
                    {su~l'}     {sull'}
                    {su~la}     {sulla}
                    {a~i}       {ai}
                    {a~gli}     {agli}
                    {a~le}      {alle}
                    {di~i}      {dei}
                    {di~gli}    {degli}
                    {di~le}     {delle}
                    {da~i}      {dai}
                    {da~gli}    {dagli}
                    {da~le}     {dalle}
                    {in~i}      {nei}
                    {in~gli}    {negli}
                    {in~le}     {nelle}
                    {su~i}      {sui}
                    {su~gli}    {sugli}
                    {su~le}     {sulle}
                    {A~il}      {Al}
                    {A~lo}      {Allo}
                    {A~l'}      {All'}
                    {A~la}      {Alla}
                    {Di~il}     {Del}
                    {Di~lo}     {Dello}
                    {Di~l'}     {Dell'}
                    {Di~la}     {Della}
                    {Da~il}     {Dal}
                    {Da~lo}     {Dallo}
                    {Da~l'}     {Dall'}
                    {Da~la}     {Dalla}
                    {In~il}     {Nel}
                    {In~lo}     {Nello}
                    {In~l'}     {Nell'}
                    {In~la}     {Nella}
                    {Su~il}     {Sul}
                    {Su~lo}     {Sullo}
                    {Su~l'}     {Sull'}
                    {Su~la}     {Sulla}
                    {A~i}       {Ai}
                    {A~gli}     {Agli}
                    {A~le}      {Alle}
                    {Di~i}      {Dei}
                    {Di~gli}    {Degli}
                    {Di~le}     {Delle}
                    {Da~i}      {Dai}
                    {Da~gli}    {Dagli}
                    {Da~le}     {Dalle}
                    {In~i}      {Nei}
                    {In~gli}    {Negli}
                    {In~le}     {Nelle}
                    {Su~i}      {Sui}
                    {Su~gli}    {Sugli}
                    {Su~le}     {Sulle}
                }{#1~\text_lowercase:n{#2}}
            }
            {portuguese}{
                \str_case_e:nnF {#1~\text_lowercase:n{#2}} {
                    {a~o}       {ao}
                    {a~a}       {à}
                    {a~os}      {aos}
                    {a~as}      {às}
                    {de~o}      {do}
                    {de~a}      {da}
                    {de~os}     {dos}
                    {de~as}     {das}
                    {em~o}      {no}
                    {em~a}      {na}
                    {em~os}     {nos}
                    {em~as}     {nas}
                    {A~o}       {Ao}
                    {A~a}       {À}
                    {A~os}      {Aos}
                    {A~as}      {Às}
                    {De~o}      {Do}
                    {De~a}      {Da}
                    {De~os}     {Dos}
                    {De~as}     {Das}
                    {Em~o}      {No}
                    {Em~a}      {Na}
                    {Em~os}     {Nos}
                    {Em~as}     {Nas}
                }{#1~\text_lowercase:n{#2}}
            }
            {brazilian}{
                \str_case_e:nnF {#1~\text_lowercase:n{#2}} {
                    {a~o}       {ao}
                    {a~a}       {à}
                    {a~os}      {aos}
                    {a~as}      {às}
                    {de~o}      {do}
                    {de~a}      {da}
                    {de~os}     {dos}
                    {de~as}     {das}
                    {em~o}      {no}
                    {em~a}      {na}
                    {em~os}     {nos}
                    {em~as}     {nas}
                    {A~o}       {Ao}
                    {A~a}       {À}
                    {A~os}      {Aos}
                    {A~as}      {Às}
                    {De~o}      {Do}
                    {De~a}      {Da}
                    {De~os}     {Dos}
                    {De~as}     {Das}
                    {Em~o}      {No}
                    {Em~a}      {Na}
                    {Em~os}     {Nos}
                    {Em~as}     {Nas}
                }{#1~\text_lowercase:n{#2}}
            }
            {spanish}{
                \str_case_e:nnF {#1~\text_lowercase:n{#2}} {
                    {a~el}      {al}
                    {de~el}     {del}
                    {A~el}      {Al}
                    {De~el}     {Del}
                }{#1~\text_lowercase:n{#2}}
            }
        }
    }
}
\endinput
%%
%% End of file `crefthe.sty'.