# Problem when using \nocite together with the standalone package

Given:

\documentclass{minimal}
\usepackage{standalone}
\begin{document}
\input{inclusion}
\nocite{*}
\bibliographystyle{plain}
\bibliography{test}
\end{document}


With:

\documentclass{minimal}
\begin{document}
Hello World!
\end{document}


And:

@book{Bateman.1906,
author = {Bateman, Newton and Selby, Paul and Short, William F.},
year = {1906},
title = {Historical encyclopedia of Illinois},
publisher = {Munsell Pub. Co.}
}

@book{Beckett.1966,
author = {Beckett, J. C.},
year = {1966},
title = {The making of modern Ireland, 1603-1623},
publisher = {Faber}
}


I get the following error:

! LaTeX Error: Cannot be used in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.5 \nocite{*}
?


Yet if I comment out either \input{inclusion} or \nocite{*} the test compiles without error. I'm running a command line of latexmk -xelatex test on Win7 Ultimate. TeX courtesy of MikTeX.

So either I'm doing something egregious or there is something wrong with standalone? Suggestions and brickbats welcome.

-
Could you explain what do you want to obtain with this code? What's in inclusion file? Why do you want to use standalone? –  Ignasi Mar 13 at 8:24
@Ignasi In posted text the second block is the inclusion file. This is a minimal example of the problem. I use standalone because I have >40 TikZ files that need to be developed independently and this approach allows me to be lazy with regards their development. Or to give myself the benefit, perhaps more efficient rather than lazy... –  hsmyers Mar 13 at 14:45
This is related to Use of apacite package with subfiles package or standalone package results in LaTeX Error: Can be used only in preamble and is because the preamble test inside \nocite gets confused by the changes done by standalone. The solution I posted there should also work with the standard \nocite, not just the one of the apacite package. I might had a fix for this to standalone itself, maybe as an option. –  Martin Scharrer Mar 13 at 23:03

This is due to how \nocite is defined:

% latex.ltx, line 6146:
\def\nocite#1{\@bsphack
\ifx\@onlypreamble\document
\@for\@citeb:=#1\do{%
\edef\@citeb{\expandafter\@firstofone\@citeb}%
\if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
\@ifundefined{b@\@citeb}{\G@refundefinedtrue
\@latex@warning{Citation \@citeb' undefined}}{}}%
\else
\@latex@error{Cannot be used in preamble}\@eha
% \AtBeginDocument{\nocite{#1}}
\fi
\@esphack}


and the fact that standalone redefines \document to be the same as \sa@document, which is reasonable from the point of view of this package, but has the effect of breaking \nocite because the

\ifx\@onlypreamble\document


test will return false.

You can fix the problem by providing a test free version of \nocite at begin document:

\documentclass{article}
\usepackage{standalone}

\makeatletter
\def\@documentnocite#1{\@bsphack
\@for\@citeb:=#1\do{%
\edef\@citeb{\expandafter\@firstofone\@citeb}%
\if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
\@ifundefined{b@\@citeb}{\G@refundefinedtrue
\@latex@warning{Citation \@citeb' undefined}}{}}%
\@esphack}
\AtBeginDocument{\let\nocite\@documentnocite}
\makeatother

\begin{document}
\nocite{*}
\input{myersinput}
\nocite{*}
\bibliographystyle{plain}
\bibliography{myersbib}
\end{document}


A simpler code uses etoolbox:

\documentclass{article}
\usepackage{standalone}

\usepackage{etoolbox}
\makeatletter
\AtBeginDocument{
\patchcmd{\nocite}
{\ifx\@onlypreamble\document}
{\iftrue}
{}
{\@latex@warning@no@line{\string\nocite\space changed, no patch possible}}
}
\makeatother

\begin{document}
\input{myersinput}
\nocite{*}
\bibliographystyle{plain}
\bibliography{myersbib}
\end{document}

-
Great! I'll re-write and test and mod this to answer accordingly. –  hsmyers Mar 13 at 14:46
Tested! I award you the cake and eat it to honor :) –  hsmyers Mar 13 at 14:53
@hsmyers Send me a slice! –  egreg Mar 13 at 15:12
Virtually speaking---done deal :) –  hsmyers Mar 13 at 21:23