Python indentation in a text code generated on a pdf file via Latex listings package

328 Views Asked by At

I am trying to use listings package of Latex in order to type ready to test Python code on a pdf file. As well known, a script such as a for loop needs some spaces in order to work correctly. My problem is that in the pdf generated text, spaces are in some ways shown, but not possible to be copied.

Here a sample code in order to make you understand my issue

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\begin{document}

\lstset{language=Python, literate={-}{-}1}
\lstset{frame=lines}
\lstset{caption={Insert code directly in your document}}
\lstset{label={lst:code_direct}}
\lstset{basicstyle=\footnotesize}
\lstset{keepspaces=true}
\lstset{columns=fullflexible}

\begin{lstlisting}
for j in range(5):
    print(j)
\end{lstlisting}

\end{document}

Now, compiling this, the pdf file shows the following lines of code

for j in range(5):
    print(j)

which looks great, but not suitable for a copy and paste test, as what you gonna paste is

for j in range(5):
print(j)

which is obsiusly wrong! Could you help me to figure out how to deal with it?

0

There are 0 best solutions below