# Custom bibliography style for @reference and @inreference with custom sorting

The thesis I am currently supporting has quite a large bibliography, so naturally there is some grouping going on, mostly based on keywords (\printbibliography[heading=sec,notkeyword=prim,notkeyword=med,notkeyword=hs,notkeyword=lex,notkeyword=onl] for example)

One part of the bibliography shall be dedicated to various encyclopedic works. The obvious bib-type here is @reference and @inreference

Some sources refer to just one article inside one encyclopedia which won't be cited anywhere else, for these I chose to use the type @inreference, giving all the details for the whole encyclopedia. Other sources are multiple entries for one encyclopedia, so I used @reference for the whole work and @inreference for each entry, crossref-ing to the whole work to get a proper connection. As seen in other, printed, bibliographies, the aim is to first have a list of all works which are cited more than once, giving an abbreviation for that work. Then follows a list of all cited entries, abbreviating the main work (crossreffed) for less clutter and redundancy.

Please consider the following (hopefully) MWE:

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{filecontents}
\usepackage[style=authoryear,backend=bibtex8,sorting=nyt,mincrossrefs=1]{biblatex}

\begin{filecontents}{Bibliography.bib}
@inreference{fantametz,
crossref = {metzler},
author = {Antonsen, Jan Erik},
title = {Phantastische Literatur},
pages = {581\psq},
keywords = {lex}
}

