# Add bibliography to toc with KOMA-Script

I try to write a small class for myself at the moment. So far everything is working as expected except of putting the bibliography title inside the table of content. Normally I use the KOMA option bibliography=totoc and listof=totoc and everything is working fine.

What I've done is to create a class which accept three options:

• add-tocs-to-toc (List of figure, list of tables, literatur)
• bib-file (pass custom bib file)

The KOMA option listof=totoc is working as aspected.

My class file bibtest.cls

\ProvidesClass{bib-test}[2017-11-12 v0.1 MWE]

\RequirePackage{etoolbox, pgfopts, scrlfile}

\newcommand*{\bibtest@cls@baseclass}{scrreprt}
\newcommand*{\bibtest@cls@baseclass@options}{}

% ----------------------------------------------------------
% Define booleans
% ----------------------------------------------------------
\newbool{bibtest@cls@if@bib}
\newbool{bibtest@cls@if@tocstotoc}

% ----------------------------------------------------------
% Define available options
% ----------------------------------------------------------
\pgfkeys{
bibtest/.cd,
bib-file/.store in          = \bibtest@bib@file,
bib-file = {},
unknown/.code               = \edef\bibtest@cls@baseclass@options{\bibtest@cls@baseclass@options,\pgfkeyscurrentname}
}

\ifbool{bibtest@cls@if@tocstotoc}{%
\KOMAoptions{%
listof  = totoc,
bibliography = totoc
}
}{}

\ProcessPgfOptions{/bibtest}

\ifbool{bibtest@cls@if@bib}{%
\AfterPackage{inputenc}{\RequirePackage[autostyle]{csquotes}}%
\AtEndDocument{\printbibliography}%
\RequirePackage[%
backend      = biber,
style        = numeric-comp
]{biblatex}%
\ifdefempty{\bibtest@bib@file}{%
\IfFileExists{source.bib}{%
\pgfkeys{/bibtest/bib-file = source.bib}%
}{%
\ClassWarning{bibtest}{Cannot find default source.bib file.}%
}%
}{%
\IfFileExists{\bibtest@bib@file}{}{%
\ClassWarning{bibtest}{Couldn't find passed bib file}
}%
}%
}{}%

\endinput


The MWE for testing

\documentclass[
bib-file = biblatex-examples.bib
]{bibtest}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\begin{document}
\tableofcontents
\listoffigures
\listoftables

\chapter{Hallo Welt}
Hallo Welt
\chapter{Hello World}
Hello World \cite{aksin}
\end{document}


As you might see there is no literatur entry inside the table of content. I tested under Windows 10 with MiKTeX.

• There is also no bibliography at all. Why should there be an entry? By the way, not KOMA, but biblatex is resposible for the entry. – Johannes_B Nov 12 '17 at 13:16
• You are overcomplicating a simple document for the sake of nothing. A class should not have a lot of testing and loading of packages. – Johannes_B Nov 12 '17 at 13:17
• The class provides \AtEndDocument{\printbibliography} to generate the bibliography, when the option add-bibliography is set. – faltfe Nov 12 '17 at 14:44

If you use biblatex together with the KOMA classes it is actually biblatex who is responsible for the format of the heading in your bibliography and not the KOMA class.

Confer also the warning in the KOMA documentation (p. 139)

Using an additional bibliography style like natbib, babelbib, or biblatex limits the influence of KOMA-Script to the bibliography hardly. In such cases it is important so see the manual of the bibliography package!

Starting from version 3.8 of biblatex and version 3.25 of KOMA script, the two packages will work together more closely. KOMA script has added a new command \bibliography@heading to typeset the bibliography heading and that command can be used by biblatex. The standard bibliography and biblist headings now use this command with KOMA classes. In essence that means that biblatex hands control over the bibliography heading back to KOMA (this behaviour can be overridden explicitly), so the default heading will obey all of KOMA script's options. Thanks to Markus Kohm for making that possible. More details can be found in https://github.com/plk/biblatex/issues/627 and https://sourceforge.net/p/koma-script/code/2685/tree//trunk/scrkernel-bibliography.dtx?diff=52f0ae7181b24b7fb854e8b2:2684.

There are further ideas to make it easier for document classes to work together with biblatex, but that would likely require change in both biblatex and the respective classes. If you have any ideas or comments, please share them at https://github.com/plk/biblatex/issues/693.

biblatex has some rudimentary code to detect certain class options passed to the KOMA classes. This, however, only works if the option is passed as optional global argument while loading the class. It does not work with \KOMAoptions.

The biblatex documentation mentions on p. 130 in footnote 27 (emphasis mine)

This applies to the traditional syntax of these options (bibtotoc and bibtotocnumbered) as well as to the <key>=<value> syntax introduced in KOMA-Script 3.x, i.e., to bibliography=nottotoc, bibliography=totoc, and bibliography=totocnumbered. The global toc=bibliography and toc=bibliographynumbered options as well as their aliases are detected as well. In any case, the options must be set globally in the optional argument to \documentclass.

So you will have to use biblatex commands and options to get the bibliography to appear in the TOC, not KOMA options.

Instead of \printbibliography use \printbibliography[heading=bibintoc].

Alternatively, you can redefine the standard \defbibheading{bibliography}. Or you could manually set

 \let\ifkomabibtotoc=\@firstoftwo
\let\ifkomabibtotocnumbered=\@secondoftwo


in bibtest@cls@if@tocstotoc.

• I wish every answer here on TeX.SX would be so good :-) – Johannes_B Nov 12 '17 at 13:51
• Thanks for your answer. \printbibliography[heading=bibtotoc]give me the error "! Package biblatex Error: Heading 'bibtotoc' not found." If I use global options as suggested in the documentation the bibliography entry within the TOC is generated. – faltfe Nov 12 '17 at 14:46
• @faltfe Ooop, sorry. It should have been heading=bibintoc. Corrected. – moewe Nov 12 '17 at 14:48
• @moewe Thanks, now it is working as aspected. – faltfe Nov 12 '17 at 14:50