phfaist / pylatexenc

Simple LaTeX parser providing latex-to-unicode and unicode-to-latex conversion
https://pylatexenc.readthedocs.io
MIT License
283 stars 35 forks source link

Special characters treated as macros #73

Closed johansmolinski closed 2 years ago

johansmolinski commented 2 years ago

Special characters escaped by \, for instance \$ or #, are treated as macro nodes. Is this on purpose? In my opinion they should just be replaced by corresponding characters ($ respectively #) and be treated like character nodes (or be included in the current character node if one is already opened).

phfaist commented 2 years ago

Yes, they are macros. E.g., you can redefine them in TeX/LaTeX; you can try for instance:

\renewcommand\#{phone number}
My \# is 012 345 6789.

Pylatexenc reflects the behavior of TeX/LaTeX by treating \#, \$, etc., as macros, as well. This is much more flexible than simply replacing them by corresponding characters, and would avoid a special treatment of a special kind of macros. In any case, you can walk through your node tree and replace these macro nodes by corresponding character nodes, if you need these elements to be treated as characters.

Is there a use case where generating a macro node for \#, \$, etc. is problematic?

I'm closing for now, feel free to reopen if I'm missing something.