[Tex/LaTex] the deal about these semantic vs syntactic commands

languagesmacrossemanticssyntax

I keep hearing people on this site talking about "semantic" vs "syntactic" commands and that the "semantic" ones are somehow better. I don't really get what they are talking about, could you explain and give some examples that show the difference.

Should I care about this at all?

Best Answer

Suppose, purely for the sake of argument, that one were typesetting a book which had frequent occasion to reference publications by name --- Science, the New York Times and so forth. In a WYSYWYG editor, one would highlight the name of the journal and click the I button. With syntactic markup, like plain ol' HTML, one would wrap the journal name in codes indicating that it is to be italicized: <i>Science</i>. With semantic markup, by contrast, one could define a command which says, "This text is the title of a publication, and is to be handled in such-and-such a manner". Then, if one wished to change the style of the document, one could tweak the code defining that command, and the change would be reflected throughout, without having to modify each instance manually (and run the risk of missing something).

This has the added advantage that one can build extra features into the command. Instead of expanding out as "put this text in italics", the "publication name" command could do something more elaborate: "put this text in italics, and add the appropriate entry to the book's index". That way, a reader who wanted to find all the places where the book references a New Yorker article or an item in the IEEE Transactions on Love Poetry could just flip to the back and look up the pages.

The LaTeX implementation of that idea would look something like this, using the makeidx package to implement the indexing:

\newcommand{\journal}[1]{\emph{#1}\index{#1@\emph{#1}}}

Having defined the \journal command in this way, we could type \journal{Science}, and LaTeX would handle both the visual formatting and the auxiliary function of indexing for us.

Edit to add: Yes, this can easily become overkill. But it's fun!

Related Question