mhelvens / latex-pkgloader

a LaTeX package for managing the options and loading order of other packages
LaTeX Project Public License v1.3c
33 stars 3 forks source link

Add `ExecuteAfterPackageLoaded` environment. #8

Closed koppor closed 6 years ago

koppor commented 8 years ago

In our computer science template, we not only load packages, but also configure them after loading.

Example:

%for theorems, replacement for amsthm
\usepackage[amsmath,hyperref]{ntheorem}
\theorempreskipamount 2ex plus1ex minus0.5ex
\theorempostskipamount 2ex plus1ex minus0.5ex
\theoremstyle{break}
\newtheorem{definition}{Definition}[section]

When directly using \RequirePackage{pkgloader}, these commands fail as the package does not get loaded immediatly.

One possiblity is to use \LoadPackagesNow and issue all configurations afterwards. The drawback is that the usepackage commands and the configurations are then split away. My goal is to keep them together.

Therefore, I'd like to have a \ExecuteAfterPackageLoaded environment, which executes the nested commands directly after the given package is loaded.

Example:

\begin{ExecuteAfterPackageLoaded}[ntheroem]
\theorempreskipamount 2ex plus1ex minus0.5ex
\theorempostskipamount 2ex plus1ex minus0.5ex
\theoremstyle{break}
\newtheorem{definition}{Definition}[section]
\end{ExecuteAfterPackageLoaded}
vermiculus commented 8 years ago

Check out afterpackage.

(Also, you shouldn't be using \<mylength>amount blah blah blah -- use LaTeX syntax instead :smile:)

koppor commented 8 years ago

Thank you for the pointer. Maybe, you could include that in the documentation? That will help users not knowing all packages :innocent:

Some code snippets are just copy and paste from the internet... There are so many things going wrong out there.

duty calls https://xkcd.com/386/

vermiculus commented 8 years ago

Well, we'll have to leave the decision of what to add to the package or its documentation to @mhelvens 😄

"So many things going wrong out there" – you're telling me… It's a major problem we're trying to address as a community: see johannesbottcher/templateConfusion. Unfortunately I haven't been involved in the conversation lately (work has gotten a little crazy), but I imagine it's still going on. I'd pop into the TeX.SX chatroom and ask if you're interested in recent efforts.

mhelvens commented 8 years ago

Hi guys! Yes, afterpackage could be used for this. But pkgloader itself can be used for this as well:

\RequirePackage {pkgloader}
    \usepackage[amsmath,hyperref]{ntheorem}
    \begin{filecontents}{ntheorem-config.sty}
        \theorempreskipamount 2ex plus1ex minus0.5ex
        \theorempostskipamount 2ex plus1ex minus0.5ex
        \theoremstyle{break}
        \newtheorem{definition}{Definition}[section]
    \end{filecontents}
    \Load {ntheorem-config} always after {ntheorem}
\LoadPackagesNow

Sure, there is the downside of having to come up with a redundant file name, but this is still conceptually more elegant, because your configuration code now has its own place in the dependency graph, and will be treated as such.

Would you try this and let me know if it works? I'm not in a good position to test it myself at the moment.

vermiculus commented 6 years ago

That's super clever.