3

Im developing a package an use keyreader to define it’s options, but I fail to set a font as an option.

\begin{filecontents}{myfonts.sty}
\ProvidesPackage{myfonts}

\usepackage{keyreader}

\krddefinekeys[TW]{fam}[my]{%
   cmd/font/\sffamily/;
}

\krdProcessOptions*[TW]<fam>\relax
\end{filecontents}

\documentclass{article}

% works
\newcommand{\mymacro}{xx}% [L1]
% doesn’t work
\newcommand{\mymacro}{\ttfamily}% [L2]
\usepackage[font=\mymacro]{myfonts}% [L1,2]

% doesn’t work
\usepackage[font=\ttfamily]{myfonts}% [L3]

% works
%\krdsetkeys[TW]{fam}{font=\ttfamily}% [L4]

\begin{document}
\myfont Test
\end{document}

I can change the font with \setkeys [L4] but not as a package option [L3]. In general giving macros as values work [L1] but not if theses macros are or contain font switches [L2]. How can I fix this?

If I try to set the font as option pdflatex hangs. In the real package I get the TeX capacity exceeds error.


BTW: \krdProcessOptions drops an error if i omit the prefix [TW] in all macros …

Tobi
  • 56,353

3 Answers3

3

A possible LaTeX3 implementation, that shows also how to set a default. The options should be passed without the backslash, in order to avoid the problem already mentioned by Ulrike. Thanks to Marco Daniel and Joseph Wright for pointing to \ProcessKeysOptions.

Package myfonts.sty

\RequirePackage{expl3,l3keys2e}
\ProvidesExplPackage {myfonts} {2012/03/05} {1}{}
\keys_define:nn { myfonts }
  {
   font .code:n    = \cs_set_eq:Nc \myfont { #1 },
   font .default:n = sffamily,
  }
\keys_set:nn { myfonts } { font }
\ProcessKeysOptions{myfonts}

Sample document

\documentclass{article}

\usepackage[font=ttfamily]{myfonts}

\begin{document}
\myfont Test
\end{document}
egreg
  • 1,121,712
1

You need to load xkvltxp if you want to use commands as values. See the documentation of xkeyval for details. You can see the difference if let you show the meaning of \myfont:

\usepackage{xkvltxp}
\usepackage[font=\ttfamily]{myfonts}% [L3]
\show\myfont

But I would avoid to define package options which need commands as values. Better move this keys to a setup command issued later.

Ulrike Fischer
  • 327,261
1

Question: \krdProcessOptions drops an error if i omit the prefix [TW] in all macros.

Answer: Upgrade your version of keyreader package. The latest version is available at http://texcatalogue.sarovar.org/entries/keyreader.html.

Question: In general giving macros as values work [L1] but not if theses macros are or contain font switches [L2]. How can I fix this?

Answer: Ulrike Fischer is right. You can stick with xkvltxp package but catoptions package also works. Both xkvltxp and catoptions can be loaded before \documentclass.

\begin{filecontents}{testpackage.sty}
\ProvidesPackage{testpackage}
\usepackage{keyreader}
\krddefinekeys{fam}[my]{%
   cmd/font/\sffamily/;
}
\krdProcessOptions*<fam>\relax
\end{filecontents}

\documentclass{article}
\usepackage{catoptions}
\usepackage{xcolor}
% works:
\newcommand{\mymacro}{\ttfamily\color{red}}
\usepackage[font=\mymacro]{testpackage}
% works:
%\usepackage[font=\ttfamily]{testpackage}
% works:
%\krdsetkeys{fam}{font=\ttfamily}

\begin{document}
{\myfont Test} document.
\end{document} 
Ahmed Musa
  • 11,742