# Is it possible to use bibunits and multibib simultaneously?

I'm typesetting a contributed book, which has a whole bunch of chapters written by a different author each. My job is make sure all contributions use more or less the same typesetting conventions and select index words.

For the references we wish to create a separate list for each chapter. Originally I was doing this using multibib, but it seems that package actually keeps each .aux file open while running pdflatex, hence now that I've finished editing a bunch of chapters, I'm suddenly running into the limited write-stack.

So I switched to bibunits, which is better suited for the job anyway.

However....... one of the contributions actually has four separate bibliographies that go with it. This is again precisely the problem multibib tries to solve and I happened to start with this contribution, which explains my original choice for that package.

So what I would like to do, is to use bibunits for all contributions and use multibib to split the references in this one special contribution. Since bibunits closes each unit before continuing to the next unit, I should be safe then wrt the limited write stack. Unfortunately these packages don't work well together, so it seems. I guess this has to do with the fact that both of these try to redefine similar same commands (although, since multibib explicitly defines \citeXXXX commands for each bibliography unit XXXX I kind of expected things to be allright)

The error I'm getting, is the standard, infamous:

! TeX capacity exceeded, sorry [input stack size=5000].
<inserted text>
}\endwrite
l.901 ...liographysyta{syta_vdweygaert-references}

!  ==> Fatal error occurred, no output PDF file produced!


What also strikes the eye, is that at some point I get an enormous long list of lines saying:

(./bu.aux) (./bu.aux) (./bu.aux) (./bu.aux) (./bu.aux) (./bu.aux) (./bu.aux)


Allright... back to what I guess is my question: I tried to get separate reference lists for a single chapter, while also creating references for individual chapters and failed. How can I succeed in doing this?

-
Is switching to biblatex an option? Then read on here: tex.stackexchange.com/questions/26598/… (also notice the linked questions at the right side) –  matth May 14 '12 at 14:15
That's a good suggestion, I need to check it out a bit more. The main problem I foresee is that the printer forces use to use the \cite command and if I understand things correctly, I'd have to replace these commands with a different biblatex command right? On the other hand.... I was already kind of doing that when using multibib.... :P –  Jakob van Bethlehem May 14 '12 at 14:29
Hmm, the special .bst files from the publisher also seem to give problems with biblatex. That's a bit more cumbersome. –  Jakob van Bethlehem May 14 '12 at 14:33
If you rely on a .bst file, biblatex is unfortunately no option. The whole point of biblatex is to get rid of the .bst language and delegate all formatting to style files written in LaTeX. –  Simifilm May 14 '12 at 14:38
Consider to switch to biblatex, select a style shippped with it that resembles your style and tweak it with the help of tex.stackexchange.com/questions/12806/…. –  lockstep May 14 '12 at 17:57

So after some hacking, I actually found a (IMO rather dirty) hack that actually makes things work for me, although with a minor modification: I don't use bibunits, but rather chapterbib.

The basic reason why they couldn't work together, is that they redefine a common set of commands. Hence the basic idea of my solution: make sure they are never, say 'active', simultaneously. For future reference, let me spill out my solution and the limitations (I'm aware of). All the following should be put in the header of your document.

The solution is designed for a setup with one main file, that uses \include to include a file for each chapter. The aim is to 'switch off' the chapterbib package before including a chapterfile that needs to have separate bibliographies. Notice that this means that this particular chapterfile can never have the bare \cite command. Any citation must be part of some subset that multibib deals with

(the reason is that when 'switching off' the chapterbib package, the \cite command works in the standard way, meaning that the \bibliography command corresponding to any \cite command in the chapterfile is expected in the main file.... hopefully this rather long explanation makes sense.. :P)

Step 1: create a backup of the commands that will be overwritten by loading multibib/chapterbib:

\makeatletter
\let\orig@bibliography\bibliography
\let\orig@thebibliography\thebibliography
\let\orig@include\include
\let\orig@cite\cite
\let\orig@nocite\nocite
\let\orig@bibliographystyle\bibliographystyle
\makeatother


