References after each chapter - CUED PhD Thesis Template

I know it is unorthodox but there's really no way to show what I have done other than the link.

I am writing my thesis using the PhD Thesis Template for Cambridge University Engineering Department (CUED) (v2.3.1) found on Overleaf at my tutor's request and I can't seem to be able to separate the references so that they are after each chapter.

I have tried using natbib with chapterbib as one question here suggested, I have tried using biblatex with refsection=chapter and nothing.

I don't know enough to understand the .cls or other documents that define the kind of document class I am using.

I am calling each reference in the chapter like

\bibliographystyle{aipnum4-1.bst}
\setstretch{0.9}
\bibliography{ch1/biblio1.bib}


I don't know what to do, I have read every question on here and cant seem to make it work.

Thanks!

Edit: Forgot to mention, even though I've tried different ways to get multiple references sections, at the moment I am using biblatex. The references for the first chapter are shown for all the rest of the chapters too, and the references that are not in the .bib of the first chapter are displayed like ?? in the rest of the thesis.

Edit2: A link to a minimal working example in overleaf is here. I know it's not ideal but I cant think of any other way.

• \bibliographystyle is incompatible with biblatex. If you get ?? for unprocessed references you are not even using biblatex (in which case you would get bold entry keys). Unfortunately the link to the template on Overleaf is not enough. The template offers a great many options and we don't know which you use. Please show us an example document that explains what you are doing (tex.meta.stackexchange.com/q/228/35864 and tex.meta.stackexchange.com/q/4407/35864). – moewe Jan 11 '19 at 4:37
• But really, do yourself a favour and avoid that template and all other templates for that matter. At best they needlessly complicate things when you want to change stuff, at worst they contain outdated or outright wrong code that breaks in unexpected places and when you can least afford it. See tex.stackexchange.com/q/390683/35864. PhDThesisPSnPDF.cls alone in its current form is 1200 lines long and thesis.tex inputs another few hundred lines from Preamble/preamble.tex. – moewe Jan 11 '19 at 4:39
• Honestly, I wish I could change templates but I cant, it was my at tutor's request and we are already not in the best of terms. I have sadly made a lot of changes and added things to the preamble. I have edited the post to add the link to a minimal example of what I am working on, only two chapters with enough text to exemplify what is happening. – M.O. Jan 11 '19 at 5:03
• The situation with biblatex was more complicated: While you were not actually using the package, that was only due to a weird coincidence. I have added the tag back in and tried to address the issues in my answer. – moewe Jan 11 '19 at 6:09

If at all possible you should try to avoid templates in general. See Why should you avoid using (complex) templates? and https://github.com/johannesbottcher/templateConfusion. Most templates are fine while they are working and giving you exactly the output you want, but it can become increasingly complicated to modify them. Bad templates may contain outdated or outright wrong code and may blow up in your face at any moment.

This particular template (Link to the Overleaf version) is also available on https://github.com/kks32/phd-thesis-template and is intended to be a PhD thesis template for Cambridge University Engineering Department (CUED). If that's not where you will be submitting your thesis, it is probably better not to use that template because you will have to modify it.

I have not had a much closer look at the template than needed for this answer, but I did not see any obvious TeXnical blunders (apart from the counter-intuitive option handling below). My concerns at this moment are mainly conceptual (this template does a lot and is intended for a particular university department) and pedagogical (the code in the template can look frightening, the many options and settings can be overwhelming and even the basic thesis.tex is long and contains many additional \inputs, \includes and conditionals).

The class has several bibliography-related options and custombib would hand all control over the bibliography back to you if you had not also used the option numbered. The way these options are handled means that (counter-intuitively) the earlier option numbered just overrides the custombib and loads natbib instead. Note that the class does not care about the order of these bibliography options, the outcome is determined by the particular nesting of tests in the implementation of the class. And the outcome of using conflicting options is not at all clear from the outside.

In Preamble/preamble.tex, specifically with

\ifuseCustomBib
%\usepackage[backend=biber,refsection=section, style=numeric-comp, citestyle=numeric, sorting=nty]{biblatex}

\usepackage[firstinits=true,
bibencoding=inputenc,
hyperref=auto,
pagination=none,
%style=standard,
refsection=chapter]
{biblatex}

\bibliographystyle{aipnum4-1.bst}
\fi


you try to explicitly load the biblatex package, yet in the next line of code you write

\bibliographystyle{aipnum4-1.bst}


which is a command of traditional BibTeX bibliographies that is incompatible with biblatex (and also slightly incorrect: the style in \bibliographystyle should be given without file extension, \bibliographystyle{aipnum4-1} would be correct).

Be that as it may, the numbered option quashes your attempt to define the bibliography anyway. It just causes natbib to be loaded and ignores your definitions because they are guarded by \ifuseCustomBib (which is false).

In the chapters (e.g. Chapter5/chapter5.tex) you then produce a bibliography with

I know this seems kind of extra, it's so that no extra white page is added in between the text and the references.
\begingroup
\makeatletter
\renewcommand{\chapter}{
\if@openright\clearpage\else\clearpage\fi
\thispagestyle{empty}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter
}
\makeatother

