views:

721

answers:

5

I want to generate the bibliography for each section, and have it at the end of the section. When I do this at the moment it generates the full bibliography and places it after each section.

Is there a way that this can be done?

The advice here says

"The chapterbib package provides an option sectionbib that puts the bibliography in a \section* instead of \chapter*, something that makes sense if there is a bibliography in each chapter. This option will not work when natbib is also loaded; instead, add the option to natbib. "

I don't understand what this means, and I've tried experimenting with what I thought the options are. Specifically, what does "add the option to natbib" mean?

My subsequent question (which evolved after my first one was solved) is to not have pagebreaks between the references, and the next section.

Thank you for your help.

+2  A: 

I haven't tried it but as I read that it suggests:

\usepackage[sectionbib]{natbib} % Note the option in the optional argument
\usepackage{chapterbib}

though I'm only guessing at the correct order of those lines.

dmckee
Thanks - I now understand what the optional argument is. I tried it in both orders, and unfortunately it gives the same results (lists all refs after each section, rather than the specific refs used).
celenius
The natbib docs claim these work in either order.
Norman Ramsey
A: 

Did you try to compile multiple times, so the changes take effect?

ilakast
+6  A: 

In addition to

\usepackage[sectionbib]{natbib}
\usepackage{chapterbib}

You will have to put each section in a separate .tex file which you then \include. You will have to run bibtex on each .tex file separately.

N.B. Using \input rather than \include avoids unwanted page breaks, but it will not create the .aux file that BibTeX needs to do its work. I looked at the definition of \include, and I don't see how to disable the page-breaking function except by disabling \clearpage entirely. You could try

\let\originalclearpage=\clearpage
\def\clearpage{\relax}

right after your \begin{document}, but you may have to put some \originalclearpage in by hand.

Norman Ramsey
I finally figured that out! Thanks everyone.The unfortunate consequence is that I now have a page break between every reference, and the next heading. Aside from the newclude package (which does not appear to work for me), are there any other approaches for this?
celenius
Separating your chapters into separate files is also just a generally good idea anyway once you've got a long enough document to want to put references after each chapter. You'll also probably need to run `\bibliographystyle{<style>}` and `\bibliography{<bib-file>}` at the end of each chapter.
Steve
@celenius: I think you need `\include` to get a .aux file for bibtex to work with. I've edited my answer to include a hack that *might* suppress those page breaks.
Norman Ramsey
@celenius: I looked at the definition of `\include`, and my previous idea won't work. I edited my answer to suggest that you just disable `\clearpage` entirely, then put in explicit page breaks where needed.
Norman Ramsey
Unfortunately that does not appear to work either, as I can't compile the document when I include the code \let\originalclearpage=\clearpage\def\clearpage{\relax} (I'm using TexShop, lest that influences what I can do or not)
celenius
+1  A: 

@celenius - if you really want to get rid of that pagebreak, here's a very dirty trick to do it...

\makeatletter
\let\O@@input@\@input@
\def\@noclearpage{\@ifnextchar\clearpage\@gobble\relax}
\def\@input@#1{\O@@input@{#1}\@noclearpage}
\let\O@@include\@include
\def\@include{\expandafter\@noclearpage\O@@include}
\let\O@include\include
\def\include{\expandafter\@noclearpage\O@include}
\makeatother

Basically we perform surgery on the \include macro to get rid of all the \clearpage instances, but the cleanest way to do this, as you can see, is still really dirty. This is horribly brittle and will likely only work for the article class, so if you're using a different \documentclass, you're out of luck. I basically derived this by enabling \tracingcommands=1 and \tracingmacros=1 and grepping the .log file for \clearpage so that I could hack whatever gets called before it to insert a \@noclearpage.

I don't recommend this solution - it would be much better to look into how chapterbib works and fix it the right way, without depending on \include and the separate .aux files it generates... but I'm positive that would be a pretty difficult task. I guess another workaround would be to write a command to emulate \include's breaking up of .aux files, without actually doing the includes...


EDIT: okay, here's a quickie

