The scope of LaTeX commands

This is a very basic question. However, I didn't find simple rules for it, so I asked it here.

Some LaTeX commands (like \noindent) affect the whole paragraph. Some of them (like \emph) affect their argument (\emph{Text goes here}), and some (like \color) affect the remaining part of the paragraph (unless they're put in {}).

I don't know how to differentiate between these cases.

-
It's easier to think of them as switches (keep in effect until switched another way or end of the group, like \itshape or \color) vs. commands (take an argument and act on it, like \textit or \tetcolor). There's not really a way to distinguish them by naming convention or such, though \text.. is pretty much always a command. Also there's a trap: for some settings, the switch state at the end of the paragraph matters, so {\centering foo}\par does not work the way you might want, and {\centering foo\par} will. (\par ends the paragraph exactly like a blank line.) –  Ulrich Schwarz Dec 3 '10 at 19:37

2 Answers

The \noindent control sequence is not a macro, but a primitive. It is not something that remains in effect for an entire paragraph. It instructs TeX to leave (internal) vertical mode and enter horizontal mode. (I sort of described TeX's vertical and horizontal modes in a blog post.)

More relevant to your question, there's no good way to distinguish which control sequences have arguments. The LaTeX 3 team has taken some pains to provide a consistent naming convention that lets the user know how many arguments and what type are expected. See the expl3 documentation for details.

To complicate matters, there are many LaTeX macros which, strictly speaking, do not take any arguments, but do in reality. For example, according to TeX, the \emph macro takes no arguments. To see this, we can ask TeX to show the implementation of a macro using the \show primitive:

*\show\emph
> \emph=macro:
->\protect \emph  .
<*> \show\emph


This is a little tricky to read at first, but in the first line, I've asked TeX to show me the definition of \emph (the * is TeX's prompt). TeX responded by telling me that \emph is a macro which takes no arguments and expands to \protect \emph . (There's actually a space in the name of the latter control sequence, which is why there are two spaces between \emph and the period.)

As an example of a macro which takes arguments, we can define \def\foo#1{asdf} which takes a single argument. When we ask TeX for the definition, it responds as follows.

*\show\foo
> \foo=macro:
#1->asdf.
<*> \show\foo


Here, it's showing us that it takes a single argument (denoted #1) and then expands to asdf (note that we didn't actually use the argument).

What this means is that we can't even interrogate TeX to ask it which macros require arguments and which do not. (Technically speaking, the problem of deciding if a given macro requires an argument is undecidable. The proof is a simple reduction to the Halting problem.)

Unfortunately, this means that differentiating control sequences that require arguments from those that remain in effect until some point in the future (like the end of a group, or the end of a paragraph) from those that perform some discrete action right then is just a matter of experience and reading documentation. Hopefully LaTeX 3 will improve the situation.

-
Thanks. Just curious, do you know when LaTeX 3 is going to launch? –  Sadeq Dousti Dec 4 '10 at 8:22
@Sadeq: I'm afraid not. Someone recently described it as coming real soon now for the past 10 years. –  TH. Dec 4 '10 at 9:29
@TH Thanks for a better post than mine below! Small addition \em and \emph are redefined in LaTeX in ltfntcmd.dtx. \emp{Test} will print differently from \emph Test! –  Yiannis Lazarides Dec 4 '10 at 16:19

Three issues are raised by your question. First the easy one arguments. When you say \emph{text goes here} you are calling a macro that has been defined with one argument, possibly something like this:

\def\emph#1{\textit{#1}}


In this case most macros will affect the enclosing text only.

When you get commands like \noindent \small etc they normally continue until redefined again. This is just a convention, commands don't need to be defined this way. It is a convention started with Knuth. Think of them as globals in other programming languages or as cascading in CSS.

When you use {} you are grouping or scoping the command(s). The grouping mechanism can be thought of a bit like scope in other programming languages, with the exception that in TeX the mechanism is much more powerful. Most assignments made inside a group are local to that group unless explicitly indicated otherwise, and outside the group old values are restored (pretty much like in Pascal). If we type the following example:

   \def\anumber{42}
\anumber
{
\def\anumber{43}
\def\b{2}
\anumber
}
\anumber


The macro anumber will have a different values within the group than outside it. There are a lot of commands associated with grouping and differentiating between local and global definitions.

-