3

How do I allow someone using my custom document class to \usepackage some library that I've already \RequirePackage'd with specific options? Optimally, when they call \usepackage, it would operate as if it's importing the package afresh, using only the arguments they supply (potentially none).

For example, I want to provide only \llbracket and \rrbracket from the stmaryrd package by default, but allow the user to import the rest of stmaryrd in their document if they wish.

Here's my class stmaryrdsubset.cls:

\ProvidesClass{stmaryrdsubset}[2017/03/14 minimal with llbracket+rrbracket from stmaryrd]
\LoadClass{minimal}
\RequirePackage[only,llbracket,rrbracket]{stmaryrd}

That works great in providing the bracket commands, but when a document (stmaryrdfull.tex) tries to re-use that package, nothing happens:

\documentclass{stmaryrdsubset}
\usepackage{stmaryrd}
\begin{document}
Yay for $\llbracket\textrm{double brackets}\rrbracket$

This is a cool symbol: $\bindnasrepma$ (it's like an upside-down ampersand!)
\end{document}

Which produces the error:

! Undefined control sequence. \bindnasrepma

And the incomplete result:

result of compiling stmaryrdfull.tex

(The cool symbol, bindnasrepma snippet, is missing)

I suspect one hack/solution would be to postpone requiring that package until \AtBeginDocument or thereabouts, to give the user a chance to \usepackage{stmaryrd} in their preamble first, and then call my own \RequirePackage[only,...]{stmaryrd} at the last minute. Mayhap there is a cleaner, more straightforward way?

  • The normal solution is to provide an option in the class (e.g., \documentclass[nobrack]{stmaryrdsubset}, which then does not load the package, so people can then load the package. Another option is to adopt memoir's \EmulatedPackage/DisemulatePackage, which does (IIRC) the opposite of of what you want. But you cannot, as far as I know, load-then-unload a package. – jon Mar 15 '17 at 3:12
  • No 'unloading' of packages: tex.stackexchange.com/a/34339/8528; generic examples of memoir's 'trick': tex.stackexchange.com/q/39415/8528 – jon Mar 15 '17 at 3:17
  • Why on Earth or Jupiter are you basing a class on minimal? On no account should you do this. It is not meant for typesetting documents! – cfr Mar 15 '17 at 3:43
  • 1
    minimal is not suitable for minimal examples. – cfr Mar 15 '17 at 4:05
  • 1
    Note that nothing (usually) prevents you from taking the code of a package and including it in your own class (after, ideally, renaming it -- especially if it's for 'internal' use). If you are smart about how you use (say) \providecommand, you should have no problems if people then go on to load the package. As for the minimal class, it's not meant for 'minimal' examples: best to stick with article, report, or book for most MWEs. – jon Mar 15 '17 at 4:10
5

There is no general way to do this.

Normally latex detects the package has been loaded and prevents loading a second time. You can turn off that check by undefining the macro that records the previous load.

Add

\expandafter\let\csname ver@stmaryrd.sty\endcsname\relax

to the end of your class and you will see on the terminal

..
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
) (/usr/local/texlive/2016/texmf-dist/tex/latex/stmaryrd/stmaryrd.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/stmaryrd/stmaryrd.sty)

so the package got loaded twice.

fontenc does this so you can use

\usepackage[T1]{fontenc}

and later

\usepackage[T2]{fontenc}

but the entire font declaration mechanism is written to support being re-run.

If you run your example with the stmaryrd package loaded twice you get exactly the same error because all the internal settings set by the original only option are still in force, so essentially it does nothing on the second load.

You could undo those switches, but then, most likely you will actually specify two math fonts both using the same underlying font. As classic tex only allows 16 math fonts in any expression that is not a good plan. You want to use the existing already allocated math fam, and the package has no code to do that so basically you would need to re-write the entire package.

Better plan would be for the class to have options to use just those characters or all of them, and then load stmaryrd in different ways depending on the document class options used.

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.