# Line-break inside a \verb'

I have set my text editor to wrap text at 76 columns.

When including verbatim text in-line, my text editor did an auto-word-wrap and I ended up with something that looked like:

\item Just another boring explanation about some code and, e.g. \verb|foo
= function (bar, baz);| More stuff


i.e. a line-break in between verbatim code.

pdflatex didn't like this:

LaTeX Error: \verb ended by end of line.

How/should I try to get around this? Or is the only way out an early line break?

• AUCTeX for Emacs never breaks \verb arguments across lines. Good editors set up for LaTeX should do the same. In case yours doesn't do this check, get the habit of writing <space>%<newline>\verb. The editor should at least respect lines that end with %; if it doesn't, change it. Oct 16, 2012 at 16:10
• @egreg - You are right. A good set up should automatically make sane line breaks. I'll look for a LaTeX plugin. Oct 16, 2012 at 17:22

I was a little bit surprised to discover that this wasn't already catered for by the fancyvrb package. When using that package, the problem would appear to be because the newline character is defined to be an outer macro, which can't then be gobbled into the verbatim command. So here's a little modification that defines a command \VerbLB which converts newlines into spaces.

\documentclass{article}
\usepackage{fancyvrb}

\makeatletter
\def\VerbLB{\FV@Command{}{VerbLB}}
\begingroup
\catcode\^^M=\active%
\gdef\FVC@VerbLB#1{%
\begingroup%
\FV@UseKeyValues%
\FV@FormattingPrep%
\FV@CatCodes%
\def^^M{ }%
\catcode#1=12%
\def\@tempa{\def\FancyVerbGetVerb####1####2}%
\expandafter\@tempa\string#1{\mbox{##2}\endgroup}%
\FancyVerbGetVerb\FV@EOL}%
\endgroup
\makeatother

\begin{document}

Some \VerbLB+text
with+ a line break.
\end{document}


This produces "Some text with a line break." (Given that the line break was inserted by your editor, my guess was that you didn't want it in the output). I've no idea if this breaks anything else! (Well, as it's a new macro it won't break anything, but I've no idea why the new line character was defined as \outer and it may be that there was a very good reason for it.)

Incidentally, I was sorely tempted to comment "Use a better editor". Some editors use soft and hard line breaks and soft ones are only used for display purposes, thus removed when the file is written.

• Ah! LIONS of the @ jungle! +1 :)
– user11232
Oct 16, 2012 at 14:53
• Thanks for writing up \VerbLB! As to the editor, I use it for a lot of tasks, and the column limit is a habit/configuration I carry over from writing code and e-mails. Oct 16, 2012 at 15:49

A verb-like construction \path from url or hyperref package can work with line breaks:

\documentclass{article}
\usepackage[obeyspaces]{url}
\begin{document}

\path{foo
= function (bar, baz);}

\end{document}


Note obeyspaces option

• This is a pretty neat hack, even if \path kind of obscures what the code really means. Oct 16, 2012 at 15:50
• using \path worked reasonably well for breaking long regular expressions in paragraph table cells. Thanks alot! Mar 7, 2017 at 9:53

Here, I use a token cycle to emulate an inline-\verb-like command with line breaking, called \bverb{...}. I should point out where I think it has application: for short inline bursts of verbatim where, for example, a long verbatim variable name runs out the right margin. This approach allows either automatic line breaking at any token, or else targeted line breaking at designated discretionary breakpoints.

It is not truly verbatim in several ways:

1. Braces must be balanced, and all cat-1/cat-2 token pairs will be represented as braces {} in the output.

2. Spaces are not treated as verbatim, but stretch and linebreak.

Here are some features:

1. By default, line breaking occurs at any character.

2. An non-empty optional character (or macro) can be specified to indicate non-hyphenating discretionary line-break points, if one desires to line-break at more logical places

3. While spaces are presented as spaces, an optional second argument may be used to respecify the rendering of spaces, for example, with \textvisiblespace.

4. Cat-14 % comment characters are treated in a verbatim fashion.

5. Best of all, that silly space that appears after detokenizing macro names goes away in \bverb, except where necessary.

Here is an MWE that tries to demonstrate all these points.

\documentclass{article}
\usepackage{tokcycle}
\newcommand\bverb[1][]{\begingroup\ifx\relax#1\relax
\def\tmpA{\allowbreak}\let\tmpB=\empty
\setlength\spaceskip{1.5ex plus 1ex minus .5ex}% SETTING \xspaceskip CAN BE DONE
\else\def\tmpA{}\let\tmpB=#1
\setlength\spaceskip{1.5ex plus 1ex minus .5ex}\fi% SETTING \xspaceskip CAN BE DONE
\catcode\%=12 \bverbaux}

\newcommand\bverbaux[2][ ]{%
\ttfamily\tokencycle
{\tctestifnum{\cstest{##1}=11}
{\tcpeek\zzz\tctestifcatnx\zzz A{\tcpush{\space}}{}}{}%
\tcpush{\string##1}}}
\def\cstest#1{\expandafter\cstestaux\string#1.\relax}
\def\cstestaux#1#2#3\relax{\the\catcode#2 }

\textwidth=2.5in
\begin{document}
\bverb{%\$#\-^_+\x vb\ x\?a\x 0 In the normal \bverb situation,
spaces are visible, and line-breaks may occur at any location.}

\bverb[\-]{A specific (non-printing) discre\-tionary break character may
be in\-serted by using the optional ar\-gument.  In  this case, only the
discretionary break (and spaces) al\-low breaks, allowing one to set
non-hyphenated breaks at the syl\-lable boundaries.}

\bverb[!]{A non-empty optional argument must be defined for the dis!cret!ionary
break to be defined.
The token required for \bverb line breaks can be defined as either a macro,
such as \- or alter!nately a character, as here, an ex!clama!tion point.}

\bverb[!]{Balanced {bracing} is a require!ment and spaces are not treated
in a \verbatim fashion.}

\bverb[\-][\textvisiblespace]{The second optional argument of \bverb is an alternate
treatment for the space character. Here, \textvisiblespace is selected.}

\bverb[!]{Notice how the nasty space that follows macro names when using
\de!tokenize disappears, whenever ap!pro!priate: \x0, \x a, \%x vs \% x}

\end{document}
`