@inreference{metametz,
crossref = {metzler},
author = {Sp{\"o}rl, Uwe},
title = {Metafiktion},
pages = {493\psq},
keywords = {lex}
}

@reference{metzler,
editor = {Burdorf, Dieter and Fasbender, Christoph and Moennighoff, Burkhard},
year = {2007},
title = {Metzler Lexikon Literatur},
subtitle = {Begriffe und Definitionen},
titleaddon = {Begr{\"u}ndet v. G{\"u}nther u.  Irmgard Schweikle},
shorttitle = {MLL},
shorthand = {Metzler 2007},
edition = {3., v{\"o}llig neu bearb. Aufl.},
publisher = {Metzler},
location = {Stuttgart},
keywords = {lex}
}

@inreference{fidakult,
crossref = {metzlerK},
author = {Nieragden, G{\"o}ran},
title = {Figurendarstelling, literarische},
pages = {200},
keywords = {lex}
}

@inreference{metakult,
crossref = {metzlerK},
author = {Wolf, Werner},
title = {Metafiktion},
pages = {487\psqq},
keywords = {lex}
}

@reference{metzlerK,
editor = {N{\"u}nning, Ansgar},
title = {Metzler Lexikon Literatur- und Kulturtheorie},
subtitle = {Ans{\"a}tze -- Personen -- Grundbegriffe},
shorttitle = {MLLK},
year = {2008},
edition = {4., aktual. u. erw. Aufl.},
publisher = {Metzler},
location = {Stuttgart},
keywords = {lex}
}

@inreference{hwphwelt,
author = {Dirks, Ulrich},
title = {Welt},
pages = {407--443},
number = {Bd. 12: W-Z},
keywords = {lex},
publisher = {{Schwabe Verlag}},
editor = {Ritter, Joachim and Gr{\"u}nder, Karlfried and Gabriel, Gottfried},
booktitle = {Historisches W{\"o}rterbuch der Philosophie},
year = {2004},
location = {Basel},
booktitleaddon = {V{\"o}llig neubearbeitete Ausgabe des
\textit{W{\"o}rterbuchs der philosophischen Begriffe} von Rudolf Eisler.},
volumes = {13}
}

@inreference{hpgwelt,
author = {Schmitz, Hermann},
title = {Welt},
pages = {2466--2484},
number = {Bd. 3 (Quantität -- Zweifel)},
keywords = {lex},
publisher = {{Karl Alber}},
editor = {Kolmer, Petra and Wildfeuer, Armin G.},
booktitle = {Neues Handbuch philosophischer Grundbegriffe},
year = {2011},
location = {Freiburg and M{\"u}nchen},
booktitleaddon = {Begr{\"u}ndet von Hermann Krings, Hans Michael Baumgartner und Christoph Wild.},
volumes = {3}
}

@reference{routledge,
year = {2008},
title = {Routledge Encyclopedia of Narrative Theory},
shorttitle = {RENT},
publisher = {Routledge},
keywords = {lex},
editor = {Herman, David and Jahn, Manfred and Ryan, Marie-Laure},
location = {London and New York}
}

@inreference{stgel,
crossref = {routledge},
author = {Saint-Gelais, Richard},
title = {Transfictionality},
pages = {612--613},
keywords = {lex}
}

@inreference{poss,
crossref = {routledge},
author = {Ryan, Marie-Laure},
title = {Possible-Worlds Theory},
pages = {446--450},
keywords = {lex}
}
\end{filecontents}

\input{Bibdriver} %Custom drivers

\begin{document}

This is a sample text much like LoremIpsum, but different. Cite1:\cite{hpgwelt}, Cite2:\cite{hwphwelt}, Cite3:\cite{metakult}, Cite4:\cite{fidakult}, Cite5:\cite{metametz}, Cite6:\cite{fantametz}, Cite7:\cite{stgel}, Cite8:\cite{poss}\\

I use nocite to have one citytion for every reference:\nocite{metzlerK}\nocite{metzler}\nocite{routledge}

\end{document}


Together with my custom Bibdriver.tex:

\DefineBibliographyStrings{ngerman}{page={{}{}},pages={{}{}}} %Keine Seitenangabe
%\DefineBibliographyStrings{ngerman}{citedas={im Folgenden im Text zit\adddotspace als}}

\DeclareBibliographyDriver{reference}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\iffieldundef{shorttitle}
{}%
{\printfield{shorttitle} =}%
\iffieldundef{editor}
{}{
\usebibmacro{editor+others}%
\setunit{\labelnamepunct}\newblock
}
\usebibmacro{maintitle+title}%
\newunit
\printlist{language}%
\newunit\newblock
\usebibmacro{byeditor+others}%
\newunit\newblock
\printfield{edition}%
\newunit
\iffieldundef{maintitle}
{\printfield{volume}%
\printfield{part}}
{}%
\newunit
\printfield{volumes}%
\newunit\newblock
\usebibmacro{series+number}%
\newunit\newblock
\printfield{note}%
\newunit\newblock
\usebibmacro{publisher+location+date}%
\newunit\newblock
\usebibmacro{chapter+pages}%
\newunit
\printfield{pagetotal}%
\newunit\newblock
\iftoggle{bbx:isbn}
{\printfield{isbn}}
{}%
\newunit\newblock
\usebibmacro{doi+eprint+url}%
\newunit\newblock
\setunit{\bibpagerefpunct}\newblock
\usebibmacro{pageref}%
\newunit\newblock
\iftoggle{bbx:related}
{\usebibmacro{related:init}%
\usebibmacro{related}}
{}%
\usebibmacro{finentry}}

\DeclareBibliographyDriver{inreference}{%
\iffieldundef{crossref}%
{\usebibmacro{crossref:full}}%
{\usebibmacro{crossref:label}}%
}%

\newbibmacro{crossref:full}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author/translator+others}%
\setunit{\labelnamepunct}\newblock
\usebibmacro{title}%
\newunit
\printlist{language}%
\newunit\newblock
\usebibmacro{byauthor}%
\usebibmacro{bytranslator}% Übersetzer anzeigen
\newunit\newblock
\usebibmacro{in:}%
\usebibmacro{maintitle+booktitle}%
\newunit\newblock
%  \usebibmacro{byeditor+others}% Hier keinen Übersetzer anzeigen
\usebibmacro{byeditor}%
\newunit\newblock
\printfield{edition}%
\newunit
\iffieldundef{maintitle}
{\printfield{volume}%
\printfield{part}}
{}%
\newunit
\printfield{volumes}%
\newunit\newblock
\usebibmacro{series+number}%
\newunit\newblock
\printfield{note}%
\newunit\newblock
\usebibmacro{publisher+location+date}%
\newunit\newblock
\usebibmacro{chapter+pages}%
\newunit\newblock
\iftoggle{bbx:isbn}
{\printfield{isbn}}
{}%
\newunit\newblock
\usebibmacro{doi+eprint+url}%
\newunit\newblock
\setunit{\bibpagerefpunct}\newblock
%  \usebibmacro{pageref}% fügt bei erstem, vollem Zitat ein Leerfeld zwischen Gesamtseitenzahlen und Komma vor zitierter Seite ein?
\usebibmacro{finentry}% Punkt am Ende des Eintrags
}

\newbibmacro{crossref:label}{%
\usebibmacro{bibindex}%
\usebibmacro{begentry}%
\usebibmacro{author}%/translator+others}%
\setunit{\labelnamepunct}\newblock
\printfield{title}%
\newunit\newblock
\usebibmacro{in:}%
\printfield{shorttitle}%
\newunit\newblock
\usebibmacro{series+number},%
\newunit\newblock
\usebibmacro{doi+eprint+url}%
\newunit\newblock
\usebibmacro{chapter+pages}%
\usebibmacro{finentry}% Punkt am Ende des Eintrags
}


The output is the following:

There are a two things wrong, which I tried to fix but failed:

• The most obvious one is the sorting order of the cited encyclopedias (the whole works I mean). I would like to sort them by their shorttitle filed, but using:

\DeclareSortingTemplate{references}{
\sort{
\field{sorttitle}
\field{title}
}
\sort{
\field[strside=left,strwidth=4]{sortyear}
\field[strside=left,strwidth=4]{year}
\literal{9999}
}
\sort{
\field{sortname}
\field{author}
\field{editor}
}
}


and forcing \newrefcontext[sorting=references] before the first \printbibliography did not work. No error, the whole bibliography just did not get printed.

• The shorthand defined for the metzler-reference gets used for every @incollection that crossref's this @reference. Using \DeclareDataInheritance{reference}{inreference}{\noinherit{shorthand}} does not throw any errors, but the result stays the same.

The reason I want to order the whole works by short title should be apparent: The layout suggests an alphabetically sorted list, sorted by the shorttitle. The reason for not using shorthand is along the same line: I could probably tweak the output for a list of shorthands thus, that it displayed the whole bibliography entry and sorted everything by the shorthand key, but every single reference to individual articles would end up being designated as that works shorthand, which is no viable alternative.

So TL;DR: Why won't it work? I am sure that can be acheived, but it seems I'm missing the obvious.

• Re issue 2: Data inheritance only works with Biber. You are using BibTeX8, so \DeclareDataInheritance and friends don't do anything. If I compile the document with backend=biber and Biber I get no troubles with the @shorthands. Note that this also explains issue 1: \DeclareSortingTemplate can only be used with Biber as well (though I'm not entirely sure if this would do what you want even with Biber). – moewe Sep 2 '18 at 7:13
• Never mind me writing sorttitle instead of shorttitle, switching to backend=biber did the trick. Suppressing shorthand works now as well. If you make an answer out of your comment, I'll have something to accept. – lhiapgpeonk Sep 2 '18 at 12:44

\DeclareSortingTemplate and \DeclareDataInheritance are Biber only features and can not be used if you only run BibTeX8. They are just two features referred to by the warning

Package biblatex Warning: Using fall-back BibTeX(8) backend:
(biblatex)                functionality may be reduced/unavailable.


that you get when you use backend=bibtex or backend=bibtex8.

I'd also suggest using

\begin{refcontext}[sorting=references]

instead of \newrefcontext. It is safer because the refcontext is now restricted to the first bibliography only.
A solution that works without refcontexts would just populate the sortkey field of the @reference entries, since all @references live in the same \printbibliography that would sort them as desired as well. (Of course you would duplicate information in the sortkey field, so this may feel a bit ad hoc.)
The second issue looked very fishy to me, but is actually just an artefact of BibTeX's all-in inheritance approach: https://github.com/plk/biblatex/issues/794. Biber has a more sophisticated inheritance scheme where shorthands are not inherited.