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