I have a Tex document that uses the listings package to print a Python source code. I also want to separately print certain Python function signatures in my document. I do not want to use the line number functionality from the listings package since the Python code might be edited in the future.
Is there any way to automatically sync certain parts of the Python code with the Tex document (without using line numbers)?
Thank you very much!
Best Answer
The main problem is that you need to analyze the Python source code, which TeX can't (easily) do. Luckily Python is capable of analyzing itself, using abstract syntax trees (see also some more thorough documentation). The idea is that we have the Python ast analyze the code and write what it finds to a TeX file.
Creates the file
linenumbers.tex
:Then, you can have your main TeX file input the derived TeX file, and use what it found:
This results in