让 lrbox 与 Verbatim 环境协作的困难

让 lrbox 与 Verbatim 环境协作的困难

这是我之前的一个问题的后续: 尝试定义包装器 lrbox 时无法识别本地命令

如果我写

\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}

我得到一份毫无怨言地编译的文档。

如果我按照@egreg对上述问题的建议,我会将环境重新定义为:

\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
  }

但是当我这样做时,我收到错误:

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

? 

答案1

您遇到了一个大问题:当使用fancyvrb设施定义逐字环境时,您不能使用\end{...}辅助环境的形式。另一方面,您不能使用\end{lrbox}我在链接问题的回答中解释的方式。

一个解决方法是使用较低级别的命令:

\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}

在此处输入图片描述

相关内容