24

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?

4
  • 1
    You stole my question, I stole your answer. Jul 29, 2010 at 20:25
  • That's only fair ;) Jul 29, 2010 at 20:44
  • 1
    I've just realised that I've been getting these confused! Ooops. **Quietly goes back through old answers changing them before anyone else notices.** Jul 29, 2010 at 21:16
  • Should this be part of the FAQ? Jul 29, 2010 at 22:48

3 Answers 3

29

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!

4
  • 4
    In my opinion, this is the correct answer. The difference between semantics and syntax, as usually discussed, relates to mark-up. Jul 30, 2010 at 0:56
  • 3
    Isn't HTML semantic? The difference between <h1>, <h2>, and <h3>, determined by a CSS rule, and \section,\subsection and \subsubsection determined by the LaTeX article class is very subtle. If it exists, I'm not seeing it. I understand the difference in terms of use- LaTeX allows the redefinition of the markup, while HTML only has a standardized markup language, but both are semantic when used correctly. Semantic HTML: en.wikipedia.org/wiki/Semantic_HTML has always been present in the standard, it's just been misused. Jul 30, 2010 at 2:12
  • Yes, that's why I threw in that modifier about "plain ol'" HTML (which might not've been the right modifier to use). Maybe BBCode would make a better contrast. Jul 30, 2010 at 4:01
  • I would think that the greater risk is getting something else by mistake - you change your formatting for journals and accidentally change the your formatting for (some) things that you wanted to emphasise as well. But as you were.
    – Jivlain
    Jul 30, 2010 at 7:23
18

Semantic is defined as "Of or relating to meaning". Syntactic is defined as "described by grammatical structure." When expressions are simple, it's fairly easy to read either structure. When they're complex, it's much easier to read things which are defined by the meaning of their expressions rather than by the order of their symbols.

\set{a, b, c} is semantic, it tells you there is a set of three things: a, b, and c. Instead, \{a, b, c\} tells you there is an opening left brace, followed by some symbol a, followed by some coma, followed by ..., but who knows what those symbols mean? Both expand to the same thing, assuming a properly defined 'set' macro, but the former is easier to read, understand, and debug in source code.

5
  • 7
    Semantic markup also helps if you later change your opinion how something should be formatted (e.g. you decide that bold for vectors looks better than putting an arrow on top of them). Then you can simply redefine the macro.
    – Caramdir
    Jul 29, 2010 at 20:52
  • I didn't know you could do \set{...} !!
    – Suresh
    Aug 17, 2010 at 17:37
  • 1
    @Suresh: you can't :) The example was hypothetical. Sep 14, 2010 at 6:24
  • 2
    dang. that would have been neat. I'm conditioned to expect to learn something new every time I visit here ;)
    – Suresh
    Sep 14, 2010 at 7:15
  • 2
    @Suresh:\newcommand{\set}[1]{\left\{#1\right\}} Mar 6, 2013 at 21:10
1

Consider hello!, bon jour! sup!. Natural and computer language taxonomy makes (amongst others) these distinctions:

  • lexical : each occupies a different place in any lexical ordering (e.g., a dictionary)
  • syntactic : most grammars (or 'production rules') parsing these terms would differ in the sense that two of the three would swallow two tokens (don't forget the !), while the other would consume three. To clarify, syntax (or grammar) is the set of rules governing how and where terms can legitimately stand in relationship to one another in a valid program
  • semantic : the three utterances broadly intend or mean the same thing
  • pragmatic : while the producers of these lexically, syntactically and semantically valid utterances all intend to convey a greeting, the context, place or pattern of their usage will determine their "efficiency" (not just in the program, but also within the community of producers, consumers and cultural guardians of the patterns).

Of course, this taxonomy applies as equally to TeX/LaTeX as any other language that might or might not exist (swap the word phonemic for lexical in purely spoken lingua if you like).

0

You must log in to answer this question.

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