45

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}
\begin{document}
\section {Introduction}
In the introduction of the document, you will find plenty of verbatim \verb|texts
that| have an hard time wrapping.
\end{document}

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.

[EDIT] 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

2
  • 1
    I is unusual around here to sign your questions (as there is already a box with your name below it) or to have any formal greeting. Also, there are plenty of female TeX users!
    – Caramdir
    Feb 6, 2011 at 3:03
  • 1
    @Caramdir, thanks for the correction! No need to go into it, but "Sirs" doesn't limit the address to male users: it's generic. Feb 6, 2011 at 3:20

1 Answer 1

61

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.


Edit:

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

3
  • 1
    So you've answered most of the question, but still one remains, does LaTeX justify AROUND the \verb command to offset badness of hboxes? Am I experiencing a limit of the spacing squishiness that LaTeX does, or is LaTeX being on purpose stupid (or verbatim) in this case? Thanks for your answer, very helpful! Feb 6, 2011 at 3:06
  • Let me add that it would be possible to define a \verb like macro which keeps the spaces as normal spaces, which would allow for line breaks. But IMHO there is no reason to do so. Feb 6, 2011 at 3:09
  • 1
    LaTeX will stretch or shrink the other spaces in the same line in order to compensate for a longer verb box, but this has its limits. If you have longer verb text on a line not much room is left for other text with spaces for the correct shrink. Feb 6, 2011 at 3:13

You must log in to answer this question.

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