I'm writing a LaTeX commands highlighter in C#, for this I need all or most of the LaTeX commands. I've searched the web and found many lists of commands but all of them are with comments and explanations that I don't need. Before I'll spend entire week deleting the unnecessary comments leaving only the commands. I would like to ask you, is there somewhere a simple text list of the commands, like this:

  • 6
    There is no such list. Most people do highlighting using TeX's own rules: a command is \ followed by one non-letter or one or more letters. (That's assuming you are not also wanting internal commands, which in traditional TeX may also contain @ as a 'letter'.) – Joseph Wright Jan 16 '13 at 19:51
  • As that is useful to you, I'll post as a slightly expanded 'proper' answer. – Joseph Wright Jan 16 '13 at 20:16
  • 2
    Don't use regex for this. Regex is for processing text, not structured languages. There are tons of good parser-combinator libraries for .NET, the best of which, in my opinion, is FParsec (though that requires F#). Using a parser means your code is readable, reusable, maintainable, and improvable. A parser allows you to capture the semnatic content. Regex allows you to capture characters. – GregRos Jan 16 '13 at 20:37
  • Not familiar with F# at all. Just heard that it exists. – MaD Jan 16 '13 at 20:48
  • 1
    To underline @Greg's point, here is my earlier try at some TeX rexps. – morbusg Jan 16 '13 at 21:04

While it is possible to list all TeX primitives or all LaTeX kernel commands, there is no list of all possible LaTeX commands as new ones can be added by packages. As such, most people use an approach of searching for the same pattern as TeX does: \ followed by either one non-letter or one or more letters: this is the pattern for 'document commands'.

(It is possible for commands not to start with \, but this is used for internal coding and not in documents. Similarly, internal commands often use one or more extra 'letters': @ is traditional while LaTeX3 uses _ and :. However, for the purposes of highlighting in a document these cases can be ignored.)

  • How is this particular RegExp looks like? Having difficulty to create such myself :( – MaD Jan 16 '13 at 20:21
  • 7
    \\([^a-zA-Z]|[a-zA-Z]+) – David Carlisle Jan 16 '13 at 20:40
  • @David Carlisle Thought about it but it seems that I also catch illegal commands such as chapter\ instead of \chapter – MaD Jan 16 '13 at 20:46
  • No it means literal \ followed by the choice group of a single non letter [^a-zA-Z] or 0-more letters [a-zA-Z]+ so would not match chapter\ – David Carlisle Jan 16 '13 at 20:49
  • + is 1 or more (which is what you want) not 0 or more as I mis-wrote above – David Carlisle Jan 16 '13 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.