5

I am trying to construct a report document that consists of a series of documents that can also be compiled individually. In these subfile documents I have been using \bibentry{} from the bibentry package to allow me to print a full citation in text.

Compiling the subfile documents individually works fine, and if I comment out the "\bibentry" line in the subfile the main document is fine, but compiling the main file with the \bibentry in the subfile fails and I receive the error

[! LaTeX Error: Cannot be used in preamble.]

Any advice would be welcomed. I'm not sure where to start with my troubleshooting.

Thanks.

The following files provide a minimal working example:


File Name: MainDocument.tex

\documentclass{report}
\usepackage[margin=0.5in]{geometry}

%For inclusion of subfiles
    \usepackage{subfiles}

%Path to the directory containing: "MainDocument.tex", "SubfileDocument.tex", and "Bibliography.bib"
        \newcommand{\Troubleshooting}{Path to Troubleshooting Directory/MWE_SubfilesWithBibEntries/}

%%Bibliography
%%       %Allows display of a full bib entry using the command \bibentry{citationkey}
       \usepackage{bibentry}
       \nobibliography*

        \newcommand{\Bibliography}{Bibliography.bib}
        \def\biblio{\bibliographystyle{siam}\bibliography{\Troubleshooting\Bibliography}}

%Document Information
\title{This Is a Document}

\begin{document}
\chapter{First}\newpage
\subfile{\Troubleshooting SubfileDocument.tex}\newpage
\bibliographystyle{siam}
\bibliography{\Troubleshooting\Bibliography}
\end{document}

File Name: SubfileDocument.tex

\documentclass[Path to to troubleshooting directory /MWE_SubfilesWithBibEntries/MainDocument.tex]{subfiles}

\begin{document}
\section{first Section}
Interesting words here that describe something cool.\\
Print text of relevant source:\\
\bibentry{testcitation}\\
Cite relevant source \cite{testcitation}
\biblio
\end{document} 

File Name: Bibliography.bib

@article{testcitation,
abstract = {Words and more word},
author = {John Doe},
doi = {},
file = {},
journal = {Journal of Amazing Things},
keywords = {},
mendeley-tags = {},
month = {aug},
number = {8},
pages = {1000-1005},
publisher = {Publisher of Cool Things},
title = {{Article of Cool Things}},
url = {},
volume = {7},
year = {1995}
}
Laura S.
  • 121

1 Answers1

5

Short answer: yes, there are incompatible. The problem is a bit tricky. The command \bibentry uses \nocite which should not be used in the preamble. The problem actually has nothing to do with bibentry. You would have the same problem just using \nocite in the subfile.

Normally, when latex encounters \begin{document}, it first executes \document and then changes its meaning into \@onlypreamble.

The \nocite command first checks whether the value of \document is \@onlypreamble. If not, it is still in the preamble and issues an error message.

Now, the \subfile command changes \document to do nothing and inputs your subfile.

When \bibentry is encountered is the subfile, it executes \nocite but since the value of \document is not \@nopreamble it deduces that it is still in the preamble hence the error.

Now, how to correct this? The package subfiles actually only contains

\newcommand{\skip@preamble}{%
  \let\document\relax\let\enddocument\relax%
  \newenvironment{document}{}{}%
  \renewcommand{\documentclass}[2][subfiles]{}}
\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}

Instead of loading the package subfiles, just put in the preamble of you main document

\makeatletter
\newcommand\subfile[1]{%
  \begingroup
  \renewcommand{\documentclass}[2][subfiles]{}%
  \renewenvironment{document}{\let\document\@onlypreamble}{}%
  \input{#1}%
  \endgroup}
\makeatother

If you want to be able to use it as well in the subfiles, put this code in a file mysubfiles.sty and load mysubfiles instead of subfiles. This way, latex will not load the package twice. Otherwise, you would get an error when you try to redefine \subfile in the subfiles because it is already defined in the main file.

% mysubfiles.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mysubfiles}

\newcommand\subfile[1]{%
  \begingroup
  \renewcommand{\documentclass}[2][subfiles]{}%
  \renewenvironment{document}{\let\document\@onlypreamble}{}%
  \input{#1}%
  \endgroup}