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}
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}
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}
\end{verbatim}
that is looked for to end a verbatim block, but the example would be even more interesting if you include something between the two\end
commands.