如何在用户定义的环境中扩展逐字块?

如何在用户定义的环境中扩展逐字块?

为了生成适合电子阅读器的小页面 PDF,我想缩小一些过大的逐字块,例如程序输出,这些输出行对于小页面来说太长。这样读者就可以在需要时放大。这个使用环境的简单测试adjustbox运行BVerbatim良好fancyvrb

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\begin{document}
\begin{adjustbox}{max width=\linewidth}
\begin{BVerbatim}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{BVerbatim}
\end{adjustbox}
\end{document}

但是,我在可重用环境中包装 adjustbox+verbatim 组合时遇到了一些麻烦。特别是这种尝试:

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\newenvironment{myverb}{
\begin{adjustbox}{max width=\linewidth}
\begin{BVerbatim}
}{
\end{BVerbatim}
\end{adjustbox}
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}

产生此错误:

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

这次尝试:

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{verbatim}
\newenvironment{myverb}{%
\adjustbox{max width=\linewidth}%
\verbatim
}{%
\endverbatim
\endadjustbox
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}

导致此错误:

! LaTeX Error: Something's wrong--perhaps a missing \item.

您认为我所需要的可以使用可用的 LaTeX 包和工具来实现吗?

答案1

定义逐字环境非常特殊。您可以在 TeX.SX 以及常问问题

然而这里的技巧是告诉 LaTeX 下一个环境定义是逐字环境。这可以通过命令来实现\VerbatimEnvironment

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\newenvironment{myverb}{%
 \VerbatimEnvironment
 \begin{adjustbox}{max width=\linewidth}
 \begin{BVerbatim}
  }{
  \end{BVerbatim}
 \end{adjustbox}
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}

相关内容