# Bad interaction between keycommand and environ?

I suspect the keycommand and environ packages are not playing nicely together, because the following code gives an error. Can anybody explain what's going on?

\documentclass{article}

\usepackage{environ}
\usepackage{keycommand}
\usepackage{graphicx}

% Turn scalebox into an environment
\NewEnviron{scaleboxenv}[1]{%
\scalebox{#1}{\BODY}%
}

% An environment with a named argument
\newkeyenvironment{scaled}[scale=1]{%
% My real code does some more stuff here
\begin{scaleboxenv}{\commandkey{scale}}
}{%
\end{scaleboxenv}
}

\begin{document}

\begin{scaled}[scale=0.7]
Hello
\end{scaled}

\end{document}


This has nothing to do with keycommand; when LaTeX finds \begin{scaleboxenv} it need to see \end{scaleboxenv}, but it doesn't because it finds \end{scaled}, which triggers the error.

\documentclass{article}

\usepackage{environ}
\usepackage{keycommand}
\usepackage{graphicx}

% Turn scalebox into an environment
\NewEnviron{scaleboxenv}[1]{%
\scalebox{#1}{\BODY}%
}

% An environment with a named argument
\newkeyenvironment{scaled}[scale=1]{%
% My real code does some more stuff here
\scaleboxenv{\commandkey{scale}}
}{%
\endscaleboxenv
}

\begin{document}

\begin{scaled}[scale=0.7]
Hello
\end{scaled}

\end{document}


Are you sure you want to use keycommand?

Here's a keycommand and environ free version.

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\newsavebox{\wickscalebox}
\newenvironment{scaled}[1][]
{%
% My real code does some more stuff here
\setkeys{wickscale}{#1}%
\begin{lrbox}{\wickscalebox}%
}
{%
\end{lrbox}%
\scalebox{\wick@scale}{\usebox{\wickscalebox}}%
}
\define@key{wickscale}{scale}{\def\wick@scale{#1}}
\makeatother

\begin{document}

\begin{scaled}[scale=0.7]
Hello
\end{scaled}

\end{document}

• No, I'm not sure I want to use keycommand, in fact I'm trying to move to pgfkeys as we speak. – John Wickerson May 13 '13 at 10:57
• Your fix is rather interesting. Is it the case that for any environment foo I can replace \begin{foo}...\end{foo} with \foo ... \endfoo? – John Wickerson May 13 '13 at 10:58
• @JohnWickerson No. For example you must never replace \begin{lrbox} with \lrbox. – egreg May 13 '13 at 11:03