# Verbatim environment nesting troubles [duplicate]

This question already has an answer here:

I have troubles trying to nest verbatim environment inside my \newenvironment definition one so I can include my source code faced to its result as on the most latex books printed. I can do it in a mechanical way:

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


But I'm not able do it in an automatic way by means my \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


The macro works if I remove \verbatim environment from the definition... I'm desperate. Anyone can help me? Thanks in advanced Best Regards!

P.S.: For any extrange reason squared brackets are missed on the \newenvironment definition. 1

## marked as duplicate by egreg environments StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Mar 1 '18 at 14:43

As I said in my comment, one cannot use verbatim as part of a macro or environment argument.

This revised answer meets the need espoused by the OP in that equations may be part of the showcased code, and counters and equation numbers are retained.

Instead, here I employ \detokenize as a faux substitute for verbatim. I cleverly use \obeyspaces and \obeylines to capture layout of source code.

\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}}%
\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:
$$x' + y^2 = z_{i}^2$$}
\showcase{and here is another equation numbered:
$$z = \frac{x}{y}$$}
\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:
$$x' + y^2 = z_{i}^2$$
and here is another equation numbered:
$$z = \frac{x}{y}$$
As you can see, the vertical alignments
can get off and page-breaking will be an issue.}
\end{document}


In the following image, I uncomment two lines in the source code to display how the \showcase perform when blocked separately, and when not.

• Thanks for your answer I have to understand how it Works but it looks nice. Thanks you so much – Sergio Mar 1 '18 at 13:50
• @Sergio \detokenize is similar to verbatim, but has more limitations (no % allowed, braces must be balanced, does not obey lines, etc.). In addition, it automatically inserts a space at the end of every macro name (in this example \makebox and \framebox). – Steven B. Segletes Mar 1 '18 at 13:57
• Hi Steven, your proposal is not useful for me due to I need break lines inside and all forbidden characters availables as well. I think that a pair of minipage nested into main minipage must be the right solution as I told you most of the Latex books make it – Sergio Mar 1 '18 at 14:34
• Hi Steven I've included a pair of pictures what I need about. Do you have any solution for it? – Sergio Mar 1 '18 at 14:39
• @Sergio Your images just made the problem much more complicated. Not only do you wish to render code on the left, but you wish to do so with all the counters and environments intact, so that, for example, equations are counted and numbered. – Steven B. Segletes Mar 1 '18 at 14:50