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.
VerbatimOut
or like environments.\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.VerbatimOut
from packagefancyvrb
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.