0

I have seen that I cannot require a package twice. How can I have an option that activates the availability of colour names

\RequirePackage[dvipsnames*,svgnames,x11names]{xcolor}

But if the option is missing the style file uses this instead

 \RequirePackage{xcolor}
Veak
  • 1
  • 1
    you have two answers but as already previously explained you would be better to not use the options, or your package will give errors if loaded after xcolor – David Carlisle Oct 07 '23 at 07:37
  • I activate xcolor because I want to use colours in my style file. Is there a more appropriate way to do this ? – Veak Oct 07 '23 at 12:55
  • yes just use \RequirePackage{xcolor} there is no reason to define 100s of colour names you don't use, why are you using those options (it rarely makes sense to load more than one of them in any case) if you have a requirement to stick to the pre-defined svg pallette use svgnames, if you are generating postscript and want to send named rather than cmyk numbered colours to dvips use dvipsnames, if you are targetting xdvi viewer and want X11 colour names use x11names, what's the point of defining them all? but in a package just don't use the options at all. – David Carlisle Oct 07 '23 at 13:21
  • 1
    It's not like you have not been warned of option clashes before https://tex.stackexchange.com/questions/697769/xcolor-tools-with-names-as-an-option#comment1733358_697769 https://tex.stackexchange.com/questions/659029/colour-packages-beyond-xcolor#comment1640024_659029 https://tex.stackexchange.com/questions/660440/definecolors-option-clash-for-package-xcolor#comment1643200_660440 – David Carlisle Oct 07 '23 at 13:30
  • Are you arguing against using the the three of them together or about me calling \RequirePackage{xcolor} ? Why are people warned of option clashes. Are there no internals so people can use options without too much hassle ? – Veak Oct 07 '23 at 13:45
  • 1
    specifically for those options there is not a lot of point in using more than one, and within a package you should mostly avoid forcing options as it restricts the end user from loading the package directly. If you just have RequirePackage{xcolor} you can use colours in your package and the end user can use whatever options to xcolor he or she wants by loading xcolor first with suitable options. – David Carlisle Oct 07 '23 at 13:48
  • 1
    The problem is not lack of internals, it's just a bad design of your package to over-constrain the use of other packages. – David Carlisle Oct 07 '23 at 13:50
  • Suppose I just use \RequirePackage{xcolor} and set up my own colour names. Would there still be a clash when users call \usepackage[svgnames]{xcolor}, then call my package ? That is my difficulty. – Veak Oct 07 '23 at 13:56
  • No that's the whole point, a user could do that (as long as they do it first) or you can use \AtBeginDocument{\RequirePackage{xcolor}} so you just make sure its loaded at that point and the user can load xcolor before or after your package – David Carlisle Oct 07 '23 at 14:01
  • I need to define some colours that I use to make some environments I define. How would your suggestion look like ? – Veak Oct 07 '23 at 14:42
  • \definecolor{package-thmcolor}{rgb}{.5,0,.2} .... \color{package-thmcolor} – David Carlisle Oct 07 '23 at 14:51
  • I still would need \RequirePackage{xcolor}, am I right ? Would I call it under some condition ? – Veak Oct 07 '23 at 14:55
  • If user forgets to declare use of xcolor first, that would mean that my package fails, correct ? Have been trying to avoid that if it makes sense. – Veak Oct 07 '23 at 15:03
  • No it would work, that's why you want Requirepackage{color in your package. – David Carlisle Oct 07 '23 at 15:52
  • Just \Requirepackage{xcolor} but without the options then. – Veak Oct 07 '23 at 16:16

2 Answers2

2

Any number of ways. Use a classic option or pick the key-value interface of your choice. Here's expl3's:

\NeedsTeXFormat{LaTeX2e}[2022-06-01]
\ProvidesPackage{veak}[veak v0.1]
\ExplSyntaxOn
\keys_define:nn { veak }
{
  colour ~ names .bool_set:N = \l_veak_colour_names_bool,
  colour ~ names .default:n = true,
  colour ~ names .initial:n = false,
}
\ProcessKeyOptions { veak }
\bool_if:NTF \l_veak_colour_names_bool
{
  \RequirePackage[svgnames,x11names,dvipsnames]{xcolor}
}{
  \RequirePackage{xcolor}
}
\ExplSyntaxOff
\endinput
cfr
  • 198,882
  • Beat me to it! I was just about to paste the same. (I think you just want veak not veak.sty in the second line, though.) – Alan Munn Oct 07 '23 at 02:12
  • @AlanMunn Sorry about that and thanks. All my files have \ProvidesPackageSVN{}[] so I almost never have to think about a regular first argument and subversion, naturally enough, includes the .sty. – cfr Oct 07 '23 at 02:36
2

One way to do this is to conditionally call the command

\PassOptionsToPackage{dvipsnames*,svgnames,x11names}{xcolor}

before loading the package. It also lets you combine multiple options that add different options to xcolor. This technique is also useful for fixing up an option clash inside a .cls file.

Davislor
  • 44,045