8

Is it possible to emit a warning if a package hasn't been loaded?

If possible, I would prefer that the solution not require \usepackage{ifthen} as it couldn't emit the warning that it was missing the ifthen package.

EDIT: Thanks Martin and Joseph for the responses. I would like to clarify my question: is it possible do to this from within the body of a document?

The \AtBeginDocument, \RequirePackage and \@ifpackageloaded are only available in the preamble. The ltxcmds package makes \ltx@ifpackageloaded available in the body but then the ltxcmds package is required for this to work.

The situation where I would like this is I am generating tables with the xtable package in R and when the floating.environment='sidewaystable' argument is used, the rotating package is required. To make this easy for users, I would like to allow them to simply \input{<path_to_xtable>}. However, if the rotating package isn't loaded pdflatex give an error message like

l.3 \begin{sidewaystable}
                         [ht]
?

I would prefer that it issue a more informative warning that the rotating package is missing. Is this possible?

11

Most of the time you don't test in this way but simply require those packages you need, but of course there are cases where load-order is awkward and a test is useful. The standard way to do this would be to test at the start of the document

\AtBeginDocument{%
  \@ifpackageloaded{OtherPackage}% Fill in name here
    {}% Nothing to say
    {%
      \PackageWarning{MyPackage}
         {You have not loaded the 'OtherPackage' package!}%
    }%
}

To allow a check in the document body, the usual way is to define a conditional

\newif\ifmy@check@OtherPackage@loaded
\AtBeginDocument{%
  \@ifpackageloaded{OtherPackage}% Fill in name here
    {\my@check@OtherPackage@loadedtrue}% Nothing to say
    {\my@check@OtherPackage@loadedfalse}%
}

and then to use that conditional to check for the package being available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.