Here is a block of latex code to produce some Python code. I'd like to be able to copy and paste it into Python.
\documentclass{standalone}
\usepackage{listings} % Include the listings-package
\begin{document}
\lstset{language=Python,
columns=fullflexible,
basicstyle=\ttfamily,
showstringspaces=false
}
\begin{lstlisting}
def test(inputvariable):
if inputvariable == 4:
print("The input variable is 4 .")
print("So I'm giving this response.")
print("done with if statement.")
test(4)
test(3)
\end{lstlisting}
\end{document}
It looks right:
Unfortunately when I copy and paste it, the indentation is lost, and that's pretty key to how Python works.
Is it possible to get the spacing to appear when I copy/paste it?
Best Answer
Inho it is not really possible in a reliable way. It depends too much on the pdf viewer. It is possible to insert real spaces with the
\pdffakespace
primitive, but the indentation is preserved in sumatra only if I map the space (globally!) to theUnicode Character 'FIGURE SPACE' (U+2007)
, and in the adobe reader it still disappears.