Skip to content

Instantly share code, notes, and snippets.

@Charl-AI
Created February 16, 2025 17:26
An actually decent referencing style with biblatex
% An actually decent referencing style. C. Jones 2025.
\PassOptionsToPackage{
style=authoryear, % Harvard/APA ish
backend=bibtex,
natbib=true, % use \citep, \citet
backref=true, % include links from bibliography back to text
maxbibnames=99, maxcitenames=2, % show all names in bibliography, but up to 2 in-text
uniquelist=false, uniquename=false, % don't try to disambiguate name clashes in-text
date=year, % don't include months in bibliography
dashed=false, giveninits=true,
sorting=nyt, sortcites=false,
isbn=false,doi=false,url=false,eprint=false, % don't include links (we will hyperlink the titles instead)
}{biblatex}
\RequirePackage{biblatex}
\DeclareNameAlias{sortname}{family-given} % Lastname, Firstname in reference list
% Simplify references by removing superfluous fields.
% remove publisher for everything other than books.
\AtEveryBibitem{
\clearlist{address}
\clearfield{date}
\clearfield{eprint}
\clearfield{isbn}
\clearfield{issn}
\clearlist{location}
\clearfield{month}
\clearfield{series}
\clearname{editor}
\ifentrytype{book}{}{
\clearlist{publisher}
}
}
% use '&' instead of 'and' for consistency with natbib
\renewcommand*{\finalnamedelim}{%
\ifnumgreater{\value{liststop}}{2}{\finalandcomma}{}%
\addspace\&\space}%
% backref format
\DefineBibliographyStrings{english}{backrefpage={cited on p.},backrefpages={cited on pp.}}
% \renewcommand*{\bibfont}{\normalfont\small} % make bibliography use \small text
\setlength\bibitemsep{0.49\baselineskip} % increase spacing between items in bibliography
% --- Make title a hyperlink to the DOI (falls back to URL)
\newbibmacro{string+doiurl}[1]{%
\iffieldundef{doi}{%
\iffieldundef{url}{%
\PackageWarning{mybibliography}{Entry \thefield{entrykey} has no DOI or URL}%
#1%
}{%
\href{\thefield{url}}{#1}%
}%
}{%
\href{http://dx.doi.org/\thefield{doi}}{#1}%
}%
}
\DeclareFieldFormat{title}{\usebibmacro{string+doiurl}{\mkbibemph{#1}}}
\DeclareFieldFormat[article,incollection,misc,inproceedings,book]{title}%
{\usebibmacro{string+doiurl}{\mkbibquote{#1}}}
% ---
% --- Make biblatex hyperlinks cover the name and year instead of just year (for consistency with natbib)
\DeclareFieldFormat{citehyperref}{%
\DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
\bibhyperref{#1}}
\DeclareFieldFormat{textcitehyperref}{%
\DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
\bibhyperref{%
#1%
\ifbool{cbx:parens}
{\bibcloseparen\global\boolfalse{cbx:parens}}
{}}}
\savebibmacro{cite}
\savebibmacro{textcite}
\renewbibmacro*{cite}{%
\printtext[citehyperref]{%
\restorebibmacro{cite}%
\usebibmacro{cite}}}
\renewbibmacro*{textcite}{%
\ifboolexpr{
( not test {\iffieldundef{prenote}} and
test {\ifnumequal{\value{citecount}}{1}} )
or
( not test {\iffieldundef{postnote}} and
test {\ifnumequal{\value{citecount}}{\value{citetotal}}} )
}
{\DeclareFieldAlias{textcitehyperref}{noformat}}
{}%
\printtext[textcitehyperref]{%
\restorebibmacro{textcite}%
\usebibmacro{textcite}}}
% ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment