I'm typesetting litterateliterate programs; they look like this:
\documentclass{article} \usepackage{fontspec} \newfontfamily{\Symbola}{Symbola} \newfontfamily{\UbuntuMono}{Ubuntu Mono} \DeclareTextFontCommand{\S}{\Symbola} \usepackage{xcolor} \newenvironment{code}{\setlength{\parindent}{0pt}\UbuntuMono}{} \begin{document} \begin{code} (fold\_left (\S{λ} x \textcolor{red}{y}. x \S{⊕} \textcolor{red}{y})\\ (a \S{∷} b \S{∷} c \S{∷} d \S{∷} nil)) \end{code} \end{document} How can I make sure that the code environment perservespreserves all whitespacewhitespaces? In particular, that whitespacewhitespaces should be in the generated PDF, too: that is, copy-pasting from the PDF should copy the whitespacewhitespaces as well.
Here are a few attempts:
- Use
\hspace{}: not very nice when looking at the sources, and spaces can't be copied from resulting PDF. - Replace all spaces by
~, and add\-for beginning-of-line spaces. Sources become ugly (not a show-stopper), but spaces still can't be copied. - Use
\catcode32=12in definition ofcode. This works fine alignment-wise, it looks perfect… but spaces still can't be copied. - Using the
listingspackage: would be great, but it dies on Unicode, and I'm tired of thelitterateliterate-based workarounds - Using the
fancyvrbpackage: Has anescapecharproperty that could be promising, and line breaks being significant in the source are not a deal-breaker, but it seems heavyweight (?). Ideally, I'd like to be able to use this environment inline, too.
What magic do I need to make my environment respect all spaces, and make them copy-pastable from the PDF?