31

The verbatim environment is used to display LaTeX commands instead of having them executed.

To display a single command in-line, the \verb|| command can be used.

To display a whole block, we can use \begin{verbatim} to open the environment and \end{verbatim} to close it.

But I've come across \verbatim as well. With that, we can also type \begin{verbatim} and \end{verbatim} having them shown on our document and not initiating and ending their environment. The problem is, I still haven't found how to terminate it.

I know there are better ways, and maybe \verbatim should be never used at all. But I just wanted to understand how it worked to learn more about LaTeX. After invoking it, how can I return to a normal environment where LaTeX code is compiled, a normal font is used and line breaks are automatic?

  • 2
    The environment \begin{verbatim}...\end{verbatim} is essentially the same as \bgroup\verbatim...\endverbatim\egroup. That is, matching \verbatim and \endverbatim inside a group. Have a look at the documentation to the verbatim environment (using texdoc verbatim on a unix-like system), to find out how to use it inside other environments –  May 28 '15 at 09:39
  • 2
    Btw, for displaying tex code in a document I recommend using the listings package. –  May 28 '15 at 09:41
  • 6
    @Andrew: No, verbatim is rather special and you can't end it with \endverbatim. – Ulrike Fischer May 28 '15 at 09:48
  • In fact, Ulrike is right. I tried and even \endverbatim gets written. Not even with \bgroup and \egroup it has any effect. Thanks to you I'm learning a lot about it, but my quick question still stands: even if I simply shouldn't, after I simply type \verbatim and nothing else, how can I close the environment? – Jeffrey Lebowski May 28 '15 at 09:51
  • 1
    @UlrikeFischer Thanks, didn't know this ( obviously! :) –  May 28 '15 at 10:02
  • @ChristianHupfer \newcommand\foo will not generate \endfoo. It's the \end{foo} which checks if \endfoo exists, and if it doesn't just inserts an \endgroup without worrying. – Manuel May 28 '15 at 11:12
  • @Manuel: You're right. I confused it with some other setup –  May 28 '15 at 11:47

3 Answers3

26

verbatim is a very special environment which looks for the exact string \end{verbatim} to end it. Unlike other latex environments you can't use \env ...\endenv instead of \begin{env} ...\end{env} with verbatim.

If you really want to start with \verbatim then you must do something like this to get around the errors due to the various latex settings for environments in general and verbatim specially:

\documentclass{article}

\begin{document}
\begingroup\makeatletter\def\@currenvir{verbatim}
\verbatim
  verbatim 
\end{verbatim}

text
\end{document} 

enter image description here

Which shows that it is not a good idea to use \verbatim directly without the help of packages like verbatim.

Ulrike Fischer
  • 327,261
21

Package verbatim supports the creation of own verbatim environment, see section 2.1 of the package documentation. The following example defines environment metaverbatim in order to allow \begin{verbatim} and \end{verbatim} inside the verbatim block:

\documentclass{article}
\usepackage{verbatim}
\newenvironment{metaverbatim}{\verbatim}{\endverbatim}

\begin{document}
\begin{metaverbatim}
\begin{verbatim}
  This is a verbatim block.
\end{verbatim}
\end{metaverbatim}
\end{document}

Result

Internally \verbatim and \endverbatim are the commands that are executed as start and end part of the environment verbatim. But they should not be used outside an environment, because the verbatim environment specifically looks for \end{...} with ... as the name of the verbatim environment.

Heiko Oberdiek
  • 271,626
14

I suggest a look at the fancyvrb package, which introduces several customization facilities.

The main environment is called Verbatim, so

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}

\begin{Verbatim}
\begin{verbatim}
This is a verbatim block.
\end{verbatim}
\end{Verbatim}

\end{document}

will work flawlessly. There's still a problem if you need to show an example of a Verbatim environment. If you do, then adding

\DefineVerbatimEnvironment{MetaVerbatim}{Verbatim}{}

will allow

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\DefineVerbatimEnvironment{MetaVerbatim}
  {Verbatim}
  {formatcom=\color{mvcolor}}% this argument contains options   
\colorlet{mvcolor}{green!60!red}

\begin{document}

\begin{Verbatim}
\begin{verbatim}
This is a verbatim block.
\end{verbatim}
\end{Verbatim}

\begin{MetaVerbatim}
\begin{Verbatim}
This is a Verbatim block.
\end{Verbatim}
\end{MetaVerbatim}

\end{document}

The color of MetaVerbatim is just to show one of the many possible customizations.

enter image description here

egreg
  • 1,121,712