# How to force line breaks within a fancyvrb verbatim environment?

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

-
Would using listings instead of (or together with) fancyvrb be acceptable? If this is the caso, you van keep your current environment in the doucment body and simply define it differently in your preamble; something like \documentclass{article} \usepackage{listings} \lstset{% basicstyle=\ttfamily, breaklines=true, columns=fullflexible, escapeinside = ||, breakindent=0pt, } \lstnewenvironment{example}{}{} \begin{document} \begin{lstlisting} Foo|\\|Bar \end{lstlisting} \end{document} – Gonzalo Medina May 9 '12 at 17:16
I'm afraid fancyvrb typesets each line in a \hbox. The listings package allows for line breaks and even provides hooks for symbols at end and start of broken lines. – egreg May 9 '12 at 17:24

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}


-