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?

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


    \def^^M{ }%


Some \VerbLB+text
with+ a line break.

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 '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:


= function (bar, baz);}


Note obeyspaces option

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.