TeX - LaTeX Asked on March 13, 2021
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?
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 the Unicode Character 'FIGURE SPACE' (U+2007)
, and in the adobe reader it still disappears.
documentclass{article}
usepackage{listings}
usepackage[T1]{fontenc}
makeatletter
lst@AddToHook{Init}
{deflst@outputspace{ pdffakespace}}
makeatother
input{glyphtounicode}
% saner but doesn't work
%pdfglyphtounicode{space}{0020}
% works in sumatra:
pdfglyphtounicode{space}{2007}
begin{document}
lstset{language=Python,
columns=fullflexible,
basicstyle=ttfamily,
showspaces,
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}
Correct answer by Ulrike Fischer on March 13, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP