1

I'm using chapterbib for a long document and the creation of bibliographies for each chapter works fine. Each chapter is in its own file that gets included.

Is there any option to split the chapter files by, e.g., section? Using input does not work as it seems and nesting includes is forbidden.

Any known workaround? Could cbunit or cbinput solve this? I'm not sure how they'd work here.

Thanks!

EDIT - MWE:

  • ./main.txt
\documentclass[a4paper,10pt,twoside,onecolumn,openright,final,titlepage]{book}

\usepackage[T1]{fontenc} \usepackage[utf8]{inputenc}

\usepackage{tocbibind}

%\usepackage[rootbib]{chapterbib} \usepackage[sectionbib]{chapterbib}

\bibliographystyle{plain} \usepackage[english]{babel}

\usepackage[pagebackref=true]{hyperref}

\usepackage{blindtext}

\renewcommand{\backref}[1]{} % Internally redefine backref in order to display \renewcommand{\backrefalt}[4]{[{\small% the pages a reference was cited \ifcase #1 Not cited.% \or Cited on page~#2.% \else Cited on pages #2.% \fi% }]}

\begin{document}

\tableofcontents

\include{chapters/chpt_1}

\include{chapters/chpt_2}

\chapter{Bibliography}

%Don't add a section to the toc \renewcommand{\addcontentsline}[3]{} %Don't add a title to the bibliography \renewcommand{\bibname}{} \bibliography{mybib}

\end{document}

  • ./chapters/chpt_1.tex:
\chapter{Test}

\section{First}

\blindtext[10]

\cite{mathworld:AssociatedLaguerrePolynomial}

\section{Second}

\blindtext[8]

\cite{mathworld:AssociatedLegendreDifferentialEquation,mathworld:AssociatedLaguerrePolynomial}

\bibliographystyle{plain} \bibliography{../mybib}

  • ./chapters/chpt_2.tex
\chapter{Test}

\section{First}

\blindtext[10]

\cite{Weisstein}

\section{Second}

\blindtext[8]

\cite{Weissteina}

\section{Third}

\blindtext[6]

\cite{Weissteinb}

\bibliographystyle{plain} \bibliography{../mybib}

  • mybib.bib
% Encoding: UTF-8

@Misc{mathworld:AssociatedLaguerrePolynomial, Title = {{Associated Laguerre Polynomial. From MathWorld---A Wolfram Web Resource \url{http://mathworld.wolfram.com/AssociatedLaguerrePolynomial.html}}},

Author = {Weisstein, Eric W.}, Note = {Last visited on 16/03/2016} }

@Misc{mathworld:AssociatedLegendreDifferentialEquation, Title = {{Associated Legendre Differential Equation. From MathWorld---A Wolfram Web Resource \url{http://mathworld.wolfram.com/AssociatedLegendreDifferentialEquation.html}}},

Author = {Weisstein, Eric W.}, Note = {Last visited on 16/03/2016} }

@Misc{Weisstein, author = {Weisstein, Eric W.}, note = {Last visited on 16/03/2016}, title = {{Wigner 3j-Symbol. From MathWorld---A Wolfram Web Resource \url{http://mathworld.wolfram.com/Wigner3j-Symbol.html}}}, }

@Misc{Weissteina, author = {Weisstein, Eric W.}, note = {Last visited on 16/03/2016}, title = {{Wigner 6j-Symbol. From MathWorld---A Wolfram Web Resource \url{http://mathworld.wolfram.com/Wigner6j-Symbol.html}}}, }

@Misc{Weissteinb, author = {Weisstein, Eric W.}, note = {Last visited on 16/03/2016}, title = {{Wigner 9j-Symbol. From MathWorld---A Wolfram Web Resource \url{http://mathworld.wolfram.com/Wigner9j-Symbol.html}}}, }

To compile:

  1. pdflatex main.tex generates the chapter aux files
  2. cd chapters; bibtex chpt_1.aux; bibtex chpt_2.aux; cd -
  3. comment out sectionbib; uncomment rootbib
  4. pdflatex main.tex; bibtex main.aux; pdflatex main.tex; pdflatex main.tex

This get's us a two entry bibliograpgy in chapter 1, a three entry bibliograpgy in chapter 2, and a five entry bibliograpgy at the end. For ease of writing - and versioning - I'd like to have each section of each chapter in its own tex-file - if that makes sense.

gothicVI
  • 588

2 Answers2

3

There are two packages that might help out in this situation: subfiles and import. My preference goes to import, so I used that package to answer your question.

To get an error free compilation I had to alter a few things in your example:

  1. The package chapterbib is still available on CTAN, but not in my TeX distribution of MikTeX. So I replaced that by the much powerful package biblatex using the biber engine to sort the citiations.
  2. In your bib file you used @Misc. That uppercase M isn't recognised by biblatex, so I changed that to the lowercase m.
  3. References to pages can be entered as option in the \cite or \parencite commands in texts.
  4. Per chapter biobliography can be obtained using the \begin{refsection} <chapter content> \end{refsection} structure.

The import package provides two commands (and aliases) to divide a long document into subdocuments. It allows for subdividing a subdocument as well. That is what I did in the next MWE's.

MWE of main.tex file:

\documentclass[a4paper,10pt,twoside,onecolumn,openright,final,titlepage]{book}

\usepackage{import}

\usepackage[style=alphabetic, backend=biber]{biblatex} \addbibresource{refer.bib} % \usepackage[english]{babel}

\usepackage{hyperref}

\usepackage{blindtext}

\begin{document}

\tableofcontents

\import{chapter1/}{chapter1}

\import{chapter2/}{chapter2}

\printbibliography[heading=bibintoc]

\end{document}

MWE of the first chapter (the second chapter has the same structure), stored in ./chapter1/chapter1.tex:

\begin{refsection}

\chapter{Test}

\subimport{sections}{section1}

\subimport{sections}{section1}

\subimport{sections}{section3}

\printbibliography[heading=subbibintoc, title=Chapter bibliography]

\end{refsection}

A MWE of section one as an example of the structure of all sections, stored in ./chapter1/sections/section1.tex:

\section{First}

\blindtext[10]

\cite{mathworld:AssociatedLaguerrePolynomial}

As you can see in the provided MWE examples, there is a structure in the way chapters and sections are organised. That structure reflects the folder organisation in which those files are stored. The main document is in a top folder. The chapter files are stored in their respective subfolders (chapter1, chapter2, ...) to that top folder. The sections are stored in a subfolder sections to each chapter folder.

In the main file each chapter is imported using the command: \import{chapterx}{chapterx}. The first chapterx refers to the folder containing the .tex file chapterx (the second one).

The command \subimport{sections}{sectiony} uses the same convention: first the folder where the file is stored.

Note Answer is partly modified according to the comments given by Sylvain Rigal. The comment about regsection=chapter didn't work as expected, so the remark give in point 4 is maintained.

alchemist
  • 1,761
  • No need for \begin{refsection} environment with biblatex, just use the package option refsection=chapter for bibliography per chapter and print the chap bib with the option segment=\therefsegment (so no need to \nocite{*} which is not optimal BTW because it print all the ref in the second chapter bib and all the ref in the bib file are printed not just the cited ones), see Multiple bibliographies and one global bibliography - all with global labels – zetyty Jun 08 '23 at 18:00
  • No need for \usepackage{tocbibind} and \addcontentsline{toc}{chapter}{Bibliography} instead print the chap bib with the option heading=subbibintoc and the global bib with the option heading=bibintoc – zetyty Jun 08 '23 at 18:00
  • You're right about the comments @SylvainRigal . I didn't delete / alter those lines as I was primarily focussed on the question about files and subfiles. I will alter the text / code in the MWE's in my answer according to your comments. – alchemist Jun 08 '23 at 18:27
  • Erratum: it seems that it's better to use the biblatex package option refsegment=chapter rather than refsection... details here. Sorry! – zetyty Jun 08 '23 at 20:08
  • Thanks for the detailed answer. However, I'm facing an issue: I've put the sections in the same directory as the chapters using \subimport{./}{cpt1_sec1.tex} which seems to work but I'm getting Package keyval Error: segement undefined. ...\therefsegment, title=Chapter bibliography]. A pdf is generated but with bold [mathworld:AssociatedLaguerrePolynomial] where the citation should be and with no bibliographies - neither in chapters nor at the end. The aux file contains cite entries though. – gothicVI Jun 09 '23 at 12:43
  • 1
    @gothicVI I assumed Silvain had tested his comments. They can be found in the biblatex manual (cf. page 142), but I couldn't get that working either. So I returned to the solution given in point 4, which is also documented in the manual (cf. page 141). Excuses for not testing the comments before I changed my answer the first time (was on road). – alchemist Jun 09 '23 at 14:30
  • @alchemist that sort of works - except for the global bibliography and the backrefference EDIT: the backref option solves the latter – gothicVI Jun 09 '23 at 15:10
  • @gothicVI that's odd: I the MWE I made the chapter bibliographies and the global bibliography were printed normally. So maybe something in your full code is disrupting the output?? – alchemist Jun 09 '23 at 16:34
  • Indeed. I've copied your example without changes but still no global bib. Do I need to run something on top? I'm getting a the following in the log: 'babel/polyglossia' detected but 'csquotes' missing. and 22: Empty bibliography where line 22 is the \printbibliography[heading=bibintoc] – gothicVI Jun 09 '23 at 17:32
  • @gothicVI Do you get the chapter bibliographies where they are supposed to be? Let's see if we can discuss this behaviour in a chat room. – alchemist Jun 09 '23 at 18:13
1

For the sake of completeness regarding the great answer of alchemist, here a solution with chapter bibliographies and a global bibliography which print only the references cited in the document (not all the references contained in the bib file).

It uses the refsegment=chapter option of biblatex package (no need for \begin{refsection}...\end{refsection} environment anymore, taken from ebosi's answer) and the option segment=\therefsegment for the \printbibliography command, in each chapter you want a chapbib, in order to print only the cited references of the current refsegment (which corresponds to a chapter in this case).

A MWE (shamefully inspired by alchemist's answer):

\documentclass{book}
\usepackage{import}

\begin{filecontents}{ref.bib} @Misc{ref1, Title = {Ref1 title}, Author = {Ref1 text} } @Misc{ref2, Title = {Ref2 title}, Author = {Ref2 text} } @Misc{ref3, Title = {Ref3 title}, Author = {Ref3 text} } @Misc{ref4, Title = {Ref4 title}, Author = {Ref4 text} } \end{filecontents}

\usepackage[refsegment=chapter, backend=biber]{biblatex} \addbibresource{ref.bib}

\begin{filecontents}{chapters/chapter1/chapter1.tex} \chapter{A chapter} \cite{ref1} \subimport{sections}{section1} \printbibliography[heading=subbibintoc,segment=\therefsegment, title=Chapter bibliography] \end{filecontents}

\begin{filecontents}{chapters/chapter1/sections/section1.tex} \section{A section} \cite{ref2} \end{filecontents}

\begin{filecontents}{chapters/chapter2/chapter2.tex} \chapter{A second chapter} \subimport{sections}{section1} \printbibliography[heading=subbibintoc,segment=\therefsegment, title=Chapter bibliography] \end{filecontents}

\begin{filecontents}{chapters/chapter2/sections/section1.tex} \section{A section} \cite{ref3} \end{filecontents}

\begin{document} \tableofcontents \import{chapters/chapter1}{chapter1} \import{chapters/chapter2}{chapter2} \printbibliography[heading=bibintoc,title={Global bibliography}] \end{document}

As pointed in comment, if you store the sections tex files in the chapter folder then use \subimport{./}{section1} in place of \subimport{sections}{section1}.

Result in both cases (note that the reference ref4 is not printed in the global bib because it is not cited in the document):

enter image description here

zetyty
  • 779