6

If I want to write a text how to use verbatim in TeX I would simply give an example like

\documentclass[a4paper,11pt]{article}
\begin{document}
   How to use verbatim in TeX:
\begin{verbatim}
  start with \begin{verbatim}
  and end with \end{verbatim}
   And now you have to write
\end{verbatim}
 In your own
\end{document}

Than it won't compile due to the first \end{verbatim}. One solution I've found looks like

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

\begin{document}
    The following is printed verbatim, but with line breaks:
    \begin{Verbatim}[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{Verbatim}
   on your own
\end{document}

i.e. to stack them with different environments. Does there exist a different solution as well? So my request is if there is same environment definition on which I don't have to switch between different verbatim/Verbatim/etc.

3
  • I don't know any way of doing this, because it's the literal \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. Commented Dec 14, 2020 at 19:46
  • You mean the way I did?
    – LeO
    Commented Dec 14, 2020 at 20:03
  • You can't use verbatim, its deprecated
    – Guesttt14
    Commented Dec 15, 2020 at 13:51

3 Answers 3

5

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}
4
  • Well the indention was not intended :-) The \DefineVerbatimEnvironment is finally what I'm looking for. But somehow I don't find it in the documentation. Therefore an additional question: is it possible to define the environment as well with the optional, i.e. that [breaklines=true] is already included in the command?
    – LeO
    Commented Dec 15, 2020 at 9:45
  • 1
    @LeO That's the purpose of the last argument to \DefineVerbatimEnvironment, which can take any list of options. For instance, \DefineVerbatimEnvironment{oVerbatim}{Verbatim}{breaklines=true}
    – egreg
    Commented Dec 15, 2020 at 10:07
  • Thx for the hint. I had no idea what the additonal {} would be good for. I don't want to update your sample - don't know if that's a good idea or looking in the comments.
    – LeO
    Commented Dec 15, 2020 at 10:34
  • @LeO I added it
    – egreg
    Commented Dec 15, 2020 at 11:25
4

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).

2
  • Hmmm about the second option: is there a way to write the content at the very beginning into a file (from within LuaLaTex)? Perhaps at the very beginning of the tex file?
    – LeO
    Commented Dec 14, 2020 at 20:17
  • You could use fancyvrb's VerbatimOut environment to do the job. I've used similar functionality to enable typing answers to exercises with the questions but printing them at the end of the chapter.
    – Don Hosek
    Commented Dec 14, 2020 at 20:27
3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .