Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
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
add comment

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
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
add comment

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

enter image description here

share|improve this answer
    
This is a pretty neat hack, even if \path kind of obscures what the code really means. –  ArjunShankar Oct 16 '12 at 15:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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