# Font size as a key-value pair using pgfkeys does not work

I want to define a new environment whose contents can be typeset using a specific font family and size optionally specified as key-value pairs. For handling the key-value pairs I am using pgfkeys. Why is LaTeX not accepting passing the font size as a key-value pair like fontsize=\large, though it has no problem with fontfamily=\sffamily? How to fix this?

Here is a minimal example:

\documentclass{minimal}
\usepackage{lipsum}
\usepackage{pgfkeys}

\pgfkeys{
/myenv/.cd,
fontfamily/.store in = \myenvfontfamily,
fontsize/.store in = \myenvfontsize,
}

\newenvironment{myenv}[1][]{%
\pgfqkeys{/myenv}{#1}%
\ifdefined\myenvfontfamily\myenvfontfamily\fi
\ifdefined\myenvfontsize\myenvfontsize\fi
}{%
}%

\begin{document}
\lipsum[1]
\begin{myenv}[fontfamily=\sffamily,fontsize=\large]
\lipsum[2]
\end{myenv}
\lipsum[3]
\end{document}


pdflatex fails with the message:

! Undefined control sequence.
\myenvfontsize ->\large

l.24 ...env}[fontfamily=\sffamily,fontsize=\large]


I want to define an environment where the user should be able to set the font using key-value pairs, much like the Verbatim environment from the fancyvrb package (although font setting is not the primary goal of the environment).

-
My first guess is maybe it's being expanded when it shouldn't be? –  Seamus Dec 21 '11 at 15:03

You code compiles (and seems to work) if you use \documentclass{article}: The minimal class has no \large.

Note that you could improve your code by means of

\pgfkeys{
/myenv/.cd,
fontfamily/.store in = \myenvfontfamily,
fontsize/.store in = \myenvfontsize,
fontfamily=,
fontsize=,
}


in which case you could omit the \ifdefined -- simply write \myenvfontfamily. In the default case it would expand to the empty string which has no effect. This would allow your users to reset the style effectively.

Note furthermore, that you could benefit from pgfkeys' builtin variables, i.e. by means of

\pgfkeys{
/myenv/.cd,
fontfamily/.initial=,
fontsize/.initial=,
}


combined with .... \pgfkeysgetvalue{/myenv/fontfamily}\temp ... \temp: this would allow you to write

\pgfkeys{/myenv/fontfamily/.add={}{\large}}


the /.add={prefix}{suffix} allows to modify an existing variable rather than replacing it.

-