3

I want to create a multi-file project for my thesis, in which I use separate files for sections. To be able to compile these separate files I tried using the subfiles package and the standalone package.

A problem arises when I use one of these packages in conjunction with the apacite package. Then, when compiling the main.tex file, I get a LaTeX Error: Can be used only in preamble. I don't understand why get I this error. It doesn't matter if the apacite package is loaded after or before subfiles or standalone.

Example of main.tex and chapter1.tex files in case of using subfiles package:

\documentclass{article}
\usepackage{subfiles}
\usepackage{apacite}
\begin{document}
\subfile{chapter1}
\end{document}
\documentclass[main.tex]{subfiles} 
\begin{document}
text
\cite{xx19xx}
\end{document}

Example of main.tex and chapter1.tex files in case of using standalone package:

\documentclass{article}
\usepackage{standalone}
\usepackage{apacite}
\begin{document}
\input{chapter1}
\end{document}
\documentclass{article}
\begin{document}
text
\cite{xx19xx}
\end{document}

In both cases I get the previous mentioned error. Is there a way in which I can use apacite and still use separate files for my project?

3
  • One other way to compile chapters on its own is to include them with \include and then use \includeonly{<chapterfilename>,...} to only include those you want. See e.g. tex.stackexchange.com/q/246/2975 Commented Jan 18, 2013 at 15:40
  • I had a look at this now. The reason is that \cite from apacite checks if it is in the preamble or not by testing the definition of \document (which is the underlying macro behind \begin{document}. However, with both packages the definition of \document got changes, so that the test thinks its still in the preamble and uses a preamble-only macro (\AtBeginDocument) which causes the error. I will code some solution for this and post an answer. Commented Jan 18, 2013 at 19:25
  • See also errors - Problem when using \nocite together with the standalone package - TeX - LaTeX Stack Exchange but there's a difference between whether apacite package is used so I'll leave it. The cause is pretty-much the same though
    – user202729
    Commented Apr 22, 2022 at 16:39

1 Answer 1

2

The reason is that with apacite the \cite macro calls \nocite which checks if it is in the preamble or not by testing the definition of \document (which is the underlying macro behind \begin{document}. However, with both packages the definition of \document got changed, so that the test always thinks its still in the preamble and uses a preamble-only macro (\AtBeginDocument) which causes the error.


A way to avoid this with standalone is to patch \nocite so that it doesn't compare \@onlypreamble with \document but \documentclass with \sa@documentclass. This works because standalone sets \documentclass to \sa@documentclass directly at the very end of the preamble by appending the required code to \document. 1

% Main document
\documentclass{article}
\usepackage{standalone}
\usepackage{apacite}
\usepackage{etoolbox}% for the \patchcmd
\makeatletter
% Patch after apacite got loaded!
\patchcmd{\nocite}{\@onlypreamble\document}{\documentclass\sa@documentclass}{}{}
\makeatother
\begin{document}
\input{chapter1}
\end{document}

For subfiles things are different. Here \document is only redefined inside the subfiles itself, so its not possible to patch the test to compare different macros in a way which works both inside and outside subfiles. Here I would simply replace the test with an always true \ifx and never use \cite or \nocite in the preamble where it doesn't belong anyway. This patch also works for standalone!

\documentclass{article}
\usepackage{subfiles}
\usepackage{apacite}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\nocite}{\ifx\@onlypreamble\document}{\iftrue}{}{}
\makeatother
\begin{document}
\subfile{chapter1}
\end{document}

1) However, the internal test is flawed anyway and should be removed by the apacite author. With and without the patch it calls \AtBeginDocument inside \nocite where \document still has its original definition and so it calls \AtBeginDocument again and then again ... until TeX runs out of resources and aborts!

1
  • I now wrote a report to the author of apacite to inform him about this issue and the broken inside-preamble test. I suggested to remove the test altogether. The original \nocite doesn't work inside the preamble either. Commented Jan 18, 2013 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .