16

I am writing my Ph.D. thesis in XeLaTeX with Biblatex, and there are times when I need to compile individual chapters, and times when I need to compile the entire document, so I use subfiles, which works wonderfully. However, in each of these chapters I have a \printbibliography command, which I would like to turn off when compiling the final document, so that only the \printbibliography command is run in the mainfile.

The file structure is something like:

Main.tex:

\usepackage{subfiles, biblatex}
\begin{document}

\subfile{Chapter1.tex}

\printbibliography
\end{document}

Chapter1.tex:

\documentclass[Main.tex]{subfiles}
\begin{document}
\cite{Someguy1981}
\printbibliography
\end{document}
12

From subfiles.sty the \subfile command is defined as:

\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}

So it is straightforward to hook in some code to locally redefine \printbibliography to do nothing. For an example add the following to the preamble of the main file:

\makeatletter

\newrobustcmd*{\nobibliography}{%
  \@ifnextchar[%]
    {\blx@nobibliography}
    {\blx@nobibliography[]}}

\def\blx@nobibliography[#1]{}

\appto{\skip@preamble}{\let\printbibliography\nobibliography}

\makeatother

Note that biblatex makes bibliographic data available via citation commands. So unless you want to view the bibliography entries outside the main file, the subfiles need not invoke \printbibliography.

  • I have tested this and it works perfectly. – Skander Apr 5 '13 at 23:39
  • 1
    This stuff goes in the main file or in the subfile? Does it works with standard LaTeX or has something XeLaTeX specific? – Lucas Soares Jun 22 '14 at 2:29
5

I also used Audrey's approach (https://tex.stackexchange.com/a/107111/85983) for a while, but I got annoyed of having to write \printbibliography at the end of each subfile. Now I'm using the following Latex Hooks in the preamble of my main file:

%%% Default start and end for each subfile
\AtBeginDocument{%
}

\AtEndDocument{%
    \printbibliography
    \listoffigures
    \listoftables
    \newpage
    \printacronyms[include-classes=abbrev,name=Acronyms]
}
2

Borrowing a post on this Latex Community thread, you can define your \printbibliography command in the preamble and then un-define it in the main document. Your main file will then contain its own call to \bibliography{} and \biblographystyle.

Something like:

Main.tex

\usepackage{subfiles, biblatex}
\newcommand{\dobib}{ % Define the command
    \bibliographystyle{acm}
    \bibliography{../../references} % Place the path relative to the subfile here
}

\begin{document}
\renewcommand{\dobib}{} % Un-define the command

    \subfile{Chapter1.tex}

    \bibliographystyle{acm}
    \bibliography{../references} % Place the path relative to the main file here
\end{document}

Chapter1.tex

\documentclass[Main.tex]{subfiles}
\begin{document}
    According to SomeGuy \cite{Someguy1981} pigs can fly.

    \dobib
\end{document}

This works fine for me.

2

Another approach could be to distinguish between the subfiles and the main file by their jobname. Say your main file is called Thesis.tex, then the jobname is normally Thesis and you can test

\IfEq{\jobname}{\detokenize{Thesis}}{}{%
    \AtEndDocument{%
        \printbibliography%
    }
}

this will insert a bibliography at the end of the subfiles and do nothing in your main file.

(if possible use this before \usepackage{biblatex}, otherwise it might cause a warning, see undefined references warning for \AtEndDocument{\printbibliography})

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.