masters-thesis

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

kaobiblio.sty (14724B)


      1 \ProvidesPackage{kaobiblio}
      2 
      3 %----------------------------------------------------------------------------------------
      4 %	PACKAGE OPTIONS AND DEPENDENCIES
      5 %----------------------------------------------------------------------------------------
      6 
      7 \RequirePackage{etoolbox} % Easy programming to modify TeX stuff
      8 \RequirePackage{perpage} % Reset counters
      9 \RequirePackage{iflang} % Check the document language
     10 \RequirePackage{xparse} % Parse arguments for macros
     11 \RequirePackage{xstring} % Parse strings
     12 \RequirePackage{hyperref} % Required for hyperlinks
     13 \RequirePackage{kvoptions} % Handle package options
     14 
     15 % Set up the package options
     16 \SetupKeyvalOptions{
     17 	family = kaobiblio,
     18 	prefix = kaobiblio@
     19 }
     20 
     21 \DeclareBoolOption{addspace}% If true, automatically add a space before printing the citation marker
     22 \DeclareBoolOption{linkeverything}% If true, the author name is a hyperlink in citation styles that support it
     23 
     24 % Choose the default options, which will be overwritten by the options 
     25 % passed to this package.
     26 \PassOptionsToPackage{
     27 	%style=numeric-comp,
     28 	%citestyle=authortitle-icomp,
     29 	citestyle=numeric-comp,
     30 	%bibstyle=authoryear,
     31 	bibstyle=numeric,
     32 	sorting=none,
     33 	%sorting=nyt,
     34 	%sortcites=true,
     35 	%autocite=footnote,
     36 	backend=biber, % Compile the bibliography with biber
     37 	hyperref=true,
     38 	backref=true,
     39 	citecounter=true,
     40 	pagetracker=true,
     41 	citetracker=true,
     42 	ibidtracker=context,
     43 	autopunct=true,
     44 	autocite=plain,
     45 }{biblatex}
     46 
     47 % Pass the unknown options to biblatex, overwriting the previous settings. Avoid passing the kao-specific options.
     48 \DeclareDefaultOption{%
     49 	\IfBeginWith{\CurrentOption}{addspace}{}{%
     50 	\IfBeginWith{\CurrentOption}{linkeverything}{}{%
     51 		\PassOptionsToPackage{\CurrentOption}{biblatex}%
     52 	}}%
     53 }
     54 
     55 % Process the options
     56 \ProcessKeyvalOptions{kaobiblio}
     57 
     58 % Load biblatex
     59 \RequirePackage{biblatex}
     60 
     61 % Remove some unwanted entries from the bibliography
     62 \AtEveryBibitem{
     63 	\clearfield{issn}
     64 	\clearfield{isbn}
     65 	\clearfield{archivePrefix}
     66 	\clearfield{arxivId}
     67 	\clearfield{pmid}
     68 	\clearfield{eprint}
     69 	\ifentrytype{online}{}{\ifentrytype{misc}{}{\clearfield{url}}}
     70 	\ifentrytype{book}{\clearfield{doi}}{}
     71 }
     72 
     73 % Convert months to integers
     74 \DeclareSourcemap{
     75 	\maps[datatype=bibtex]{
     76 		\map[overwrite]{
     77 			\step[fieldsource=month, match={jan}, replace=${1}]
     78 			\step[fieldsource=month, match={feb}, replace=${2}]
     79 			\step[fieldsource=month, match={mar}, replace=${3}]
     80 			\step[fieldsource=month, match={apr}, replace=${4}]
     81 			\step[fieldsource=month, match={may}, replace=${5}]
     82 			\step[fieldsource=month, match={jun}, replace=${6}]
     83 			\step[fieldsource=month, match={jul}, replace=${7}]
     84 			\step[fieldsource=month, match={aug}, replace=${8}]
     85 			\step[fieldsource=month, match={sep}, replace=${9}]
     86 			\step[fieldsource=month, match={oct}, replace=${10}]
     87 			\step[fieldsource=month, match={nov}, replace=${11}]
     88 			\step[fieldsource=month, match={dec}, replace=${12}]
     89 		}
     90 	}
     91 }
     92 
     93 %----------------------------------------------------------------------------------------
     94 %	BACK REFERENCES
     95 %----------------------------------------------------------------------------------------
     96 
     97 % Check if a string is in a comma-separated list
     98 \newcommand\IfStringInList[2]{\IfSubStr{,#2,}{,#1,}}
     99 
    100 % Set the language-specific back reference strings
    101 % #LANGUAGE
    102 \@ifpackageloaded{polyglossia}{%
    103 	\IfLanguageName{danish}{%
    104 		\DefineBibliographyStrings{danish}{%
    105 			backrefpage = {citeret på side},
    106 			backrefpages = {citeret på sider},
    107 		}
    108 	}{}
    109 	\IfLanguageName{english}{%
    110 		\DefineBibliographyStrings{english}{%
    111 			backrefpage = {cited on page},
    112 			backrefpages = {cited on pages},
    113 		}
    114 	}{}
    115 	\IfLanguageName{italian}{%
    116 		\DefineBibliographyStrings{italian}{%
    117 			backrefpage = {citato a pag.},
    118 			backrefpages = {citato a pagg.},
    119 		}
    120 	}{}
    121 }{
    122 	\@ifpackageloaded{babel}{%
    123 		\IfStringInList{danish}{\bbl@loaded}{%
    124 			\DefineBibliographyStrings{danish}{%
    125 				backrefpage = {citeret på side},
    126 				backrefpages = {citeret på sider},
    127 			}
    128 		}{}
    129 		\IfStringInList{english}{\bbl@loaded}{%
    130 			\DefineBibliographyStrings{english}{%
    131 				backrefpage = {cited on page},
    132 				backrefpages = {cited on pages},
    133 			}
    134 		}{}
    135 		\IfStringInList{italian}{\bbl@loaded}{%
    136 			\DefineBibliographyStrings{italian}{%
    137 				backrefpage = {citato a pag.},
    138 				backrefpages = {citato a pagg.},
    139 			}
    140 		}{}
    141 	}{}
    142 }
    143 
    144 %----------------------------------------------------------------------------------------
    145 %	CITATION COMMANDS
    146 %----------------------------------------------------------------------------------------
    147 
    148 % Command to format the marginnote created for cited items
    149 \NewDocumentCommand{\formatmargincitation}{m}{% The parameter is a single citation key
    150 	\parencite{#1}: \citeauthor*{#1} (\iffieldundef{year}{\bibsstring{nodate}}{\printfield{year}}), \citefield{#1}[emph]{title}%
    151 }
    152 
    153 % Command to format the marginnote created for supercited items
    154 \NewDocumentCommand{\formatmarginsupercitation}{m}{% The parameter is a single citation key
    155 	\supercite{#1} \citeauthor*{#1} (\iffieldundef{year}{\bibsstring{nodate}}{\printfield{year}})%
    156 }
    157 
    158 % The following command needs to be redefined every time \sidecite is called in order for \DeclareCiteCommand's wrapper to work correctly
    159 \NewDocumentCommand{\kaobiblio@marginnote}{m}{%
    160 	\marginnote{#1}%
    161 }
    162 
    163 % biblatex-like commands that also print a citation in the margin
    164 % Usage:
    165 	% First optional argument is always vertical shift and must be given as an (empty) argument when using following a postnote and/or prenote
    166 	% 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)
    167 	% Third optional argument is always the postnote
    168 	% Mandatory argument is always the citation key(s)
    169 
    170 % Command to \cite and print a citation in the margin
    171 % First optional argument: vertical shift
    172 % Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
    173 % Third optional argument: postnote
    174 % Mandatory argument: citation key
    175 \NewDocumentCommand{\sidecite}{o o o m}{%
    176 	\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
    177 		\marginnote[#1]{##1}%
    178 	}%
    179 	\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
    180 	}{%
    181 		\formatmargincitation{\thefield{entrykey}}%
    182 	}{%
    183 		\\% separator between multiple citations
    184 	}{%
    185 	}%
    186 	% With this we print the marker in the text and add the item to the bibliography at the end
    187 	\IfNoValueOrEmptyTF{#2}%
    188 	{\def\@tempa{\cite{#4}\kaobiblio@sidecite{#4}}}%
    189 	{\IfNoValueOrEmptyTF{#3}%
    190 		{\IfNoValueTF{#3}%
    191 			{\def\@tempa{\cite[#2]{#4}\kaobiblio@sidecite{#4}}}%
    192 			{\def\@tempa{\cite[#2][]{#4}\kaobiblio@sidecite{#4}}}% postnote is empty, so pass empty postnote
    193 		}%
    194 		{\def\@tempa{\cite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
    195 	}%
    196 	\ifkaobiblio@addspace%
    197 		\unskip~\@tempa%
    198 	\else%
    199 		\@tempa%
    200 	\fi%
    201 }
    202 
    203 % Command to \supercite and print a citation in the margin
    204 % First optional argument: vertical shift
    205 % Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
    206 % Third optional argument: postnote
    207 % Mandatory argument: citation key
    208 \NewDocumentCommand{\sidesupercite}{o o o m}{%
    209 	\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
    210 		\marginnote[#1]{##1}%
    211 	}%
    212 	\DeclareCiteCommand{\kaobiblio@sidesupercite}[\kaobiblio@marginnote]{%
    213 	}{%
    214 		\formatmarginsupercitation{\thefield{entrykey}}%
    215 	}{%
    216 		\\% separator between multiple citations
    217 	}{%
    218 	}%
    219 	% With this we print the marker in the text and add the item to the bibliography at the end
    220 	\IfNoValueOrEmptyTF{#2}%
    221 	{\def\@tempa{\supercite{#4}\kaobiblio@sidesupercite{#4}}}%
    222 	{\IfNoValueOrEmptyTF{#3}%
    223 		{\IfNoValueTF{#3}%
    224 			{\def\@tempa{\supercite[#2]{#4}\kaobiblio@sidesupercite{#4}}}%
    225 			{\def\@tempa{\supercite[#2][]{#4}\kaobook@sidesupercite{#4}}}% postnote is empty, so pass empty postnote
    226 		}%
    227 		{\def\@tempa{\supercite[#2][#3]{#4}\kaobiblio@sidesupercite{#4}}}%
    228 	}%
    229 	\@tempa%
    230 }
    231 
    232 % Command to \textcite and print a citation in the margin
    233 % First optional argument: vertical shift
    234 % Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
    235 % Third optional argument: postnote
    236 % Mandatory argument: citation key
    237 \NewDocumentCommand{\sidetextcite}{o o o m}{%
    238 	\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
    239 		\marginnote[#1]{##1}%
    240 	}%
    241 	\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
    242 	}{%
    243 		\formatmargincitation{\thefield{entrykey}}%
    244 	}{%
    245 		\\% separator between multiple citations
    246 	}{%
    247 	}%
    248 	% With this we print the marker in the text and add the item to the bibliography at the end
    249 	\IfNoValueOrEmptyTF{#2}%
    250 		{\def\@tempa{\textcite{#4}\kaobiblio@sidecite{#4}}}%
    251 		{\IfNoValueOrEmptyTF{#3}%
    252 			{\IfNoValueTF{#3}%
    253 				{\def\@tempa{\textcite[#2]{#4}\kaobiblio@sidecite{#4}}}%
    254 				{\def\@tempa{\textcite[#2][]{#4}\kaobook@sidecite{#4}}}% postnote is empty, so pass empty postnote
    255 			}%
    256 			{\def\@tempa{\textcite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
    257 		}%
    258 	\ifkaobiblio@addspace%
    259 		\unskip~\@tempa%
    260 	\else%
    261 		\@tempa%
    262 	\fi%
    263 }
    264 
    265 % Command to \parencite or \parencite* and print a citation in the margin
    266 % First optional (star) argument: use \parencite* if included; otherwise use \parencite
    267 % Second optional argument: vertical shift
    268 % Third optional argument: postnote if the fourth argument isn't specified; prenote if the fourth argument is specified (same pattern as the \parencite command)
    269 % Fourth optional argument: postnote
    270 % Mandatory argument: citation key
    271 \NewDocumentCommand{\sideparencite}{s o o o m}{%
    272 	\RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
    273 		\marginnote[#2]{##1}%
    274 	}%
    275 	\DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
    276 	}{%
    277 		\formatmargincitation{\thefield{entrykey}}%
    278 	}{%
    279 		\\% separator between multiple citations
    280 	}{%
    281 	}%
    282 	% With this we print the marker in the text and add the item to the bibliography at the end
    283 	\IfBooleanTF#1%
    284 		{\IfNoValueOrEmptyTF{#3}%
    285 			{\parencite*{#5}\kaobiblio@sidecite{#5}}%
    286 			{\IfNoValueOrEmptyTF{#4}%
    287 				{\IfNoValueTF{#4}%
    288 					{\def\@tempa{\parencite*[#3]{#5}\kaobiblio@sidecite{#5}}}%
    289 					{\def\@tempa{\parencite*[#3][]{#5}\kaobiblio@sidecite{#5}}}% postnote is empty, so pass empty postnote
    290 				}%
    291 				{\def\@tempa{\parencite*[#3][#4]{#5}\kaobiblio@sidecite{#5}}}%
    292 			}%
    293 		}%
    294 		{\IfNoValueOrEmptyTF{#3}%
    295 			{\def\@tempa{\parencite{#5}\kaobiblio@sidecite{#5}}}%
    296 			{\IfNoValueOrEmptyTF{#4}%
    297 				{\IfNoValueTF{#4}%
    298 					{\def\@tempa{\parencite[#3]{#5}\kaobiblio@sidecite{#5}}}%
    299 					{\def\@tempa{\parencite[#3][]{#5}\kaobiblio@sidecite{#5}}}% postnote is empty, so pass empty postnote
    300 				}%
    301 				{\def\@tempa{\parencite[#3][#4]{#5}\kaobiblio@sidecite{#5}}}%
    302 			}%
    303 		}%
    304 	\ifkaobiblio@addspace%
    305 		\unskip~\@tempa%
    306 	\else%
    307 		\@tempa%
    308 	\fi%
    309 }
    310 
    311 
    312 %----------------------------------------------------------------------------------------
    313 %	LINKING THE AUTHOR'S NAME
    314 %----------------------------------------------------------------------------------------
    315 
    316 % 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 `linkeverything' option provides an easy way to activate this behaviour.
    317 
    318 \ifkaobiblio@linkeverything
    319 	\xpatchbibmacro{cite}
    320 		{\usebibmacro{cite:label}%
    321 		 \setunit{\printdelim{nonameyeardelim}}%
    322 		 \usebibmacro{cite:labeldate+extradate}}
    323 		{\printtext[bibhyperref]{%
    324 			 \DeclareFieldAlias{bibhyperref}{default}%
    325 			 \usebibmacro{cite:label}%
    326 			 \setunit{\printdelim{nonameyeardelim}}%
    327 			 \usebibmacro{cite:labeldate+extradate}}}
    328 		{}
    329 		{\PackageWarning{biblatex-patch}
    330 			 {Failed to patch cite bibmacro}}
    331 
    332 	% Include labelname in labelyear link
    333 	\xpatchbibmacro{cite}
    334 		{\printnames{labelname}%
    335 		 \setunit{\printdelim{nameyeardelim}}%
    336 		 \usebibmacro{cite:labeldate+extradate}}
    337 		{\printtext[bibhyperref]{%
    338 			 \DeclareFieldAlias{bibhyperref}{default}%
    339 			 \printnames{labelname}%
    340 			 \setunit{\printdelim{nameyeardelim}}%
    341 			 \usebibmacro{cite:labeldate+extradate}}}
    342 		{}
    343 		{\PackageWarning{biblatex-patch}
    344 			 {Failed to patch cite bibmacro}}
    345 
    346 	% Access hyperref's citation link start/end commands
    347 	\makeatletter
    348 	\protected\def\blx@imc@biblinkstart{%
    349 		\@ifnextchar[%]
    350 			{\blx@biblinkstart}
    351 			{\blx@biblinkstart[\abx@field@entrykey]}}
    352 	\def\blx@biblinkstart[#1]{%
    353 		\blx@sfsave\hyper@natlinkstart{\the\c@refsection @#1}\blx@sfrest}
    354 	\protected\def\blx@imc@biblinkend{%
    355 		\blx@sfsave\hyper@natlinkend\blx@sfrest}
    356 	\blx@regimcs{\biblinkstart \biblinkend}
    357 	\makeatother
    358 
    359 	\newbool{cbx:link}
    360 
    361 	% Include parentheses around labelyear in \textcite only in
    362 	% single citations without pre- and postnotes
    363 	\def\iflinkparens{%
    364 		\ifboolexpr{ test {\ifnumequal{\value{multicitetotal}}{0}} and
    365 					 test {\ifnumequal{\value{citetotal}}{1}} and
    366 					 test {\iffieldundef{prenote}} and
    367 					 test {\iffieldundef{postnote}} }}
    368 
    369 	\xpatchbibmacro{textcite}
    370 		{\printnames{labelname}}
    371 		{\iflinkparens
    372 			 {\DeclareFieldAlias{bibhyperref}{default}%
    373 				\global\booltrue{cbx:link}\biblinkstart%
    374 				\printnames{labelname}}
    375 			 {\printtext[bibhyperref]{\printnames{labelname}}}}
    376 		{}
    377 		{\PackageWarning{biblatex-patch}
    378 			 {Failed to patch textcite bibmacro}}
    379 
    380 	\xpatchbibmacro{textcite}
    381 		{\usebibmacro{cite:label}}
    382 		{\iflinkparens
    383 			 {\DeclareFieldAlias{bibhyperref}{default}%
    384 				\global\booltrue{cbx:link}\biblinkstart%
    385 				\usebibmacro{cite:label}}
    386 			 {\usebibmacro{cite:label}}}
    387 		{}
    388 		{\PackageWarning{biblatex-patch}
    389 			 {Failed to patch textcite bibmacro}}
    390 
    391 	\xpretobibmacro{textcite:postnote}
    392 		{\ifbool{cbx:link}
    393 			 {\ifbool{cbx:parens}
    394 					{\bibcloseparen\global\boolfalse{cbx:parens}}
    395 					{}%
    396 				\biblinkend\global\boolfalse{cbx:link}}
    397 			 {}}
    398 		{}
    399 		{\PackageWarning{biblatex-patch}
    400 			 {Failed to patch textcite:postnote bibmacro}}
    401 \else
    402 \fi
    403 
    404 %----------------------------------------------------------------------------------------
    405 %	CITATION ENVIRONMENTS
    406 %----------------------------------------------------------------------------------------
    407 
    408 % TODO: create a fancy environment for this. Perhaps printing also the 
    409 % abstract.
    410 
    411 % Cite commands (assuming biblatex is loaded)
    412 \DeclareCiteCommand{\fullcite}{%
    413 	\defcounter{maxnames}{99}%
    414 	\usebibmacro{prenote}}
    415 	{\clearfield{url}%
    416 	\clearfield{pages}%
    417 	\clearfield{pagetotal}%
    418 	\clearfield{edition}%
    419 	\clearfield{issn}%
    420 	\clearfield{doi}%
    421 	\usedriver
    422 	{\DeclareNameAlias{sortname}{default}}
    423 	{\thefield{entrytype}}
    424 }
    425 {\multicitedelim}
    426 {\usebibmacro{postnote}}