-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaobiblio.sty
421 lines (386 loc) · 14.6 KB
/
kaobiblio.sty
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
\ProvidesPackage{kaobiblio}
\RequirePackage{etoolbox} % Easy programming to modify TeX stuff
\RequirePackage{perpage} % Reset counters
\RequirePackage{iflang} % Check the document language
\RequirePackage{xparse} % Parse arguments for macros
\RequirePackage{xstring} % Parse strings
\RequirePackage{hyperref} % Required for hyperlinks
\RequirePackage{kvoptions} % Handle package options
\SetupKeyvalOptions{
family = kaobiblio,
prefix = kaobiblio@
}
\DeclareBoolOption{addspace}
\DeclareBoolOption{linkeverything}
% Choose the default options, which will be overwritten by the options
% passed to this package.
\PassOptionsToPackage{
%style=numeric-comp,
%citestyle=authortitle-icomp,
citestyle=numeric-comp,
%bibstyle=authoryear,
bibstyle=numeric,
sorting=none,
%sorting=nyt,
%sortcites=true,
%autocite=footnote,
backend=biber, % Compile the bibliography with biber
hyperref=true,
backref=true,
citecounter=true,
pagetracker=true,
citetracker=true,
ibidtracker=context,
autopunct=true,
autocite=plain,
}{biblatex}
% Pass the unknown options to biblatex, overwriting the previous settings. Avoid passing the kao-specific options.
\DeclareDefaultOption{%
\IfBeginWith{\CurrentOption}{addspace}{}{%
\IfBeginWith{\CurrentOption}{linkeverything}{}{%
\PassOptionsToPackage{\CurrentOption}{biblatex}%
}}%
}
% Process the options
\ProcessKeyvalOptions{kaobiblio}
% Load biblatex
\RequirePackage{biblatex}
% Remove some unwanted entries from the bibliography
\AtEveryBibitem{
\clearfield{issn}
\clearfield{isbn}
\clearfield{archivePrefix}
\clearfield{arxivId}
\clearfield{pmid}
\clearfield{eprint}
\ifentrytype{online}{}{\ifentrytype{misc}{}{\clearfield{url}}}
\ifentrytype{book}{\clearfield{doi}}{}
}
% Convert months to integers
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=month, match={jan}, replace=${1}]
\step[fieldsource=month, match={feb}, replace=${2}]
\step[fieldsource=month, match={mar}, replace=${3}]
\step[fieldsource=month, match={apr}, replace=${4}]
\step[fieldsource=month, match={may}, replace=${5}]
\step[fieldsource=month, match={jun}, replace=${6}]
\step[fieldsource=month, match={jul}, replace=${7}]
\step[fieldsource=month, match={aug}, replace=${8}]
\step[fieldsource=month, match={sep}, replace=${9}]
\step[fieldsource=month, match={oct}, replace=${10}]
\step[fieldsource=month, match={nov}, replace=${11}]
\step[fieldsource=month, match={dec}, replace=${12}]
}
}
}
%----------------------------------------------------------------------------------------
% BACK REFERENCES
%----------------------------------------------------------------------------------------
% Check if a string is in a comma-separated list
\newcommand\IfStringInList[2]{\IfSubStr{,#2,}{,#1,}}
% Set the language-specific back reference strings
% #LANGUAGE
\@ifpackageloaded{polyglossia}{%
\IfLanguageName{danish}{%
\DefineBibliographyStrings{danish}{%
backrefpage = {citeret på side},
backrefpages = {citeret på sider},
}
}{}
\IfLanguageName{english}{%
\DefineBibliographyStrings{english}{%
backrefpage = {cited on page},
backrefpages = {cited on pages},
}
}{}
\IfLanguageName{italian}{%
\DefineBibliographyStrings{italian}{%
backrefpage = {citato a pag.},
backrefpages = {citato a pagg.},
}
}{}
}{
\@ifpackageloaded{babel}{%
\IfStringInList{danish}{\bbl@loaded}{%
\DefineBibliographyStrings{danish}{%
backrefpage = {citeret på side},
backrefpages = {citeret på sider},
}
}{}
\IfStringInList{english}{\bbl@loaded}{%
\DefineBibliographyStrings{english}{%
backrefpage = {cited on page},
backrefpages = {cited on pages},
}
}{}
\IfStringInList{italian}{\bbl@loaded}{%
\DefineBibliographyStrings{italian}{%
backrefpage = {citato a pag.},
backrefpages = {citato a pagg.},
}
}{}
}{}
}
%----------------------------------------------------------------------------------------
% CITATION COMMANDS
%----------------------------------------------------------------------------------------
% Command to format the marginnote created for cited items
\NewDocumentCommand{\formatmargincitation}{m}{% The parameter is a single citation key
\parencite{#1}: \citeauthor*{#1} (\citeyear{#1}), \citetitle{#1}%
}
% Command to format the marginnote created for supercited items
\NewDocumentCommand{\formatmarginsupercitation}{m}{% The parameter is a single citation key
\supercite{#1} \citeauthor*{#1} (\citeyear{#1})%
}
% The following command needs to be redefined every time \sidecite is called in order for \DeclareCiteCommand's wrapper to work correctly
\NewDocumentCommand{\kaobiblio@marginnote}{m}{%
\marginnote{#1}%
}
% biblatex-like commands that also print a citation in the margin
% Usage:
% First optional argument is always vertical shift and must be given as an (empty) argument when using following a postnote and/or prenote
% Second optional argument is always the postnote if the third argument isn't specified or is the prenote if the third argument is specified (same pattern as the biblatex commands)
% Third optional argument is always the postnote
% Mandatory argument is always the citation key(s)
% Command to \cite and print a citation in the margin
% First optional argument: vertical shift
% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
% Third optional argument: postnote
% Mandatory argument: citation key
\NewDocumentCommand{\sidecite}{o o o m}{%
\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
\marginnote[#1]{##1}%
}%
\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
}{%
\formatmargincitation{\thefield{entrykey}}%
}{%
\\% separator between multiple citations
}{%
}%
% With this we print the marker in the text and add the item to the bibliography at the end
\IfNoValueOrEmptyTF{#2}%
{\def\@tempa{\cite{#4}\kaobiblio@sidecite{#4}}}%
{\IfNoValueOrEmptyTF{#3}%
{\IfNoValueTF{#3}%
{\def\@tempa{\cite[#2]{#4}\kaobiblio@sidecite{#4}}}%
{\def\@tempa{\cite[#2][]{#4}\kaobook@sidecite{#4}}}% postnote is empty, so pass empty postnote
}%
{\def\@tempa{\cite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
}%
\ifkaobiblio@addspace%
\unskip~\@tempa%
\else%
\@tempa%
\fi%
}
% Command to \supercite and print a citation in the margin
% First optional argument: vertical shift
% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
% Third optional argument: postnote
% Mandatory argument: citation key
\NewDocumentCommand{\sidesupercite}{o o o m}{%
\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
\marginnote[#1]{##1}%
}%
\DeclareCiteCommand{\kaobiblio@sidesupercite}[\kaobiblio@marginnote]{%
}{%
\formatmarginsupercitation{\thefield{entrykey}}%
}{%
\\% separator between multiple citations
}{%
}%
% With this we print the marker in the text and add the item to the bibliography at the end
\IfNoValueOrEmptyTF{#2}%
{\def\@tempa{\supercite{#4}\kaobiblio@sidesupercite{#4}}}%
{\IfNoValueOrEmptyTF{#3}%
{\IfNoValueTF{#3}%
{\def\@tempa{\supercite[#2]{#4}\kaobiblio@sidesupercite{#4}}}%
{\def\@tempa{\supercite[#2][]{#4}\kaobook@sidesupercite{#4}}}% postnote is empty, so pass empty postnote
}%
{\def\@tempa{\supercite[#2][#3]{#4}\kaobiblio@sidesupercite{#4}}}%
}%
\@tempa%
}
% Command to \textcite and print a citation in the margin
% First optional argument: vertical shift
% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
% Third optional argument: postnote
% Mandatory argument: citation key
\NewDocumentCommand{\sidetextcite}{o o o m}{%
\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
\marginnote[#1]{##1}%
}%
\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
}{%
\formatmargincitation{\thefield{entrykey}}%
}{%
\\% separator between multiple citations
}{%
}%
% With this we print the marker in the text and add the item to the bibliography at the end
\IfNoValueOrEmptyTF{#2}%
{\def\@tempa{\textcite{#4}\kaobiblio@sidecite{#4}}}%
{\IfNoValueOrEmptyTF{#3}%
{\IfNoValueTF{#3}%
{\def\@tempa{\textcite[#2]{#4}\kaobiblio@sidecite{#4}}}%
{\def\@tempa{\textcite[#2][]{#4}\kaobook@sidecite{#4}}}% postnote is empty, so pass empty postnote
}%
{\def\@tempa{\textcite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
}%
\ifkaobiblio@addspace%
\unskip~\@tempa%
\else%
\@tempa%
\fi%
}
% Command to \parencite or \parencite* and print a citation in the margin
% First optional (star) argument: use \parencite* if included; otherwise use \parencite
% Second optional argument: vertical shift
% Third optional argument: postnote if the fourth argument isn't specified; prenote if the fourth argument is specified (same pattern as the \parencite command)
% Fourth optional argument: postnote
% Mandatory argument: citation key
\NewDocumentCommand{\sideparencite}{s o o o m}{%
\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
\marginnote[#2]{##1}%
}%
\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
}{%
\formatmargincitation{\thefield{entrykey}}%
}{%
\\% separator between multiple citations
}{%
}%
% With this we print the marker in the text and add the item to the bibliography at the end
\IfBooleanTF#1%
{\IfNoValueOrEmptyTF{#3}%
{\parencite*{#5}}%
{\IfNoValueOrEmptyTF{#4}%
{\IfNoValueTF{#4}%
{\def\@tempa{\parencite*[#3]{#5}}}%
{\def\@tempa{\parencite*[#3][]{#5}}}% postnote is empty, so pass empty postnote
}%
{\def\@tempa{\parencite*[#3][#4]{#5}}}%
}%
}%
{\IfNoValueOrEmptyTF{#3}%
{\def\@tempa{\parencite{#5}}}%
{\IfNoValueOrEmptyTF{#4}%
{\IfNoValueTF{#4}%
{\def\@tempa{\parencite[#3]{#5}}}%
{\def\@tempa{\parencite[#3][]{#5}}}% postnote is empty, so pass empty postnote
}%
{\def\@tempa{\parencite[#3][#4]{#5}}}%
}%
}%
\ifkaobiblio@addspace%
\unskip~\@tempa%
\else%
\@tempa%
\fi%
}
%----------------------------------------------------------------------------------------
% LINKING THE AUTHOR'S NAME
%----------------------------------------------------------------------------------------
% In biblatex, when citing with the style authoryear or using \textcite, only the year is linked to the reference in the bibliography. Despite the arguments of one of the mantainers of the biblatex package (https://github.com/plk/biblatex/issues/428), some users think that in the author* style the author name should be a link as well. The `linkname' option provides an easy way to activate this behaviour.
\ifkaobiblio@linkeverything
\xpatchbibmacro{cite}
{\usebibmacro{cite:label}%
\setunit{\printdelim{nonameyeardelim}}%
\usebibmacro{cite:labeldate+extradate}}
{\printtext[bibhyperref]{%
\DeclareFieldAlias{bibhyperref}{default}%
\usebibmacro{cite:label}%
\setunit{\printdelim{nonameyeardelim}}%
\usebibmacro{cite:labeldate+extradate}}}
{}
{\PackageWarning{biblatex-patch}
{Failed to patch cite bibmacro}}
% Include labelname in labelyear link
\xpatchbibmacro{cite}
{\printnames{labelname}%
\setunit{\printdelim{nameyeardelim}}%
\usebibmacro{cite:labeldate+extradate}}
{\printtext[bibhyperref]{%
\DeclareFieldAlias{bibhyperref}{default}%
\printnames{labelname}%
\setunit{\printdelim{nameyeardelim}}%
\usebibmacro{cite:labeldate+extradate}}}
{}
{\PackageWarning{biblatex-patch}
{Failed to patch cite bibmacro}}
% Access hyperref's citation link start/end commands
\makeatletter
\protected\def\blx@imc@biblinkstart{%
\@ifnextchar[%]
{\blx@biblinkstart}
{\blx@biblinkstart[\abx@field@entrykey]}}
\def\blx@biblinkstart[#1]{%
\blx@sfsave\hyper@natlinkstart{\the\c@refsection @#1}\blx@sfrest}
\protected\def\blx@imc@biblinkend{%
\blx@sfsave\hyper@natlinkend\blx@sfrest}
\blx@regimcs{\biblinkstart \biblinkend}
\makeatother
\newbool{cbx:link}
% Include parentheses around labelyear in \textcite only in
% single citations without pre- and postnotes
\def\iflinkparens{%
\ifboolexpr{ test {\ifnumequal{\value{multicitetotal}}{0}} and
test {\ifnumequal{\value{citetotal}}{1}} and
test {\iffieldundef{prenote}} and
test {\iffieldundef{postnote}} }}
\xpatchbibmacro{textcite}
{\printnames{labelname}}
{\iflinkparens
{\DeclareFieldAlias{bibhyperref}{default}%
\global\booltrue{cbx:link}\biblinkstart%
\printnames{labelname}}
{\printtext[bibhyperref]{\printnames{labelname}}}}
{}
{\PackageWarning{biblatex-patch}
{Failed to patch textcite bibmacro}}
\xpatchbibmacro{textcite}
{\usebibmacro{cite:label}}
{\iflinkparens
{\DeclareFieldAlias{bibhyperref}{default}%
\global\booltrue{cbx:link}\biblinkstart%
\usebibmacro{cite:label}}
{\usebibmacro{cite:label}}}
{}
{\PackageWarning{biblatex-patch}
{Failed to patch textcite bibmacro}}
\xpretobibmacro{textcite:postnote}
{\ifbool{cbx:link}
{\ifbool{cbx:parens}
{\bibcloseparen\global\boolfalse{cbx:parens}}
{}%
\biblinkend\global\boolfalse{cbx:link}}
{}}
{}
{\PackageWarning{biblatex-patch}
{Failed to patch textcite:postnote bibmacro}}
\else
\fi
%----------------------------------------------------------------------------------------
% CITATION ENVIRONMENTS
%----------------------------------------------------------------------------------------
% TODO: create a fancy environment for this. Perhaps printing also the
% abstract.
% Cite commands (assuming biblatex is loaded)
\DeclareCiteCommand{\fullcite}{%
\defcounter{maxnames}{99}%
\usebibmacro{prenote}}
{\clearfield{url}%
\clearfield{pages}%
\clearfield{pagetotal}%
\clearfield{edition}%
\clearfield{issn}%
\clearfield{doi}%
\usedriver
{\DeclareNameAlias{sortname}{default}}
{\thefield{entrytype}}
}
{\multicitedelim}
{\usebibmacro{postnote}}