[Tex/LaTex] In-line \verb — overfull hbox problem


I'm having a problem that I've had before and I'm tired of hacking my way through it and hope that there is some supported solution. I'll go ahead and create a document for example:

\documentclass {article}
\section {Introduction}
In the introduction of the document, you will find plenty of verbatim \verb|texts
that| have an hard time wrapping.

So, the problem here is that the \verb environment doesn't wrap well (unlike the example I posted above where I put in the return to aid in viewing). There is an overfull hbox error.

How can I make the \verb command wrap and justify with its surroundings better? I would expect LaTeX to just increase or decrease the spacing around the \verb command. Is it just too much smooshing or stretching for LaTeX to agree to do it? Thanks in advance for any help.

After reading When should one use \verb and when \texttt I think that \texttt may be the right way to go here. This considerably covers the question above, but the question still stands: What about \verb?

[EDIT] Also see the related question about \texttt{} \texttt overfull hbox problem

Best Answer

The \verb macro is for verbatim text, i.e. to tell LaTeX to interpret the characters without their special meaning. It is only intended to be used for short inline verbatim material, like explaining a LaTeX macro. For longer verbatim material there is the verbatim environment (and several others). The fact that it is printed in tt font is just a side-effect! If you use spaces inside verbatim text you are requesting a verbatim, unstretchable and unbreakable space. The star-version \verb* even prints these spaces using a visual symbol.

Please always use \texttt or \ttfamily for text which should be printed in tt font. The use of \verb simply for the font is a misuse. You can use it for short words without penalty, but for longer text this issues arise. Note that verbatim mode is fragile and \verb doesn't work inside macro arguments.

If you really want to use it try the shortvrbpackage which allows you to use one character for verbatim text, e.g. only |text| instead of \verb|text|. Then you can exclude the spaces like that:

... verbatim |texts| |that| have ...

One common use of this is in package documentation, where | is already a short verbatim marker. It allows package authors to print macros they describe verbatim or some shorter related text in tt font. Again: it is not intended to be used for longer text.


  • N.B. for | to work as a delimiter in the shortverb package, you have to define it using the command \MakeShortVerb{\|}

Documentation for the shortverb package is available here.

Related Question