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}

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


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

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

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

  \@ifpackageloaded{OtherPackage}% Fill in name here
    {\my@check@OtherPackage@loadedtrue}% Nothing to say

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

| improve this answer | |

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.