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?