I am writing a small python script that does some magic (will post it on github when ready). I need to search through my .tex file commands. They way I implemented it so far is that the script looks for backslashes and read whatever comes after until the command call ends. For this I need to know all possible admissible character that I can use in a command, so that my script is perfectly robust and won't stop at the wrong char. Any idea?

(please don't give comments about the search algorithm etc, will open a new thread once my script is ready enough and get feedback separately)

  • the command escape does not have to be \ look at xii.tex on ctan or texlive which uses j for example Commented Nov 14, 2022 at 0:21

1 Answer 1


The rules of TeX about control sequences are simple. There are

  • control symbols
  • control words

Control symbols have the backslash (more precisely, a character with category code 0) and exactly one character that hasn't category code 11.

Control words have the backslash followed by any number of characters with category code 11 and any character with category code not 11 will stop the search for the name.

Do you see the problem? At any moment, you can change the category code of a character and so allow control words that weren't available before or disallow others.

This is exploited by \makeatletter, which changes @ to catcode 11, so allowing control words with @ in their name. Once a control word enters the TeX scanner, its name is recorded in a way that's independent from catcodes.

I don't know the aim of your Python script and whether you can make assumptions about “standard” category codes. But it would be easy to trick your script: if you type


TeX will interpret this the same as \begin, because the sequence ^^" is transformed into b before the procedure to scan a control sequence name. Oh, this transformation only takes place if ^ has category code 7. And in order to know the catcode of a character at a given place, you need to interpret the TeX code ahead of it.

  • Thank you! My python code is reading from /begin{document} and somehow eliminating macros\commands in there. The script doesn't need to be perfect right now (I am using it for my thesis), I am just assuming the user (me) is only calling stuff like \textbf or simply things you can create with def or newcommand Commented Nov 14, 2022 at 0:22
  • You will eventually need a closed set of some sort. (1) Non-Latin letters are allowed: \newcommand\邏{logic} \邏; (2) I had to make q the command character (like backslash) for a court transcript macro-set for touch-typing; (3) be careful of commands/macros that expand to text (e.g., accents, \TeX, \title, \author).
    – Cicada
    Commented Nov 14, 2022 at 12:43
  • cool thanks! Actually just made a list of acceptable characters which so far contains Latin characters + numbers + *, I think this covers everything I have done so far. Will be sure to create a separate file for that so that if someone forks my project can then add whatever they need! Commented Nov 14, 2022 at 15:06
  • 1
    @TommasoSeneci Digits are not allowed in control words; \12 is the control symbol \1 followed by 2 (not part of the name). In LaTeX, the * isn't really part of the name, but I guess that in your script you may consider it so.
    – egreg
    Commented Nov 14, 2022 at 15:07
  • Oh nice, that's what I needed to know! I have many backslash + 1 single digit, probably it's then bad practice, didn't know! And for the *, just need it for sections and equations, so it's come quite in handy to include it Commented Nov 14, 2022 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .