Skip to main content

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=12 in definition of code. This works fine alignment-wise, it looks perfect… but spaces still can't be copied.
  • Using the listings package: would be great, but it dies on Unicode, and I'm tired of the litterateliterate-based workarounds
  • Using the fancyvrb package: Has an escapechar property 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?

I'm typesetting litterate 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 perserves all whitespace? In particular, that whitespace should be in the generated PDF, too: that is, copy-pasting from the PDF should copy the whitespace 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=12 in definition of code. This works fine alignment-wise, it looks perfect… but spaces still can't be copied.
  • Using the listings package: would be great, but it dies on Unicode, and I'm tired of the litterate-based workarounds
  • Using the fancyvrb package: Has an escapechar property 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?

I'm typesetting literate 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 preserves all whitespaces? In particular, that whitespaces should be in the generated PDF too: that is, copy-pasting from the PDF should copy the whitespaces 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=12 in definition of code. This works fine alignment-wise, it looks perfect… but spaces still can't be copied.
  • Using the listings package: would be great, but it dies on Unicode, and I'm tired of the literate-based workarounds
  • Using the fancyvrb package: Has an escapechar property 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?

Source Link
Clément
  • 4.1k
  • 1
  • 32
  • 43

How can I preserve all whitespace in an environment?

I'm typesetting litterate 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 perserves all whitespace? In particular, that whitespace should be in the generated PDF, too: that is, copy-pasting from the PDF should copy the whitespace 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=12 in definition of code. This works fine alignment-wise, it looks perfect… but spaces still can't be copied.
  • Using the listings package: would be great, but it dies on Unicode, and I'm tired of the litterate-based workarounds
  • Using the fancyvrb package: Has an escapechar property 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?