Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 …

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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}
share|improve this answer
add comment

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} 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.