逐字环境嵌套问题

逐字环境嵌套问题

我在尝试将逐字环境嵌套在我的\newenvironment定义环境中时遇到了麻烦,因此我可以将源代码包含在其结果中,就像在大多数印刷的乳胶书上一样。我可以用机械的方式做到这一点:

\begin{minipage}[b]{5in}
\fbox{\begin{minipage}[t]{2.5in}
\makebox[0mm]{gorilla gorilla}\\
\framebox[2mm][l]{gorilla gorilla}\\
\framebox[2mm][r]{gorilla gorilla}\\
\end{minipage}}\hfill
\begin{minipage}[t]{2.5in}
\begin{verbatim}
\makebox[0mm]{gorilla gorilla}\\
\framebox[2mm][l]{gorilla gorilla}\\
\framebox[2mm][r]{gorilla gorilla}\\
\end{verbatim}
\end{minipage}\mbox{}
\end{minipage}

在此处输入图片描述

但我无法通过以下方式自动完成此操作\newenvironment

\newenvironment{exe}[1]{\begin{minipage}{5in}
\begin{minipage}{2.5in} #1 \end{minipage}\hfill 
\begin{minipage}{2.5in}\begin{verbatim}#1\end{verbatim}\end{minipage}}
{\end{minipage}}

\newcommand{\bex}{\begin{exe}}
\newcommand{\eex}{\end{exe}}

%Inside the body:
\bex{\framebox[2mm]{gorilla gorilla}\\
\framebox[2mm][l]{gorilla gorilla}\\
\framebox[2mm][r]{gorilla gorilla}\\
}\eex

如果我从定义中删除环境,宏就会起作用\verbatim...我很绝望。有人能帮助我吗?提前致谢!

PS:由于任何特殊原因,方括号缺失\newenvironment1

答案1

正如我在评论中所说,不能将其用作verbatim宏或环境参数的一部分。

完全修改的答案

这个修改后的答案满足了 OP 所主张的需求,因为方程式可能是展示代码的一部分,并且计数器和方程式编号被保留。

相反,在这里我使用\detokenize来代替verbatim。我巧妙地使用\obeyspaces\obeylines来捕捉源代码的布局。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listofitems}
{
\obeylines
\xdef\detokenizedcr{\detokenize{
}}% TO CAPTURE A DETOKENIZED CARRIAGE RETURN
\gdef\showcaseaux#1{\par\noindent%
  \xdef\tmp{\detokenize{#1}}%
  \greadlist\mycode{\tmp}%
  \begin{minipage}[t]{.48\textwidth}%
  \let^^M=\space#1\mbox{}\vspace{-\baselineskip}%
  \end{minipage}%
  \hspace{.04\textwidth}%
%  \fboxsep=-\fboxrule\fbox{% UNCOMMENT THIS LINE TO SEE BOXES
  \begin{minipage}[t]{.48\textwidth}\ttfamily
  \foreachitem\x\in\mycode{\ifnum\xcnt=1\else\\\fi\mbox{}\x}%
  \strut%
  \end{minipage}%
%  }% UNCOMMENT THIS LINE TO SEE BOXES
  \par
  \catcode`\^^M=5 %
  \catcode`\ =10 }
}
\newcommand\showcase{\obeylines\obeyspaces\showcaseaux}
\expandafter\setsepchar\expandafter{\detokenizedcr}
\begin{document}
\showcase{Here is an example of an unnumbered displayed equation:
   \[ x' + y^2 = z_{i}^2 \]}
\showcase{and here is the same equation numbered:
 \begin{equation}
   x' + y^2 = z_{i}^2
 \end{equation}}
\showcase{and here is another equation numbered:
 \begin{equation}
   z = \frac{x}{y}
 \end{equation}}
 \showcase{As you can see, the vertical alignments are good.}
Are    we
  back to
     normal    yet?

\noindent\hrulefill

Here is what happens if you don't block the showcases separately:
\showcase{Here is an example of an unnumbered displayed equation:
   \[ x' + y^2 = z_{i}^2 \]
and here is the same equation numbered:
 \begin{equation}
   x' + y^2 = z_{i}^2
 \end{equation}
and here is another equation numbered:
 \begin{equation}
   z = \frac{x}{y}
 \end{equation}
As you can see, the vertical alignments 
can get off and page-breaking will be an issue.}
\end{document}

在此处输入图片描述

在下图中,我取消了源代码中的两行注释,以显示\showcase分别被阻止时和未被阻止时的性能。

在此处输入图片描述

相关内容