[Tex/LaTex] Syntax of TeX/LaTeX


{} and [] do some kind of grouping, and comma seems to be special as well. Being a programmer I find it frustrating to use Latex on incantation-only basis. I would like to know how does it exactly work.

What is exactly the syntax of TeX/LaTeX?
How is it parsed?
Does Latex introduces new commands only or changes the syntax as well?
Or maybe changing syntax locally is a inherent feature of Tex?

Best Answer

For a programmer, I would recommend reading the book "TeX by Topic" (available for free at http://www.eijkhout.net/tbt/). I think that that will give you the best answer to your general questions (which really are, in my opinion, a bit too general for this site; I would advise you to read TbT and then ask more focussed questions on particular aspects).

However, you mention a couple of specifics so let me try to answer them. {} are lexical:


produces: "hello world".

The grouping given by square brackets is an illusion. Some commands in LaTeX start with "If the next character is [, gobble the rest up to ] and use it as the first argument.". This can cause problems if, for example, what you want to pass has a square bracket in it:

\newcommand{\hello}[2][hello]{#1 #2}
\hello[Greetings [1] to the]{world}


hello world
greetings world
Greetings [1 worldo the]world

because the first argument to "hello" in the last line is "Greetings [1".

(This can be remedied by using braces to do proper grouping, \hello[{Greetings [1] to the}]{world} would work fine).

I've never heard of commas being special. Can you give an example?

Related Question