Step 2: load multibib and define the required bibliography groups you need. Let me remind you again, that later on chapterbib will be 'switched off' whenever we make multibib 'active'. The consequence is that each citation inside a chapterfile that is processed by multibib must be part of some group, ie you can only use \citeXXX commands for groups you created using \newcites{XXX}{Some title}. So e.g. one could have:

\usepackage{multibib}
\newcites{main}{\refname}
\newcites{man}{Manuscripts}
\newcites{com}{Complete Works}


Step 3: the multibib package actually only rewrites the \bibliography and \thebibliography command. Next store these rewritten versions and move back to the original versions:

\makeatletter
\let\mbb@bibliography\bibliography
\let\mbb@thebibliography\thebibliography

\let\bibliography\orig@bibliography
\let\thebibliography\orig@thebibliography

\makeatother


Step 4: as far as the chapterbib package is concerned, it is as if the multibib was never loaded, so we can safely load the former package and store the changes it made:

\usepackage{chapterbib}

\makeatletter
\let\cpb@bibliography\bibliography
\let\cpb@thebibliography\thebibliography
\let\cpb@bibliographystyle\bibliographystyle
\let\cpb@include\include
\let\cpb@cite\cite
\let\cpb@nocite\nocite


Step 5: this step is at your own convenience and mainly safes typing - it entails creating a set of macros that allow easily switching between 'chapterbib-mode' and 'multibib-mode':

\def\bibmulti{% switch to multibib-mode
\let\bibliography\mbb@bibliography%
\let\thebibliography\mbb@thebibliography%
\let\bibliographystyle\orig@bibliographystyle%
\let\include\orig@include%
\let\cite\orig@cite%
\let\nocite\orig@nocite%
}

\def\bibchapter{% switch to 'chapterbib'-mode
\let\bibliography\cpb@bibliography
\let\thebibliography\cpb@thebibliography
\let\bibliographystyle\cpb@bibliographystyle
\let\include\cpb@include
\let\cite\cpb@cite
\let\nocite\cpb@nocite
}
\makeatother


Towards your document: so how to use all this? This is what your 'main' file could look like using the bibmulti and bibchapter macros:

\documentclass{article} % or whatever class you want
\begin{document}
\include{chapter1}
\include{chapter2}
\bibmulti
\include{chapter3}
\bibchapter
\include{chapter4}
\end{document}


This assumes the above described header, where indeed chapterbib is loaded second. That package will kick in for the first two chapters, each of which should have commands:

\bibliographystyle{your_favourite_style}
\bibliography{your_bibtex_database}


Then, just before including chapter three, we 'switch off' chapterbib and 'switch on' multibib; this file should use the special citation commands created by multibib, e.g.:

\citemain{ref1}
\citemain{ref2}
\citeman{manuscript1}
% etc...
% etc...
\bibliographystylemain{style_for_main}
\bibliographymain{main_references}

\bibliographystyleman{style_for_manuscripts}
\bibliographyman{manuscript_references}

\nocitecom{*}
\bibliographystylecom{style_for_com}
\bibliographycom{complete_works_references}


Then, after chapter 3 was processed, the bibchapter macro again switches to using the chapterbib version of the citation macros. So when chapter 4 is included, we're back to the original situation and the chapterbib package will again make sure that a separate reference list is created for that chapter.

I hope this explains covers enough for others. Maybe for reference I should mention that after TeXing this document, you'll need to run bibtex on the chapter files for chapters 1,2 and 4 (chapterbib writes citations inside the '.aux' file for the separate chapters) and on the files main.aux, man.aux and com.aux (these are the files created by multibib)

Limitations Each time you call \newcite, an element is pushed to the write-stack (I think). By using this setup you prevent having to do that for each chapter, but still if you have many chapters that need to be dealt with by multibib you'll likely still hit the limit of the write stack. In other words: this setup is designed for the case where the situation of multiple sets of references per chapter is rare (actually, in my particular case there is only one such chapter)

There may be other limitations I'm not aware of. I'm just a newbie at writing TeX for my LaTeX documents, so I really can't give guarantees about the applicability of this solution to other systems and related situations.

-