4

This is a follow up to an earlier question of mine: local commands are not recognized when trying to define a wrapper lrbox

If I write

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{multicol}
\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{\ae@titlebox}
\newsavebox{\aetitlebox}
\newenvironment{nfigverb}[1][]
  {%%'
    \global\@ae@nfig@verb@true
    \lrbox\aetitlebox
    \pgfqkeys{/ae/nfigverb}{#1}%%'
    \edef\ae@begin@minipage{%%'
      \noexpand\minipage{\pgfkeysvalueof{/ae/nfigverb/width}}}%%'
    \edef\ae@vbegin@verbatim{%%'
      \noexpand\Verbatim[\pgfkeysvalueof{/ae/nfigverb/usegobble}]}%%'
      \ae@begin@minipage
        \ae@vbegin@verbatim
  }
  {%%'
        \endVerbatim
      \endminipage
    \endlrbox
  }

\def\aetest{\if@ae@nfig@verb@ Ciao\else Hello\fi}

\makeatother
\begin{document}

\begin{multicols}{2}

\aetest

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

\fbox{\usebox{\aetitlebox}}

\aetest

\end{multicols}
\end{document}

I get a document that compiles without complaint.

If I follow @egreg 's suggestion to the question mentioned above, I would redefine the environment as:

\newenvironment{nfigverb}[1][]
  {%%'
    \global\@ae@nfig@verb@true
    \begin{lrbox}{\ae@titlebox}
    \pgfqkeys{/ae/nfigverb}{#1}%%'
    \edef\ae@begin@minipage{%%'
      \noexpand\minipage{\pgfkeysvalueof{/ae/nfigverb/width}}}%%'
    \edef\ae@vbegin@verbatim{%%'
      \noexpand\Verbatim[\pgfkeysvalueof{/ae/nfigverb/usegobble}]}%%'
      \ae@begin@minipage
        \ae@vbegin@verbatim
  }
  {%%'
        \endVerbatim
      \endminipage
    \end{lrbox}
    \global\setbox\aetitlebox=\box\ae@titlebox
  }

But when I do this, I get the error:

Runaway argument?
! File ended while scanning use of \FancyVerbGetLine.
<inserted text> 
                \par 
<*> mwe_02.tex

? 
A.Ellett
  • 50,533
  • Is your goal to get verbatim in a box? That can be done with \documentclass{article} \usepackage{verbatimbox} \begin{document} \begin{verbbox} as;lfk slfjksd flsk fls dfls \end{verbbox} \framebox[\textwidth][l]{\theverbbox} \end{document} – Steven B. Segletes Jul 30 '13 at 00:06
  • @StevenB.Segletes That's part of what I'm doing. But mostly I have many different examples of code I want to exhibit. Usually, I use the same parameters to set the environment, sometimes I want to modify them. So, what I'm going after is creating the environment once, in my premable, for uniformity purposes. Also, if I decide I want to change the general appearance, then there's only one place I have to go to make that change. – A.Ellett Jul 30 '13 at 00:15
  • Would the numberedblock package help you? It is for displaying short blocks of code. – Steven B. Segletes Jul 30 '13 at 00:32
  • Related: for the verbatim environment (lowercase), there's a different issue of lrbox use hbox: see boxes - Can't I put a list inside an \lrbox? - TeX - LaTeX Stack Exchange – user202729 Jun 26 '22 at 12:42

1 Answers1

3

You're incurring in a big problem: when defining a verbatim environment with fancyvrb facilities, you can't use the \end{...} form for helper environments. On the other hand you can't use \end{lrbox} as explained in my answer to the linked question.

A way out is to use a lower level command:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{multicol}
\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][]
  {%%'
   \global\@ae@nfig@verb@true
   \global\setbox\aetitlebox=\hbox\bgroup
   \pgfqkeys{/ae/nfigverb}{#1}%%'
   \minipage{\pgfkeysvalueof{/ae/nfigverb/width}}
   \begingroup\edef\x{\endgroup
   \noexpand\Verbatim[\pgfkeysvalueof{/ae/nfigverb/usegobble}]}\x
  }
  {%%'
   \endVerbatim
   \endminipage
   \egroup
  }

\def\aetest{\if@ae@nfig@verb@ Ciao\else Hello\fi}

\makeatother
\begin{document}

\noindent\aetest

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

\noindent
\kern-\fboxsep\kern-\fboxrule
\fbox{\usebox{\aetitlebox}}%
\kern-\fboxsep\kern-\fboxrule

\noindent\aetest
\end{document}

enter image description here

egreg
  • 1,121,712
  • Why \begingroup\edef\x{\endgroup? This code seems to work find if I just write \edef\x{\noexpand\Verbatim[....} – A.Ellett Jul 29 '13 at 21:59
  • @A.Ellett In this way, any previous definition of \x is irrelevant and the new one disappears as soon as \x is executed. See http://tex.stackexchange.com/a/19750/4427 – egreg Jul 29 '13 at 22:06