# How to keep indentations in Python code copied from LaTeX PDF?

I tried to include Python 3.6 code in a LaTeX PDF document which should be easily be copied to save to a file or to try the Python code.

Although,

\begin{verbatim}
for row in range(1,9):
for col in range(1,9):
print(int(str(row)+str(col)))
\end{verbatim}


shows nicely in the PDF but if I copy it and paste it to a text editor it looks like:

for row in range(1,9):
for col in range(1,9):
print(int(str(row)+str(col)))


The indentations which are essential for Python are gone.

I also tried the suggestion here using the listings package:

How to highlight Python syntax in LaTeX Listings \lstinputlistings command

where I can simply give the file name of an Python file e.g.

\pythonexternal{Test.py}


And the source code will be included and colored. But the same thing with the missing spaces at the start when copy&pasting.

If I use the option "showspaces=true" I get the following:

for␣row␣in␣range(1,9):
␣␣␣␣for␣col␣in␣range(1,9):
␣␣␣␣␣␣␣␣print(int(str(row)+str(col)))


which is also not suitable for copy&paste. Well, I could replace all ␣ with space... not a really practicable solution.

There have been some weird hacks described in 2011 here:

How to make listings code indentation remain unchanged when copied from PDF?

Is there anything new since then...? Any ideas how to achieve copy&paste Python code in a LaTeX PDF? Thank you for suggestions.

• I used once attachfile package to get code snippets attached to the PDF (but not every PDF viewer will display them). I also used filecontentsdef to create those snippet files from LaTeX source, but you might prefer VerbatimOut or like environments. – user4686 Feb 25 '18 at 21:22
• I prefer not to use something which is displayed (or not) depending on the viewer. I couldn't try filecontentsdef. How to include? \usepackage{filecontensdef}? It should be included in MikTeX but TeXnicCenter didn't load it. VerbatimInput would be an option, but when copying the Python code from PDF to text editor the indentations are also gone. As I understand the above mentioned thread and hacks from 2011, there was no working solution. – theozh Feb 26 '18 at 7:45
• you did not understand. I referred to VerbatimOut from package fancyvrb which allows (like filecontentsdef) to create some file from within the LaTeX source. Then you attach these files with package attachfile. As per filecontentsdef you need to know how to update your TeX installation this is not the problem here. – user4686 Feb 26 '18 at 12:08
• maybe, I did not make myself clear. I don't want to create an external file from LaTeX code and I don't want to attach a file. I want to include source code from an external file which can be selected and copied later from the resulting PDF and pasted into a text editor including indentations. – theozh Feb 26 '18 at 19:32
• I know no way to copy paste from PDF (especially if you don't want to restrict to specific viewers) and preserve code indentation. I proposed file attachments as the only foolproof method I know of. HTML or for that matter plain text are simply better than PDF. This is major issue with PDF. – user4686 Feb 26 '18 at 22:35