\bibliographystyle{aipnum4-1.bst}
\setstretch{0.9}
\bibliography{Chapter5/biblio5.bib}
\endgroup
\thispagestyle{empty}
\clearpage
%\if@openright\cleardoublepage\else\clearpage\fi
\mbox{}
\thispagestyle{empty}


which looks wrong on quite a few levels. But again we can take away from this bit of code that you are using traditional BibTeX methods and not biblatex – this time explicitly.

There are several methods to obtain split bibliographies (see Sectioning bibliography by type of referred item) and there are BibTeX-compatible way to obtain per-chapter bibliographies as well (see References at the end of each chapter and for example How to use chapterbib package: syntax and Bibliography in each chapter). Yet I believe that biblatex offers the easiest and most convenient way to per-chapter bibliographies.

Have a look at What to do to switch to biblatex? to see what you have to do to switch to biblatex.

Remove all the \bibliographystyle and the \bibliography instructions in the document body.

The following is a "minimal" example using PhDThesisPSnPDF that shows how you could use biblatex in your document. Please read the comments carefully, they should explain how this works.

% no option numbered
\documentclass[a4paper,12pt,times,preprint,index, custombib]{Classes/PhDThesisPSnPDF}

% in Preamble/preamble.tex you need
\ifuseCustomBib
\usepackage[
backend=biber,
style=numeric,
refsection=chapter]
{biblatex}

% you also need to declare your bib file
% biblatex-examples.bib is an example file
% that is available everywhere where biblatex is installed
% you'll need something like
% and maybe
% etc. you can have several of these lines
\fi

\begin{document}
% in your chapters you'll have
\chapter{Lorem}
Lorem ipsum~\cite{sigfridsson} dolor~\cite{worman}.
% just use \cite as normal

\printbibliography[heading=subbibliography] % this prints the bibliography

% next chapter
\chapter{Dolor}
Dolor~\cite{nussbaum} sit~\cite{sigfridsson}

\end{document}


It seems that in your case the option refsection=chapter produces the error

! Package biblatex Error: Patching \@makechapterhead failed.

See the biblatex package documentation for explanation.
Type  H <return>  for immediate help.
...

l.21 \begin{document}

This is an internal issue typically caused by a conflict
between biblatex and some other package. Modifying


on Overleaf. This error does not occur on my local installation (MikTeX 2.9 on Win 10 updated this morning) with up to date packages.

The problem seems to be that the definition of \@makechapterhead is botched as \tracingpatches shows

[debug] tracing \ifpatchable on input line 142
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ control sequence is a macro with parameters
[debug] -- macro cannot be retokenized cleanly
[debug] -> the macro may have been defined under a category
[debug]    code regime different from the current one
[debug] -> the replacement text may contain special control
[debug]    sequence tokens formed with \csname...\endcsname;
[debug] -> the replacement text may contain carriage return,
[debug]    newline, or similar characters


I did not manage to find the culprit (between a completely outdated TeX system with biblatex 3.7, a massively complicated template and a system I have no proper access to that was just too much for now), but you can remove the option refsection=chapter and emulate it by hand with \begin{refsection}...\end{refsection}.

% no option numbered
\documentclass[a4paper,12pt,times,preprint,index, custombib]{Classes/PhDThesisPSnPDF}

% in Preamble/preamble.tex you need
\ifuseCustomBib
\usepackage[
backend=biber,
style=numeric,
]
{biblatex}

% you also need to declare your bib file
% biblatex-examples.bib is an example file
% that is available everywhere where biblatex is installed
% you'll need something like
% and maybe
% etc. you can have several of these lines
\fi

\begin{document}
% in your chapters you'll have
\begin{refsection}
\chapter{Lorem}
Lorem ipsum~\cite{sigfridsson} dolor~\cite{worman}.
% just use \cite as normal

\printbibliography[heading=subbibliography] % this prints the bibliography
\end{refsection}

% next chapter
\begin{refsection}
\chapter{Dolor}
Dolor~\cite{nussbaum} sit~\cite{sigfridsson}

\end{refsection}
\end{document}


A different workaround would be to issue

\makeatletter

directly before you load biblatex.
• Thank you so much for your answer but unfortunately, it doesn't work. biblatex doesn't load either on my pc or overleaf, both ways I get the same errors. I must have a package that is conflicting with it. If you could take a look I'd be so greatefull. The same overleaf link I sent has been updated with the changes you put forward. Again, I know this took a lot of work from you so thank you so much. I feel like I know more things now. – M.O. Jan 11 '19 at 12:07
• I couldn't make either of those work. They both compile without any errors though, but no reference is shown. Would you mind telling me your version of latex and any relevant packages? I am on Ubuntu 17 and I get the same error in my computer than in Overleaf. – M.O. Jan 11 '19 at 14:17
• I am not sure. There's a problem with the second chapter once I actually add the text in. It says Undefined control sequence. \printbibliography[heading=subbibliography]. Already made sure the .bib file had no non-ASCII characters with grep and I know the problem lies in the bib file because if I comment out \printbibliography then it compiles fine. There are only ASCII characters in my .bib`, now at least (before it never had an issue), but it keeps happening. – M.O. Jan 11 '19 at 16:21