[Tex/LaTex] How to implement a command that checks for loaded packages

conditionalspackagesprogramming

I use the following code to check if a package is loaded:

\RequirePackage{ltxcmds}
\newcommand{\IfPackageLoaded}[2]{\ltx@ifpackageloaded{#1}{#2}{}}

However, sometimes I want to check if more than one package is loaded. Therefore I would like to generalise the command, such that more than one package can be added in a list separated with commas:

\IfPackagesLoaded{xcolor, colortbl, hyperref}{
...
}%

What would the typical implementation of the processing of such a list look like?

Best Answer

You can use the LaTeX kernel's \@for macro, which processes a comma-separated list, in conjunction with an if switch. The \IfPackagesLoaded macro initially sets the switch to true, then goes through the list and sets the switch to false if a package is missing.

\documentclass{article}

\usepackage{ltxcmds}

\usepackage{fixltx2e}
\usepackage{doc}

\makeatletter
\newif\ifallloaded
\newcommand{\IfPackagesLoaded}[3]{%
  \allloadedtrue
  \@for\@tempa:=#1\do{%
    \ltx@ifpackageloaded{\@tempa}{}{\allloadedfalse}}%
  \ifallloaded #2\else #3\fi}
\makeatother

\begin{document}

\IfPackagesLoaded{fixltx2e,doc}{yes}{no}
\IfPackagesLoaded{hyperref,fixltx2e}{yes}{no}

\end{document}
Related Question