Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  Martin Scharrer Jan 18 '13 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. –  Martin Scharrer Jan 18 '13 at 19:25
add comment

1 Answer

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!

share|improve this answer
    
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. –  Martin Scharrer Jan 18 '13 at 20:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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