[Tex/LaTex] How to force line breaks within a fancyvrb verbatim environment

line-breakingverbatim

Consider the following LaTeX document:

\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{example}{Verbatim}{commandchars=\\\{\}}
\begin{document}
\begin{example}
Foo\\{}Bar
Foo\linebreak{}Bar
\end{example}
\end{document}

I would expect the \\ and \linebreak commands to produce a line break each, so the typeset output ought to look like

Foo
Bar
Foo
Bar

However, the actual output is

Foo               Bar
FooBar

How do I produce an explicit line break inside a fancyvrb-based verbatim environment?

(Incidentally »Just hit Return« isn't the answer I'm looking for. I have lots of text based on the alltt environment which contains markup like

Foo\nextline Bar

I use this to introduce »typographic« line breaks in shell command output, where there is no actual line break in the output but I need to start a new line because there is no more room on the current line. In my document, these »typographic« line breaks can be distinguished from genuine line breaks because there is a special symbol before/after the line break, along the lines of

\newcommand{\nextline}{\ensuremath{\rhd}\linebreak\ensuremath{\lhd}}

I'd much rather not change a couple thousand pages' worth of documentation.)

Best Answer

I am afraid fancyvrb doesn't allow what you want to do; a possible workaround would be using the listings package instead of (or together with) fancyvrb (listings offers automatic line breaking and many other useful features; please refer to the package documentation for details). You can keep your current example environment in the document body, and simply define it using listings in your preamble; something like this:

\documentclass{article} 
\usepackage{listings} 

\lstset{% 
  basicstyle=\ttfamily, 
  breaklines=true,
  columns=fullflexible,
  escapeinside = ||,
  breakindent=0pt} 
\lstnewenvironment{example}{}{} 

\begin{document} 

\begin{example} 
Foo|\\|Bar 
\end{example} 

\end{document}

enter image description here