3

I would like to define options for a package using pgfopts. So far it works quite well, but I can't figure out how to execute default options. This is described here: How do you define a default choice in pgfkeys but for some reason it does not work in my test package.

Let the package be

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpack}[2019/10/01 v0.1]

\RequirePackage{pgfopts}
\pgfkeys{
  /testpack/.cd,
  lastword/.is choice,
  lastword/yes/.code=\AtEndDocument{\par Yes last word.},
  lastword/no/.code=\AtEndDocument{\par No last word.},
  lastword/.default=no
}
\ProcessPgfPackageOptions*

and the test document

\documentclass{article}

%\usepackage[lastword=yes]{testpack}
%\usepackage[lastword=no]{testpack}
\usepackage{testpack}

\begin{document}
Here is the text.
\end{document}

then the first two load options give the desired behavior. However, when loading testpack without any options no code is executed at all. I assumed that the .default statement does this trick, but apparently it does not.

Any ideas?

3 Answers 3

3

You're looking for .initial. .default sets a default value which is applied if the key is specified but no value is supplied. But .initial doesn't work for subkeys (from the manual "Note that no subkeys are involved."). So the easiest way to set an initial value for your choice key is to use lastword=no. But bear in mind that since this is code it inserts an \AtEndDocument each time it is called, so you'd get \par No last word.\par Yes last word. if you'd then use the option lastword=yes later on.

\documentclass{article}

\begin{filecontents*}{testpack.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpack}[2019/10/01 v0.1]

\RequirePackage{pgfopts}
\pgfkeys{
  /testpack/.cd,
  lastword/.is choice,
  lastword/yes/.code=\AtEndDocument{\par Yes last word.},
  lastword/no/.code=\AtEndDocument{\par No last word.},
  lastword=no
}
\ProcessPgfPackageOptions*
\end{filecontents*}

%\usepackage[lastword=yes]{testpack}
%\usepackage[lastword=no]{testpack}
\usepackage{testpack}

\begin{document}
Here is the text.
\end{document}
2
  • Thanks. Dumb question: subkey meaning that it is in /testpack/lastword?? Would /lastword work with .initial? Commented Dec 12, 2019 at 23:43
  • 2
    @carlosvalderrama if it wasn't a choice key but a key storing information directly it would. .initial doesn't apply to keys which are in testpack/lastword/..., but only to testpack/lastword (as far as I understand from the documentation, I'm not entirely sure whether I got that right).
    – Skillmon
    Commented Dec 12, 2019 at 23:49
0

For what it is worth, I resolved the problem by checking whether any method has been called. If not, the package still needs initialization and there I can call the desired code.

The updated package looks like

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpack}[2019/10/01 v0.1]
%
\newif\if@testpack@needs@init@
\@testpack@needs@init@true
%
\newcommand{\testpack@handle@yes}{%
  \@testpack@needs@init@false
  \AtEndDocument{\par Yes last word.}
}
\newcommand{\testpack@handle@no}{%
  \@testpack@needs@init@false
  \AtEndDocument{\par No last word.}
}
%
\RequirePackage{pgfopts}
\pgfkeys{
  /testpack/.cd,
  lastword/.is choice,
  lastword/yes/.code=\testpack@handle@yes{},
  lastword/no/.code=\testpack@handle@no{},
}
\ProcessPgfPackageOptions*
%
\if@testpack@needs@init@
\testpack@handle@no{}
\fi

The code gives the expected results now. What do you think? Any thoughts on the robustness of this approach?

1
  • 1
    Try with \usepackage[lastword=no,lastword=yes]{testpack}... Commented Dec 13, 2019 at 15:46
0

Here is my solution.

The testpack.sty file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpack}[2019/10/01 v0.1]
% 
% 
\RequirePackage{pgfopts}
\pgfkeys{
  /testpack/.cd,
  lastword/.is choice,
  lastword/yes/.code={
    \def\testpack@at@end@document{\AtEndDocument{\par Yes last word.}}
  },
  lastword/no/.code={
    \def\testpack@at@end@document{\AtEndDocument{\par No last word.}}
  },
  lastword=no,
}
\ProcessPgfPackageOptions*
\testpack@at@end@document

Then, a test file:

\documentclass{article}
\usepackage[lastword=yes,lastword=no]{testpack}

\begin{document}
AAA
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .