1

I'm trying to write an aptly-named package xkeyvalisinsanelydifficult such that the user can \usepackage either as \usepackage{xkeyvalisinsanelydifficult} or \usepackage[turnedon]{xkeyvalisinsanelydifficult}, but (preferably, if possible) not as \usepackage[turnedon=42]{xkeyvalisinsanelydifficult} (just because it wouldn't make much sense in this package's case).

Contents of xkeyvalisinsanelydifficult.sty:

\ProvidesPackage{xkeyvalisinsanelydifficult}[2022/07/22]
\RequirePackage{xkeyval}

\newcommand{\somesimplecommand}{nothing at all} \define@boolkey{myfamily}{turnedon}{} \ProcessOptionsX\relax

\ifKV@myfamily@turnedon \def\somesimplecommand{ON} \else \def\somesimplecommand{OFF} \fi

Contents of sample.tex:

\documentclass{article}
\usepackage{xkeyvalisinsanelydifficult}

\begin{document} Hello, it's turned \somesimplecommand!

\end{document}

When sample.tex 2nd line is \usepackage{xkeyvalisinsanelydifficult}, things go well: the document compiles and reads "Hello, it's turned OFF!" (which is expected because the xkeyval docs state that \define@boolkey initializes a boolean to false, with \newif).

When sample.tex 2nd line is \usepackage[turnedon]{xkeyvalisinsanelydifficult}, the document doesn't compile with an error that reads ERROR: LaTeX Error: Unknown option 'turnedon' for package 'xkeyvalisinsanelydifficult'.

How to rewrite these files such that if the 2nd line of sample.tex is \usepackage[turnedon]{xkeyvalisinsanelydifficult}, it compiles and reads "Hello, it's turned ON!" ?

  • do you have to use xkeyval? With a new latex you could do it without it. – Ulrike Fischer Jul 22 '22 at 22:30
  • @UlrikeFischer I don't have to. I read a post here discussing key-value packages and considered pgfkeys because I know a bit about TiKZ, but it seemed even harder than xkeyval. And I actually managed to get some simple stuff working with xkeyval, but only using \DeclareOptionX macros, never with the \define@ ones... I don't know the other packages; are they easier? – Daniel Diniz Jul 22 '22 at 22:59
  • you don't need any package with current latex see https://tex.stackexchange.com/a/648001/1090 – David Carlisle Jul 22 '22 at 23:53
  • @DavidCarlisle reading some posts here (such as the one you've linked) and the "File V ltkeys.dtx" section of source2e.pdf, I could understand that a key-value capacity has been embedded into the more recent versions of the LaTeX kernel. that's good, but I'd rather use a package such as xkeyval because this feature seems very little documented (understandable, as it's still very new!), and I know nothing about the LaTeX3 syntax it's written in, so I'll be hopeless as soon as I get stuck (which will certainly happen). – Daniel Diniz Jul 23 '22 at 00:32
  • you dont need to use l3 programming for the new keys feature, that's rather the point of it – David Carlisle Jul 23 '22 at 00:35
  • well, yes, but there's still the issue of the amount of documentation available (that is, if I got stuck with the little doc there is, I wouldn't even be able to examine the source, because I don't know L3 syntax) – Daniel Diniz Jul 23 '22 at 00:38
  • The underlying keyval system is not new, only that you can use it with package options is new. It is documented in interface3. It has the advantage that you can avoid option clash errors. – Ulrike Fischer Jul 23 '22 at 06:00
  • 1
    if you define your keys in the family myfamily you must use \ProcessOptionsX<myfamily>\relax to process them (the default family is xkeyvalisinsanelydifficult.sty.) – Ulrike Fischer Jul 23 '22 at 07:25
  • I've managed to make \usepackage[turnedon=true]{xkeyvalisinsanelydifficult} work using \ProcessOptionsX<myfamily>. \usepackage[turnedon]{xkeyvalisinsanelydifficult} behaves like \usepackage{xkeyvalisinsanelydifficult}, so it would be a minor nuisance to the user, to have to type turnedon=true instead of simply turnedon. – Daniel Diniz Jul 23 '22 at 13:10
  • 1
    Use \define@boolkey{myfamily}{turnedon}[true]{} to define a default value "true" (that would not be necessary with l3keys, boolean keys by default assume true if no value is given). – Ulrike Fischer Jul 23 '22 at 14:56

1 Answers1

0

Change the contents of xkeyvalisinsanelydifficult.sty to:

\ProvidesPackage{xkeyvalisinsanelydifficult}[2022/07/22]
\RequirePackage{xkeyval}

\newcommand{\somesimplecommand}{nothing at all} \define@boolkey{myfamily}{turnedon}[true]{} \ProcessOptionsX<myfamily>\relax

\ifKV@myfamily@turnedon \def\somesimplecommand{ON} \else \def\somesimplecommand{OFF} \fi

This will give turnedon the desired behavior:

  • \usepackage{xkeyvalisinsanelydifficult}: false
  • \usepackage[turnedon=false]{xkeyvalisinsanelydifficult}: false
  • \usepackage[turnedon]{xkeyvalisinsanelydifficult}: true
  • \usepackage[turnedon=true]{xkeyvalisinsanelydifficult}: true
  • \usepackage[turnedon=X]{xkeyvalisinsanelydifficult}, where X is anything other than true or false: ERROR: Package xkeyval Error: value 'X' is not allowed.