3

Consider the following files:

.
├── main.tex
├── preface.tex
├── preface.bib
├── chapter.tex
└── chapter.bib

The contents of these files are detailed at the end of this post.


The important part is the following. I am using \usepackage{titletoc} in conjunction with \usepackage[sectionbib]{chapterbib}. When titletoc is not used in a chapter (see preface.tex), then everything goes fine and the bibliography is added at the end of the chapter. However when titletoc is used in a chapter (see chapter.tex) in the following way:

\chapter{Chapter}
\startcontents[chapters]
\printcontents[chapters]{p}{1}{}{}
% ... CONTENTS HERE ...
\bibliographystyle{alpha}
\bibliography{chapter} 

then one of the two package seems to fail, and I end up with the bibliography being a chapter (and not a section within a chapter as I would like), and the citations failing and appearing as [?].

Question: how to make it work properly and have chapters starting with a local table of contents, and ending with a section containing the references?

Download of the files: link (this is not necessary, it's just for those who want to avoid to have to copy/paste)


illustration


main.tex:

% main.tex
\documentclass[oneside, 10pt]{book}

\usepackage[T1]{fontenc}
\usepackage[margin = 2cm]{geometry}
\usepackage{lipsum}
\usepackage{titletoc}
\usepackage[sectionbib]{chapterbib}  
\usepackage{hyperref}

\begin{document}
\frontmatter
\include{preface}
\mainmatter
\include{chapter}
\end{document}

preface.tex:

% preface.tex
\chapter{Preface}

\section{Preface first section}
\lipsum[1]\cite{preface:ref1}

\section{Preface second section}
\lipsum[1]\cite{preface:ref2}

\section{Preface third section}
\lipsum[1]\cite{preface:ref3}

\bibliographystyle{alpha}
\bibliography{preface} 

preface.bib:

% preface.bib
@article{preface:ref1,
    author  = {Someone}, 
    title   = {Preface1},
    journal = {Journal},
    year    = 2000,
}

@article{preface:ref2,
    author  = {Someone}, 
    title   = {Preface2},
    journal = {Journal},
    year    = 2000,
}

@article{preface:ref3,
    author  = {Someone}, 
    title   = {Preface3},
    journal = {Journal},
    year    = 2000,
}

chapter.tex:

% chapter.tex
\chapter{First chapter}
\startcontents[chapters]
\printcontents[chapters]{p}{1}{}{}

\section{First chapter first section}
\lipsum[1]\cite{chapter:ref1}

\section{First chapter second section}
\lipsum[1]\cite{chapter:ref2}

\section{First chapter third section}
\lipsum[1]\cite{chapter:ref3}

\bibliographystyle{alpha}
\bibliography{chapter} 

chapter.bib:

% chapter.bib
@article{chapter:ref1,
    author  = {Someone}, 
    title   = {FirstChapter1},
    journal = {Journal},
    year    = 2000,
}

@article{chapter:ref2,
    author  = {Someone}, 
    title   = {FirstChapter2},
    journal = {Journal},
    year    = 2000,
}

@article{chapter:ref3,
    author  = {Someone}, 
    title   = {FirstChapter3},
    journal = {Journal},
    year    = 2000,
}
  • 1
    I would look at etoc and biblatex instead. – Johannes_B Apr 12 at 3:38
2

Yes there is a conflict with the packages. (The sectionbib option and the use of hyperref are unrelated.) When generating a TOC (or LOF/LOT) chapterbib inserts settings for the included-file (chapter) indicator, so any cites in the title or caption will be produced properly in the TOC/LOF/LOT (even though such cites are bad). No attempt to preserve local context is made, with the assumption that the TOC is in the main file, but that leaves the indicator pointing to the main file at the end of the TOC.

I see that the partial TOC is performed in a group, so the quickest fix is to change \gdef to \def in the definition

\def\@CB@wtoc#1{\string\@writefile{#1}{\gdef % <- change to \def
  \string\the@ipfilectr{\@extra@b@citeb}}}%

(Change it yourself on line 24 of chapterbib.sty!) I expect I may introduce some more complicated state restoration, along the lines of

 \def\@tempa#1{\AtBeginDocument{\if@filesw
     \immediate\write\@auxout{\string\@writefile
       {#1}{\string\global\string\let\string\save@ipfilectr\string\the@ipfilectr}}\fi}}
 \@tempa{toc}\@tempa{lof}\@tempa{lot}
 \def\@tempa#1{\AtEndDocument{\if@filesw
     \immediate\write\@auxout{\string\@writefile
       {#1}{\string\global\string\let\string\the@ipfilectr\string\save@ipfilectr}}\fi}}
 \@tempa{toc}\@tempa{lof}\@tempa{lot} 

By the way, it seems like a lot of trouble to have separate bib files for each chapter. I wouldn't do that unless the chapters were written by different authors independently.

| improve this answer | |
  • Can you elaborate on the state restoration and where to put it? – Vincent Apr 12 at 4:40
  • 1
    That block could go anywhere in chapterbib.sty, or after \makeatletter in the document preamble. It uses \AtBeginDocument and \AtEndDocument to make the .toc (and .ptc) files start with \global\let\save@ipfilectr\the@ipfilectr to remember the pointer; and end with \global\let\the@ipfilectr\save@ipfilectr to restore the original setting. – Donald Arseneau Apr 12 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.