0

The following MCE:

% Subfile
%%%%%%%%%
\begin{filecontents}[overwrite]{subfile.tex}
\documentclass{article}
\tcbuselibrary{documentation}
\begin{document}
Foo.
\end{document}
\end{filecontents}

% Main file
%%%%%%%%%%%
\documentclass{article}
\usepackage{tcolorbox}
\usepackage[subpreambles]{standalone}
%
\tcbuselibrary{documentation} % ← To be removed 
%
\begin{document}
\input{subfile}
\end{document}

compiles like a charm, except if \tcbuselibrary{documentation} is removed from the main file (compile at least twice). The errors are:

! LaTeX hooks Error: Sorting rule for 'begindocument' hook applied too late. (hooks) Try setting this rule earlier.

For immediate help type H . ...

l.421 ...begindocument}{showkeys}{before}{nameref}

and, later:

! Undefined control sequence. \csname tcb@doc@index@\idx@format \endcsname \hypersetup { citeco... l.237 }}

? ! Undefined control sequence. \kvtcb@colhyper

l.237 }}

I'm aware of the Ulrike's warning:

This subpreambles stuff looks quite fragile, e.g. hyperref errors as it is loaded to late, [...]

in standalone package wrongly claims sub-preamble of a subfile changed if used with \file_input:n instead of \input but do you understand what's going on and how to fix this issue?

1
  • 1
    well exactly my comment. nameref contains a rule to sort code in begindocument, and if nameref (or hyperref) is loaded to late it will error because this. Jan 4 at 22:14

1 Answer 1

2

When standalone package option subpreambles is set, sub-preambles are first collected in .sta file, then loaded at begin of document (\AtBeginDocument). But in general loading packages in \AtBeginDocument is not a good idea, see previous discussions in issue latex3/hyperref#242.

I've reported this to standalone (see issue MartinScharrer/standalone#3) and suggested to use \AddToHook{begindocument/before} instead.

Below is a workaround. It uses internal, implementation-specific info that \RemoveFromHook{<hook>}[<label>] will globally store the removed code in \l__hook_return_tl.

% Subfile
%%%%%%%%%
\begin{filecontents}[overwrite]{subfile.tex}
\documentclass{article}
\tcbuselibrary{documentation}
\begin{document}
Foo.
\end{document}
\end{filecontents}

% Main file
%%%%%%%%%%%
\documentclass{article}
\usepackage{tcolorbox}
\usepackage[subpreambles]{standalone}

% move standalone code in hook `begindocument` to `begindocument/before`
\RemoveFromHook{begindocument}[standalone]
\ExpandArgs{nv}\AddToHook{begindocument/before}{l__hook_return_tl}

%
%\tcbuselibrary{documentation} % ← To be removed
%
\begin{document}
\input{subfile}
\end{document}

You must log in to answer this question.

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