5

I'm trying to create a package with three options as follows:

\usepackage[swpl]{mypack}

swpl option provides the environment

\begin{myexample}[forced bracketed options]...\end{myexample}

available for all xelatex/latex

\usepackage[tcb]{mypack}

tcb option provides the same environment name (with different definition)

\begin{myexample}[other forced bracketed options]...\end{myexample}

available for latex/xelatex and the third option:

\usepackage[pdf,swpl]{mypack} or \usepackage[pdf,tcb]{mypack}

provides the same environment name (with other definition) available for (pdf/lua/xe)latex. I'm triying to adapt related answer for this...but I did not succeed. If anyone can help me with a skeleton (using kvoptions or pgfkeys) I'd appreciate it. I was unable to add [requiered] for the myexample environment and boolean option for swpl and tcb.

PD: Using the same environment name because the first option uses showexpl and the second option tcolorbox for verbatim.

2 Answers2

8

From the description I assume that definition of the environment mypack does not need to be changed after the package is loaded. Then two switches and classical options will do:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypack}{2014/04/17 My package}

\newif\ifmypack@swpl % swpl: true, tcb: false
\newif\ifmypack@pdf  % option pdf

\DeclareOption{swpl}{\mypack@swpltrue}
\DeclareOption{tcb}{\mypack@swplfalse}
\DeclareOption{pdf}{\mypack@pdftrue}

\ProcessOptions* % process options in the order they are given

Then the switches \ifmypack@swpl and \ifmypack@pdf can be used for the implementation of environment myexample, e.g:

\ifmypack@swpl
  % code for option "swpl"
\else
  % code for option "tcb"
\fi
Heiko Oberdiek
  • 271,626
4

This is a rewrite of Heiko Oberdiek's answer using the new option processing facilities in recent versions of LaTeX.

\NeedsTeXFormat{LaTeX2e}[2023-01-12]
\ProvidesPackage{mypack}{2023/04/17 My package}

\newif\ifmypack@swpl % swpl: true, tcb: false \newif\ifmypack@pdf % option pdf

\DeclareKeys [mypack] {% swpl.if = mypack@swpl, tcb.ifnot = mypack@swpl, pdf.if = mypack@pdf, swpl.usage = load, tcb.usage = load, pdf.usage = load, } \ProcessKeyOptions [mypack]

cfr
  • 198,882
  • Great answer, can you give an alternative version more expl3 for new users? – Pablo González L Aug 14 '23 at 16:07
  • @PabloGonzálezL Sorry, I'm not sure what you're asking. I deliberately avoided expl3 in this precisely to make it more intelligible to new users! Obviously, if you use expl3, you can use that in conjunction with this. – cfr Aug 15 '23 at 01:30