I am looking to write python programs that modify LaTeX source files. To do this I would like to have a basic parser in python that can reliably read and write LaTeX files while maintaining the tree. I'm okay if it is not a full implementation, but I need it to handle LaTeX's odd quoting rules and {} notation. Regular expressions simply do not work for this, due to the fact that braces can be recursive.
EDIT:
The main thing I want to handle is recursive braces, which is why I need a parser, rather than a simple lexical analyzer. That is, I want to be able to register \foo{} as a command I care about and catch:
\foo{this is the foo argument}
But I also want to be able to catch:
\foo{this is \emph{really} the foo argument}
Is there any such python module out there?
.texis the same as the original input file, then no. But depending on the actual use case this may still be a suitable approach. – Marijn Sep 27 '20 at 14:03this is \emph{really} the foo argument, then what do you want to do with it? Modify and write back? What kind of modification? And what is the reason for doing this? Note that if it is just about nested braces then you may also be able to do what you want using general purpose libraries, e.g.,from pyparsing import nestedExpr. – Marijn Sep 28 '20 at 19:04