3

I can't get the following code to work correctly:

\documentclass{article}
\usepackage{pgfkeys}

\makeatletter
\newif\if@ae@nfig@verb@
\pgfkeys
  {%%'
  /ae/nfigverb/.is family,
  /ae/nfigverb,
    usegobble/.initial=,
    gobble/.style = { usegobble={gobble=#1,}},
    width/.initial=3in,
  }

\newsavebox{\aetitlebox}
\newenvironment{nfigverb}[1][]
  {%%'
    \pgfqkeys{/ae/nfigverb}{#1}%%'
    \edef\ae@begin@minipage{%%'
      \noexpand\minipage{\pgfkeysvalueof{/ae/nfigverb/width}}}%%'
    \lrbox\aetitlebox
      \ae@begin@minipage
  }
  {%%'
   \endminipage
    \endlrbox
  }

\makeatother
\begin{document}

\begin{nfigverb}[width=3in]
  as;lfk slfjksd flsk fls dfls
\end{nfigverb}

\usebox{\aetitlebox}

\end{document}

When I try to compile this I get the error:

! Undefined control sequence.
\\nfigverb ...rbox \aetitlebox \ae@begin@minipage 

l.32 \begin{nfigverb}[width=3in]

? 

Because of the way I intend to use keys for setting parameters of various environments, I seem to need to go the route here of \noexpand on the macro specifying the environment name so that passed options get parsed correctly.

Note

This is actually part of a bigger question. But I couldn't even get this little snippet of code to work.

I seem to be able to fix this if I write:

\newenvironment{nfigverb}[1][]
  {%%'
    \lrbox\aetitlebox
    \pgfqkeys{/ae/nfigverb}{#1}%%'
    \edef\ae@begin@minipage{%%'
      \noexpand\minipage{\pgfkeysvalueof{/ae/nfigverb/width}}}%%'
      \ae@begin@minipage
  }
  {%%'
   \endminipage
    \endlrbox
  }

I don't understand why this change makes any difference to what I'm using inside this environment. An explanation would be greatly appreciated.

Update

I'm trying to follow @tohecz 's answer at lrbox in \newenvironment.

The problem I'm having is that

\newenvironment{nfigverb}...
   {\begin{lrbox}{\mybox}
     ....}{\end{lrbox}}

will not let me later call

 \usebox{\mybox}

and print the contents from this new box.

A.Ellett
  • 50,533
  • 1
    *Never* use \lrbox...\endlrbox, but always \begin{lrbox}...\end{lrbox}, because this environment does an \endgroup that is exactly the cause of your problem. – egreg Jul 29 '13 at 20:18
  • @egreg But as I understand things, if I use \begin{lrbox}, then the box is emptied once I leave the new environment. I need to use the box later in my document. – A.Ellett Jul 29 '13 at 20:22
  • @A.Ellett no, lrbox is designed to be an environment so begin/end works – David Carlisle Jul 29 '13 at 20:26

1 Answers1

4

Here's the definition of \lrbox:

% latex.ltx, line 4634:
\def\lrbox#1{%
  \edef\reserved@a{%
    \endgroup
    \setbox#1\hbox{%
      \begingroup\aftergroup}%
        \def\noexpand\@currenvir{\@currenvir}%
        \def\noexpand\@currenvline{\on@line}}%
  \reserved@a
    \@endpefalse
    \color@setgroup
      \ignorespaces}

The \endgroup is there to match the \begingroup issued by \begin when \begin{lrbox} is executed. There are various reasons for this, but the main thing is that you should never use the \lrbox...\endlrbox form.

Also the \edef is useless, but you must set \aetitlebox globally.

\documentclass{article}
\usepackage{pgfkeys}

\makeatletter
\newif\if@ae@nfig@verb@
\pgfkeys
  {%%'
  /ae/nfigverb/.is family,
  /ae/nfigverb,
    usegobble/.initial=,
    gobble/.style = { usegobble={gobble=#1,}},
    width/.initial=3in,
  }

\newsavebox{\aetitlebox}
\newenvironment{nfigverb}[1][]
  {%
   \pgfqkeys{/ae/nfigverb}{#1}%
   \begin{lrbox}{\z@}
   \minipage{\pgfkeysvalueof{/ae/nfigverb/width}}
  }
  {%
   \endminipage
   \end{lrbox}%
   \global\setbox\aetitlebox=\box\z@
  }
\makeatother

\begin{document}

\begin{nfigverb}[width=3in]
  as;lfk slfjksd flsk fls dfls
\end{nfigverb}

\usebox{\aetitlebox}

\end{document}
egreg
  • 1,121,712
  • The \edef is there for other reasons. I reduced a lot of my code here. In my actual code I need the \edef to pass optional arguments to the minipage before it gets expanded. \edef is not there for any global purposes. – A.Ellett Jul 29 '13 at 20:32
  • This works for the MWE I posted above. I'm posting another question, but your solution, while it works for what I asked here, still doesn't work in my actual document. – A.Ellett Jul 29 '13 at 20:41