# 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. –  egreg Oct 16 '12 at 16:10
@egreg - You are right. A good set up should automatically make sane line breaks. I'll look for a LaTeX plugin. –  ArjunShankar Oct 16 '12 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 :) –  Harish Kumar Oct 16 '12 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. –  ArjunShankar Oct 16 '12 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. –  ArjunShankar Oct 16 '12 at 15:50