add tag
निरंजन
I recently uploaded v0.3 of package [`marathi`](https://ctan.org/pkg/marathi) in which I have used the command `\@ifpackageloaded` for checking if package `csquotes` is loaded or not. In the source file of the package that particular code can be seen on [line no. 404-409](https://gitlab.com/niruvt/marathi/-/blob/master/marathi/marathi.dtx#L404-409). I expect the following code to work now, but it seems not to work. (Thanks @samcarter for a slightly better MWE.)

```
\documentclass{article}
\makeatletter
\AtBeginDocument{%
  \@ifpackageloaded{csquotes}{}{%
    \RequirePackage[style=british]{csquotes}%
  }%
}%
\makeatother

\begin{document}
\enquote{text}
\end{document}  
```

What is exactly wrong? My code or my expectation? :joy:
Top Answer
samcarter
`csquotes` is one of the packages which need to be loaded before `\AtBeginDocument`. Instead you could use the `etoolbox` package and load it at the end of the preamble (csquotes would anyway load etoolbox, so no real disadvantage from loading it):

```
\documentclass{article}

\usepackage{etoolbox}

\makeatletter
\AtEndPreamble
{%
  \@ifpackageloaded{csquotes}{}{%
    \RequirePackage[style=british]{csquotes}%
  }%
}%
\makeatother


\begin{document}
\enquote{text}
\end{document}
```

or for usage in your package:

```
\RequirePackage{etoolbox}
\AtEndPreamble
{%
  \@ifpackageloaded{csquotes}{}{%
    \RequirePackage[style=british]{csquotes}%
  }%
}%
```
Answer #2
Skillmon
With a recent LaTeX version we got hooks built into LaTeX. This interface is accessible via the macro `\AddToHook` (which current versions of `etoolbox` will use as well).

The hooks allow some ordering by rules (to get better compatibility between packages independent on load-order for example), but most of the time it is enough to use the correct hook with `\AddToHook`.

To use some code at the end of the preamble (but before the real effects of `\begin{document}` take action) one can use the hook `begindocument/before`.

```
\documentclass[]{article}

\makeatletter
\AddToHook{begindocument/before}
  {\@ifpackageloaded{csquotes}{}{\RequirePackage[style=british]{csquotes}}}
\makeatother

\begin{document}
\enquote{foobar}
\end{document}
```

With this interface there are three spots to hook into during `\begin{document}`, those are:

- `begindocument/before`: this is at the end of the preamble (but still part of the preamble)
- `begindocument`: this is inside `\begin{document}` (don't load packages here, and don't include typeset material here, other setup code is fine)
- `begindocument/after`: this is right at the start of the document (put typeset material here)

The documentation of the new hook mechanism can be accesed via `texdoc lthooks-doc`. However there are a few more hooks which are documented elsewhere:

- `texdoc ltfilehook-doc` (hooks for files)
- `texdoc ltshipout-doc` (hooks for the shipout routine)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.