# Proportional font, word-wrapping verbatim

How can I get a xelatex-compatible verbatim-like environment which uses a proportional font and applies word wrapping? I would like to able to put in something like this...

Proposition 2.3.13.

Suppose that f : M_1 -> M_2 is a map between metric spaces. Then f is continuous iff for every set U open in M_2, f^-1(U) is open in M_1.

Proof

Suppose that f is continuous, and let U be an open subset of M_2. We are required to prove that f^-1(U) is open in M_1. Let x \in f^-1(U). Then f(x) \in U, and since U is open in M_2 there exists ε>0 such that B_ε(f(x)) \subset U. By Definition 2.3.6, there exists δ>0 such that f(B_δ(x)) \subset B_ε(f(x)). Hence f(B_δ(x)) \subset U, so B_δ(x) \subset f^-1(U), and we have shown that f^-1(U) is open in M_1.

... and get out output like this:

(In case you're wondering why I want to do this, the text is input to a program and so needs to be represented literally: it would be misleading to e.g. typeset _1 as a subscript, because it can't be entered into the computer that way.) Edit: unfortunately there is one small problem with the answer below. Single-newlines aren't preserved. This is my fault for not making it clear with the MWE that I wanted this -- sorry. Could anyone tell me how to modify the vb environment to preserve newlines? Here's a new MWE...

\documentclass[a4paper,10pt]{article}
\usepackage[T1]{fontenc}

\makeatletter

\begingroup
\lccode\~\\
\lowercase{\endgroup
\def\vb{%
\par
\parindent\z@
\parskip1\baselineskip plus 2pt\relax
\let\do\@makeother\dospecials%
\let\@xobeysp\space
\catcode\ \active
\catcode\\\active
\let~\scanendvb
}}
\def\scanendvb#1#2#3#4#5#6#7{%
\def\x{#1#2#3#4#5#6#7}%
\ifx\x\endvbstr
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\end{vb}}{\char\\#1#2#3#4#5#6#7}}

\edef\endvbstr{end\string{vb\string}}

\makeatother
\def\endvb{\par}

\begin{document}
\begin{vb}
Definition 2.1.2.
A _metric space_ M = (A, d) consists of a non-empty set A together with a map d : A\times A -> \bfR such that:
(M1a) d(x, y)\geq 0 for all x, y in A.
(M1b) d(x, y) = 0 <=>  x = y  for all x, y in A.
(M2) d(x, y) = d(y, x) for all x, y in A.
(M3) d(x, y)+d(y, z)\geq d(x, z) for all x, y, z in A.
The elements of A are called the _points_ of the metric space M, and d is called a _metric_ on A. We sometimes also call d the _metric_ of M.
\end{vb}
\end{document}


\documentclass{article}
\usepackage{fontspec}
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
\makeatletter

\begingroup
\lccode\~\\
\lowercase{\endgroup
\def\vb{%
\par
\parindent\z@
\parskip1\baselineskip plus 2pt\relax
\let\do\@makeother\dospecials%
\let\@xobeysp\space
\catcode\ \active
\catcode\\\active
\let~\scanendvb
}}
\def\scanendvb#1#2#3#4#5#6#7{%
\def\x{#1#2#3#4#5#6#7}%
\ifx\x\endvbstr
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\end{vb}}{\char\\#1#2#3#4#5#6#7}}

\edef\endvbstr{end\string{vb\string}}

\makeatother
\def\endvb{\par}

\begin{document}

\begin{vb}

Proposition 2.3.13.

Suppose that f : M_1 -> M_2 is a map between metric spaces. Then f is continuous iff for every set U open in M_2, f^-1(U) is open in M_1.

Proof

Suppose that f is continuous, and let U be an open subset of M_2. We are required to prove that f^-1(U) is open in M_1. Let x \in f^-1(U). Then f(x) \in U, and since U is open in M_2 there exists ε>0 such that B_ε(f(x)) \subset U. By Definition 2.3.6, there exists δ>0 such that f(B_δ(x)) \subset B_ε(f(x)). Hence f(B_δ(x)) \subset U, so B_δ(x) \subset f^-1(U), and we have shown that f^-1(U) is open in M_1.
\end{vb}

\end{document}

• I stupidly didn't make it clear that I wanted single-newlines to be preserved... sorry. Cf. new MWE above. Is it easy to fix? – Mohan Dec 8 '12 at 14:55
• egreg provided a solution at tex.stackexchange.com/a/86110/17049 – Mohan Dec 8 '12 at 15:39