[Tex/LaTex] RequirePackage and usepackage behave differently

errorslistingspackagesprobsoln

I design exams which should contain code samples using pdflatex. In lecture scripts I successfully use the listings package. The general design is done via the exam document class.

I created my own class file expanding the exam class file and channeling down the options to the exam class.

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{exam}}
\DeclareOption{answers}%
    {%
        ...
        \PassOptionsToClass{\CurrentOption}{exam}%pass option to exam
    }
\ProcessOptions
\LoadClass[a4paper,12pt,oneside,openany,\droptnm]{exam}

This class defines a special predefined format for the listings and deals with problems.
Therefore, my 'private' exam class would like to use

\RequirePackage{listings}
\RequirePackage{probsoln}

This, unfortunately, causes error messages beginning with:

! LaTeX Error: Command \abovecaptionskip already defined.
...
! LaTeX Error: Command \belowcaptionskip already defined.
...
! LaTeX Error: Command \solution already defined.
...

The following code in the main exam document works:

\usepackage{listings}
\usepackage{probsoln}

When I look up information sources I gather the information that RequirePackage and usepackage are identical with the difference that RequirePackage is allowed before documentclass but usepackage is not.

Anybody around to enlighten me why RequirePackage does not work in the class file here?

Best Answer

One of the issues here is that both probsoln and exam define the solution environment. However, probsoln will only define it if it doesn't already exist. If you load probsoln first, it will define the solution environment, because it's undefined. Then you load exam, which also tries to define the solution environment, and this is what causes the error Command \solution already defined.

Where you put \RequirePackage depends on when you need the commands that are defined in that package. If, say, your class options need to use xkeyval (or something similar) you'd need to load that package before the options, but if the basic class commands need to be defined before the commands in any included packages, then those packages need to be loaded after \LoadClass.