# Why is TeX/LaTeX-“programming” so different from other languages in terms of syntax?

Usually, e.g., when I want to have an if, I do something like this:

if(value) {
... whatever code ...
}


But in LaTeX, its:

\ifnum\count0<100
... whatever LaTeX-code ...
\fi


There are no brackets and so on, for example, which indicate where if-statements begin and where they end. Also, there are no brackets around if condition itself which is checked by LaTeX and so on.

Those are all minor differences, of course, but (from my experience) its way easier to read source from other languages than to read source which is part of a TeX-Document. This might, of course, be a personal thing because I program more often with non-LaTeX-languages than with LaTeX, but I still do not see the advantage of forcing users to use such a syntax.

Can anyone explain this to me?

-
when knuth created tex, he was intending it for use by himself and his secretary, and he designed the language that he thought was most appropriate for its intended use -- typesetting. he didn't foresee its adoption by so many other users. one must simply accept the fact that knuth doesn't think the same way that most other people do, and vice versa. –  barbara beeton May 23 '14 at 18:05
Your example is actually TeX syntax and not necessarily the same you would use in LaTeX, see etoolbox's conditionals for example. –  clemens May 23 '14 at 18:33
it is not only "minor differences": try \newif\iftestA \iftestA \let\x\iftrue \else \let\x\iffalse \fi \bye. Mastering the TeX syntax with conditionals takes a lot of time! The LaTeX and etoolbox conditionals are much safer \iftest{arguments}{YES}{NO} and such style is even almost necessary in certain circumstances. Naturally, underneath, it all boils down to careful wrappers of the TeX primitives. –  jfbu May 23 '14 at 18:53
Adding to what @barbarabeeton said, Knuth intended TeX to be what he considered to be an implementation of "literate" programming, that is, writing for code humans and not for machines. Most programmers have learnt to think like a computer does (e.g. they have a for each primitive instead of recursive loops)… TeX was never meant that way – it is meant to be written in a meaningful, readable order (no libraries or functions) and self-documented. Most humans that know programming languages do not think like Knuth, and do not see code as some form of prose. –  ienissei May 23 '14 at 20:50
Just to add my humble POV, difference is in the eye of the beholder. Syntax preference is a matter of taste and ideology; if you grew up accustomed to a certain construct or command pattern, there's a tendency for labeling different representations of the same lagic as wacky or strange. :) Besides, I have the impression Maslow's hammer applies here as well. :) I remember of the arithmetic IF in Fortran and different decimal marks in ALGOL, so your mileage may vary. Personally, I believe this question turns more opinion-based. –  Paulo Cereda May 24 '14 at 11:13

The if ... fi notation is fairly common (probably more so then) for example this shell (sh or bash) script echos yes or no depending on whether the argument is x

if [ "\$1" = x ]
then

echo yes

else

echo no

fi


what you consider normal depends a lot on what you are used to C programmers might expect

 (x<100)? "yes" : "no" ;


Perhaps even closer in nature, since they are both macro expansion languages of similar age, the C preprocessor tests such as

#ifdef ZZZZ
#define SOMETHING
#endif


which declares the macro SOMETHING if the macro ZZZZ is not declared and so it goes on.

-

This is mostly TeX (not the nice, structured LaTeX you normally write). Plus (La)TeX is a macro/markup language, most of what is written just gets passed through. Such languages have a distinct "feel" (if on Unix/Linux, take a peek at m4(1); check out the C/C++ preprocessor).

I just saw an announcement for lollipop here, it might be what you are looking for (I'm planning to check it out sometime).

-

TeX language mixes the objects which are intended to be executed and object for printing only. By default, when the object is nothing special then it is printed. This is first main difference between TeX language and common procedural languages. This approach gives you very powerful tool. This implies that the macro-expansion language is used. Compare:

hello world \end      % prints hello world in TeX
print{"hello world"}  % prints hello world in procedural language


The TeX braces {} (more precisely the characters with catcodes 1 and 2) have four different meaning in the language. This may cause a small problems sometimes but this enables amazing tricks too. The meanings are: (1) they must be always balanced (this is checked by TeX internal algorithms). (2) they affect the parameters reading and they are discarded by TeX internal algorithm when they are at the outer level in parameter text. (3) they delimit groups and borders of the boxes or math-sublists (they delimit groups too). The settings are local in such groups. (4) They delimit the text of the macro (after \def or companion).

Thus it is not so much good idea to give the fifth meaning for braces: for delimiting the conditionals. That is the reason why conditional are solved by different way. But macro programmer can prepare brace-oriented conditionals for users but he/she never uses such type of reducing.

The procedural language programmer says, for example:

if (a%2 == 0) {
x = 1;
} else {
x = -1;
}


but TeX macro programmer have more ways at what level of expansion the conditional is processed:

\valX = \ifodd\valA -\fi 1
% or:
\ifodd\valA \valX = -1 \else \valX = 1 \fi


I mean that TeX is very very powerful tool but there are only few people they are able to understand it and to utilize it efficiently. Unfortunately.

-