21

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 – Andrew May 28 '15 at 9:39
  • 2
    Btw, for displaying tex code in a document I recommend using the listings package. – Andrew May 28 '15 at 9:41
  • 6
    @Andrew: No, verbatim is rather special and you can't end it with \endverbatim. – Ulrike Fischer May 28 '15 at 9: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 9:51
  • 1
    @UlrikeFischer Thanks, didn't know this ( obviously! :) – Andrew May 28 '15 at 10:02
18

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.

  • Thanks for satisfying my curiosity! Searching for the meaning of every command you used I feel like I learned a lot! BTW, should I use \makeatother afterwards? – Jeffrey Lebowski May 28 '15 at 10:08
  • 2
    The \makeatletter is in a group, so \makeatother is not needed. – Ulrike Fischer May 28 '15 at 10:08
  • But there is an \endverbatim macro. I use it when creating environments with definitions like {...\setbox0=\vbox\bgroup \verbatim }{\endverbatim% \unskip\setbox0=\lastbox\egroup...} – Steven B. Segletes May 28 '15 at 10:45
  • @StevenB.Segletes: Yes there is. But you can't use it to end the "verbatim" processing of \verbatim. – Ulrike Fischer May 28 '15 at 11:05
16

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.

  • 2
    Thanks, that is a great way to include \begin{verbatim} and \end{verbatim} inside a verbatim block! – Jeffrey Lebowski May 28 '15 at 9:49
10

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.