8.27.1 \verb

Synopsis:

\verb char literal-text char
\verb* char literal-text char

Typeset literal-text as it is input, including special characters and spaces, using the typewriter (\tt) font.

This example shows two different invocations of \verb.

This is \verb!literally! the biggest pumpkin ever.
And this is the best squash, \verb+literally!+

The first \verb has its literal-text surrounded with exclamation point, !. The second instead uses plus, +, because the exclamation point is part of literal-text.

The single-character delimiter char surrounds literal-text—it must be the same character before and after. No spaces come between \verb or \verb* and char, or between char and literal-text, or between literal-text and the second occurrence of char (the synopsis shows a space only to distinguish one component from the other). The delimiter must not appear in literal-text. The literal-text cannot include a line break.

The *-form differs only in that spaces are printed with a visible space character.

The output from this will include a visible space on both side of word ‘with’:

The command's first argument is \verb*!filename with extension! and ...

For typesetting Internet addresses, urls, the package url is a better option than the \verb command, since it allows line breaks.

For computer code there are many packages with advantages over \verb. One is listings, another is minted.

You cannot use \verb in the argument to a macro, for instance in the argument to a \section. It is not a question of \verb being fragile (see \protect), instead it just cannot work, as the \verb command changes the catcode regime before reading its argument, and restore it immediately afterward, nevertheless with a macro argument the content of the argument has already be converted to a token list along the catcode regime in effect when the macro was called. However, the cprotect package can help with this.


Unofficial LaTeX2e reference manual