RequirePackage and usepackage behave differently

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}}
{%
...
\PassOptionsToClass{\CurrentOption}{exam}%pass option to exam
}
\ProcessOptions
``````

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?

-
You should edit your question to add a stripped-down version of your custom class that reproduces the problem when used in a document. –  Jubobs Jun 15 '14 at 15:25
Where did you put your `\RequirePackage` lines? They should be after `\LoadClass`. If you put `\RequirePackage{probsoln}` before `\LoadClass{exam}` you'll get the `Command \solution already defined` error. I don't know what's causing the other errors. As Jubobs commented, we'd need to see a complete minimal class to help further. –  Nicola Talbot Jun 15 '14 at 15:29
@Jubobs: The difference between `\RequirePackage` and `\usepackage` is irrelevant here, the order of the loaded classes and packages matters. A MWE would show that order. –  Heiko Oberdiek Jun 15 '14 at 15:29
A test file seems not to be necessary as moving the `RequirePackage` after the `LoadClass` command solved the problem. Thank you very much for the input. But there needs to be more to it as I am not allowed to put all `RequirePackage` commands after the `LoadClass` part. What is the logic behind it? –  Ulrich Hauser-Ehninger Jun 15 '14 at 15:44
@UlrichHauser-Ehninger Recommended reading: latex-project.org/guides/clsguide.pdf –  Jubobs Jun 15 '14 at 15:45

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`.
Thank you for this answer. This really helps an sheds some light on the order problem. So, if `exam` would test for the `solution` environment as well it would always work, but -depending on the calling order- would provide different definitions for the `solution`environment? –  Ulrich Hauser-Ehninger Jun 15 '14 at 16:01
Yes, it would result in different definitions for the `solution` environment, which may cause unexpected results. In general, classes assume they're starting with a "clean plate". That is, they just have the kernel commands defined. –  Nicola Talbot Jun 15 '14 at 17:14