\makeatletter
\newenvironment{auxfile}[1]{\relax
  \ifnum\@auxout=\@partaux
    \@latex@error{auxfile environments cannot be nested or \string\include d}
    \@eha
  \else\@changeaux{#1}\fi
}{\immediate\closeout\@partaux\let\@auxout\@mainaux}
\def\@changeaux#1{%
  \immediate\write\@mainaux{\string\@input{#1.aux}}%
  \let\@auxout\@partaux
  \immediate\openout\@partaux#1.aux%
  \immediate\write\@partaux{\relax}}
\makeatother

Then you can just insert \begin{auxfile}{foo}...\end{auxfile} and it will use foo.aux instead of the normal .aux file. This is fully compatible with chapterbib. I don't think CTAN has anything like this, so maybe I'll submit it as a mini-package.

Steve
Wow! Thanks for that; what is it and what do I do with it? I tried putting it in the preamble of main and the beginning of my chapters.
celenius
This just defines an environment - you can put the `\newenvironment` definition it in your main preamble, then use `\begin{auxfile}{sec1}...\bibliographystyle{...}\bibliography{...}\end{auxfile}` to encapsulate the sections that need to have their own bibs, rather than using `\include`.
Steve
Unfortunately it doesn't work for more than one section. I tried it for one section and it was fine; for a second section the compiler said there were illegal errors. "A level-1 auxiliary file: test.auxIllegal, another \bibstyle command---line 5 of file test.aux : \bibstyle : {chicago}I'm skipping whatever remains of this commandIllegal, another \bibdata command---line 6 of file test.aux : \bibdata : {refs}I'm skipping whatever remains of this commandDatabase file #1: refs.bib
celenius
Not entirely sure what your context is here - who's giving the errors? I'll assume it's bibtex. You'll need to run bibtex on the aux base names, not the main. That is, if you have main.tex that makes sec1.aux and sec2.aux, run `bibtex sec1` and `bibtex sec2` and **not** `bibtex main`.
Steve
When I use this now, it does work (separate references per section - very nice!), however I still get the page break, after each set of references.
celenius
+1  A: 

If you are using Biblatex, as for citing article titles, you can use it to produce bibliographies at the end of sections or chapters, or even have a combined bibliography where they are separated by chapter/section. As a package, it is intended to replace "babelbib, bibtopic, bibunits, chapterbib, cite, inlinebib, mlbib, multibib, splitbib."

You can put a bibliography after each section, in one of three ways. First, wrap the text of your section in a \begin{refsection}/\end{refsection} pair, as such

\section{SomeSectionName}
\begin{refsection}
% your text goes here
\printbibliography
\end{refsection}
\section{NextSection}

Second, after each \section statement you put a \newrefsection statement which ends the previous section and begins the new one. And, you precede the next \section with a \printbibliography statement, again. Finally, there is a refsection package option that takes either none, part, chapter, section, or subsection as an argument. To group your bibliographic entries per section in a global bibliography you use refsegment instead, using \bibbysegment to print all the segments in order. (\bibbysection can be used in the same manner for ref-sections, too.)

I don't know how much you'll have to split up your text, as per @Norman's answer, but with a little experimentation you can figure it out.

rcollyer
As an aside, I am a fan of latexmk (http://ctan.tug.org/tex-archive/support/latexmk/) which automatically runs each needed program in the latex cycle as many times as necessary. I'm using it, and biblatex, for my dissertation, and it saves me a lot of hassle.
rcollyer
Hmmm, this looks great, but I can't get it to work. Having installed Biblatex, I tried following the example above but I don't understand where the \newrefsection goes. (this is all very exciting though - thanks!)
celenius
@celenius: it's supposed to go right after your `\section` statements, instead of wrapping the whole section in an environment. Personally, I prefer to use the package option as you don't have to worry about adding all the additional statements.
rcollyer
I must be missing something - I've read part of the documentation and can get the first simple example of \printbibliography working, but the \begin{refsection} \printbibliography \end{refsection} is not working.
celenius
I honestly don't know. Try using the package option instead, and see how that works. Biblatex is still considered beta software, so there may be quirks in it that have to get ironed out.
rcollyer