4

I'm doing a project where I need programming source code printed throughout a PDF, with syntax highlighting and keeping the original spacing and indentation intact so copy/pasting works. Because I also need the fontspec package, I can't use the answer from here. I've been able to get it almost perfect with the listings package, but, although it prints the indenting fine, all leading indents/spaces aren't copyable!

I have tried most of the listings settings, many random hacks found online (most resulted in crashes or weird underscores rather than spaces), minted (crashed xetex (and yes, I am using --shell-escape)), verbatim, and verbments (crashed xetex also).

This is what I have so far:

\documentclass[12pt]{article}

\usepackage{color}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{textcomp}

\usepackage{fontspec}
\setmonofont[Scale=0.8, Path=/usr/share/fonts/OTF/]{Inconsolata.otf} 
%\setmonofont[Scale=0.8]{inconsolata} % can't find the font unless I do the above...

\lstset{
    basicstyle=\small\ttfamily,
    keywordstyle=\color{red},
    identifierstyle=,
    commentstyle=\color{blue},
    stringstyle=\color{green},
    showstringspaces=false,
    tabsize=4,
    numbers=left,
    captionpos=b,
    numberstyle=\tiny,
    keepspaces=true,
    numbers=none,
    columns=fullflexible,
    fancyvrb=true,
    language=python
    }

\begin{document}

And so we have the following...

\begin{lstlisting}
# sample code
def main():
    i = 0
    while i < 5:
        print(i)
        i += 1
\end{lstlisting}

\end{document}

Note that this appears fine on the pdf, but it loses the indentation spaces on copy/paste. Surely, by now, there is an up-to-date solution that will work with xelatex and not be pdf-viewer specific?

  • This is an open problem that has been addressed (but never completely solved) in a number of other questions on this site; for instance, http://tex.stackexchange.com/questions/148144/viewer-independent-copyable-spaces-at-the-beginning-of-a-line – jub0bs Aug 01 '14 at 12:38
  • related: http://tex.stackexchange.com/questions/28066/problem-copying-text-from-pdf-spaces-being-stripped?rq=1 – jub0bs Aug 01 '14 at 12:45
  • Thanks. Hopefully this problem will have a complete answer in the future... looks like I won't be using latex for this project. :( –  Aug 02 '14 at 05:02

0 Answers0