Note to Self: Clojure Code in LaTeX vs. Copy&Paste

This post is a somewhat extended “Note to Self” in the sense that some of this may also hopefully be useful for others.

I like LaTeX a lot and its “listings” package is great for including source code in documents. However, while the source code typically looks good in the generated documents, there are problems when copying and pasting the code from the listings.

Typically, one of the reasons of providing a code example is that someone can reproduce certain things. Hence, it would be great if it was possible to directly copy and paste code from listings generated with LaTeX to a source code file or REPL. Unfortunately, there are some issues related to this.

At first, I will show a minimum working example for illustrating the problem. Afterwards, I show a minimum working example with the solution I use so far. The current “solution” is based on various things I could find on the Internet and merging all the bits and pieces. Actually, when searching for “LaTeX” and “lstlisting” and “copy paste” or “source code” one can find many posts, questions, etc. with all sorts of answers.

The Problem

On my machine, the following “naive” LaTeX document

\documentclass{scrartcl}

\usepackage{listings}

\begin{document}

\begin{lstlisting}
(let [my-list '(1 2 3)
      my-product (reduce * my-list)]
  (println "My-Product (1*2*3): " my-product))
\end{lstlisting}

\end{document}

produces the output shown in the following screenshot:

mwe_bad

The problem is that copying and pasting this from a PDF viewer, such as Evince, results in the following when pasted:

( l e t [ my− l i s t ’ ( 1 2 3 )
my−p r o d u c t ( r e d u c e ∗ my− l i s t ) ]
( p r i n t l n ”My −Product ( 1 ∗ 2 ∗ 3 ) : ” my−p r o d u c t ) )

Clearly, this cannot be used for pasting it into a source file or the REPL. The most obvious problem is the spacing of characters but there are also issues related to quotation marks etc.

My current Solution

In order to fix these copy&paste issues, I currently use the following solution:

\documentclass{scrartcl}

\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[variablett]{lmodern}

\lstset{
 basicstyle=\ttfamily,
 columns=fullflexible,
 upquote,
 keepspaces,
 literate={*}{{\char42}}1
 {-}{{\char45}}1
}

\begin{document}

\begin{lstlisting}
(let [my-list '(1 2 3)
      my-product (reduce * my-list)]
  (println "My-Product (1*2*3): " my-product))
\end{lstlisting}

\end{document}

This produces the following output:

mwe_good

Copying and pasting this from the PDF, results in the following:

(let [my-list '(1 2 3)
my-product (reduce * my-list)]
(println "My-Product (1*2*3): " my-product))

Besides the indentation not being correct, this is actually something that can be run as-is in a REPL or be used in a source code file.

The credit for the solution goes to all the helpful posts on this topic. As I had to merge various solution approaches from multiple posts and play a bit with all the input before coming to the above “solution”, I, unfortunately, did not keep track of the references that led me to this state. So I just go with a general “thank you” to the nice and helpful LaTeX community.

Advertisements
This entry was posted in Misc., Snippets and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s