Nested verbatims

There are pretty much only two solutions: One is to use a differently-named verbatim environment as you did, because the verbatim environment is actually looking for the literal string \end{verbatim} for its end. This is a general issue with verbatim printing in any markup language.

The other option is to use \verbatiminput from the verbatim package to include the desired verbatim text from an external file. (fancyvrb provides an equivalent \VerbatimInput command if you prefer that package's functionality).


One might think to use a stack, but it would require very deep surgery and would not solve the issue if the inner verbatim is not balanced. I don't think you have so many verbatim environments in which \end{verbatim} appears.

Sorry, but the most practical solution (and probably the only feasible one) is to use a different name for the outer environment. If you don't want to use fancyvrb, you can use the older verbatim.

\documentclass[a4paper,11pt]{article}
\usepackage{verbatim}

\newenvironment{overbatim}{\verbatim}{\endverbatim}

\begin{document}

The following is printed verbatim, but with line breaks:
\begin{overbatim}
This is a line to start; never indent `verbatim'
unless you really want indented lines
\begin{verbatim}
test
\end{verbatim}
And now you have to write
\end{overbatim}
on your own

\end{document}

enter image description here

With fancyvrb, if you want to nest Verbatim inside

\documentclass[a4paper,11pt]{article}
\usepackage{fvextra}

\DefineVerbatimEnvironment{oVerbatim}{Verbatim}{}

\begin{document}

    The following is printed verbatim, but with line breaks:
    \begin{oVerbatim}[breaklines=true]
        This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
        \begin{Verbatim}
            test
        \end{Verbatim}
     And now you have to write
    \end{oVerbatim}
   on your own

The following is printed verbatim, but with line breaks:
\begin{oVerbatim}[breaklines=true]
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own

\end{document}

enter image description here

And you see why I recommend never indenting verbatim environments.

If you want to pass breaklines=true to all oVerbatim environments, you can exploit the last argument to \DefineVerbatimEnvironment, which accepts any list of fancyvrb options.

\documentclass[a4paper,11pt]{article}
\usepackage{fvextra}

\DefineVerbatimEnvironment{oVerbatim}{Verbatim}{breaklines=true}

\begin{document}

    The following is printed verbatim, but with line breaks:
    \begin{oVerbatim}[breaklines=true]
        This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
        \begin{Verbatim}
            test
        \end{Verbatim}
     And now you have to write
    \end{oVerbatim}
   on your own

The following is printed verbatim, but with line breaks:
\begin{oVerbatim}
This is a very long line that will need to be broken into pieces otherwise it will run into and out of the margins
\begin{Verbatim}
test
\end{Verbatim}
And now you have to write
\end{oVerbatim}
on your own

\end{document}

Depending on your encoding this may work or not.

I introduced a zero-width space (unicode U+200B) between \ and end to break the inner \end command functionnaly but not visually and it renders just fine:

\begin{verbatim}\begin{verbatim}\​end{verbatim}\end{verbatim}

enter image description here

This zero-width space is, understandably, invisible in the code above. Overleaf's editor renders it as a red line with a small tack:

enter image description here

Tags:

Verbatim