Is there a way to find out the correct order to load packages

feynmanincompatibilitynicematrixpackages

I recently made some extension to an old document of mine, and it stopped compiling. Luckily I am aware of the package ordering issue, especially with the notorious hyperref package. However, I am not aware of a way to systematically find the correct order of the packages besides trial and error. Is there a systematic way to determine the correct package loading order besides trial and error?

For example, when I use both the feyn and the nicematrix package, then the order does not matter. However, if I additionally load the amsmath package first, then I need to load the nicematrix package before the feyn package. I did not find anything in the documentation of either package regarding the loading order 🙁

Example for anyone to try:

\documentclass{article}
\usepackage{amsmath}
\usepackage{feyn}
\usepackage{nicematrix}   % should come before the feyn package
\begin{document}
\begin{equation}
\begin{pNiceArray}{cc}[first-row,first-col]
 & 1 & 2 \\
3 & a & b \\
4 & c & d
\end{pNiceArray}
\end{equation}
\begin{equation}
\Diagram{\vertexlabel^a \\
fd \\
& g\vertexlabel_{\mu,c} \\
\vertexlabel_b fu\\
}
= ig\gamma_\mu (T^c)_{ab}
\end{equation}
\end{document}

Best Answer

There can be no general rule here and nothing you can expect to find in the individual package documenation, if package A sets \foo=1 and packageB sets \foo=2 then the load order depends on whether you want \foo to be 1 or 2, or if packageA requires \foo to be 1 and packageB requires it to be 2 then the packages are incompatible and can't be loaded together unless you patch one of them to use a different name.

There are thousands of packages on ctan, individual packages may test against the main core packages such as amsmath but will not in general test against all combinations of contributed packages.

In recent latex releases we have added the hook mechanism that allows you to hook in to package loading and control the order in which hooks are run independent of package loading order. This can address some issues here but the general issue is not solvable.


However the case you give here is documented by thefeyn package. That package somewhat bravely makes ! active so it will be incompatible with lots of things, however it provides an option not to do that. If you use

\usepackage[noglobalbang]{feyn}

there is no error.


Package clashes can only be avoided if you report them to the maintainer and they decide to take special action to avoid known issues.

here feynwould be better to use

\edef!{\string!}

rather than

\def!{\char`\!}               % ! produces this character everywhere

to define its "normal" !as \char`\! only produces a !in typesetting contexts, eg try \typeout{hello!}

Similarly nicematrixcould (probably) locally ensure the catcode of known active punctuation characters and so defend itself against packages that make characters active already in the preamble. (Although really it shouldn't have to do that.)

Related Question