I've examined this in a bit more detail in my blog, but for the purposes of answering the question I'll do a bit of editing and post the key points here.
First, the standard \newcount
, \newtoks
, etc., functions allocate globally. So something like
\begingroup
\newcount\mycount
\endgroup
will permanently consume a register, even though \mycount
disappears at the end of the group. The etex package provides \loccount
, \loctoks
, etc., which do free up the allocation at the end of a group. So
\begingroup
\loccount\mycount
\endgroup
is at least not going to consume registers. However, there are other issues.
In most languages, a local variable is local to some function, and nested functions have there own independent local variables. In TeX, things are different, as it is a macro language and only grouping makes things local. So something like
\def\BadIdea{%
\loccount\mycount
...
}
will not destroy \mycount
at the end of the material inserted by \BadIdea
. On the other hand, things will work within a group, so doing
\def\BetterIdea{%
\begingroup
\loccount\mycount
....
\endgroup
}
will destroy \mycount
as expected. However, you'd get much the same effect by doing
\newcount\mycount
\def\BetterIdea{%
\begingroup
....
\endgroup
}
or even (if you know it's safe)
\def\BetterIdea{%
\begingroup
\@tempcnta=<value>
....
\endgroup
}
i.e. using the general scratch registers.
As local variables for TeX are about groups, and not about macros, I think that best practice remains to allocate globally and use locally. With my LaTeX3 'hat' on, I'd point out that we did try some local allocations out, and when we worked things through decided it was a bit of a nightmare.
There is a standard. It's a matter of opinion whether it's sensible or not, but it's been there since the release of LaTeX2e.
Font aspect changing commands have two forms: action and declaration
The same holds for abstract instruction such as "emphasize"
Size changing commands have only the declarative form.
Let me review the three points.
Font aspect changing commands
The declarative form for changing the font's aspect is formed from a prefix denoting briefly the purpose and by a suffix denoting the attribute that's changed:
\sffamily
\itshape
\bfseries
The "action form" is a command that takes an argument; all kernel defined commands of this type have a prefix text
and a suffix corresponding to the similar declaration:
\textsf{words}
\textit{words}
\textbf{words}
The fundamental difference is that a declaration's scope is up to the end of the group in which it's issued (or a countermanding declaration).
Two important commands are \textnormal
(action) and \normalfont
(declaration) that instruct LaTeX to use the font which was current at document start.
Abstract instruction
By "abstract instruction" I mean \emph
and \em
, which usually act by italicizing, but one should not rely on this, as the effect can be redefined by the document class.
The form \emph
is the action: \emph{words}
, while \em
is a declaration. The latter can be useful in defining environments.
Size changing commands
The size changing commands have only the declarative form, as very rarely one needs to set a couple of words in a smaller or larger size. Examples
\footnotesize
\small
\normalsize
\large
Their scope ends with the group where they have been issued (usually an environment). If a paragraph must be typeset in a different size it's important to remember marking explicitly the end with \par
or a blank line. Commonly it's the environment where they are issued that takes care of it.
Best Answer
I beg to disagree with some of the opinions expressed in the linked page. The list of commands to avoid doesn't distinguish between commands to avoid at all costs and ones that should be used to define personal macros in the preamble, for instance; some of the commands listed do have their place in a document body.
Note The following should not be taken as "do so or you're wrong"; most of the recommendations make little sense for a short document where one look is sufficient to control it as a whole and where defining personal commands for just a single use would be a waste of time. For short documents only the first two sections are to be strictly followed.
Commands that must never be used
The two letter font changing commands such as
\bf
,\it
,\tt
or\sf
must never be used in a document, either in the body or in the preamble. They have been obsolete since the introduction of LaTeX2e in 1992.The trouble with
\\
Somebody may remember Alfred Hitchcock's movie "The Trouble with Harry" where a dead body appears and causes any sort of troubles among a quiet small town in New England. Our
\\
is quite similar.The
\\
command should very rarely mean "break a line" except in environments where line breaks are to be marked explicitly:flushleft
,flushright
,center
,tabular
,tabbing
,align
,gather
,multline
and derived ones. However, in some cases it can be used, but not for denoting an end of paragraph or to "leave a blank line".The
\noindent
command is quite similar: I don't think to have used it in a document body other than in very particular situations involving explanations of TeX programming (such documents often require manual tweakings, but are the exception rather than the rule). If one needs\noindent
once in a document, then there's something wrong with the way the document is being prepared.Font changing commands
The "modern" font changing commands are to be considered as tools for building logical structures. For instance,
\textit
or\mathit
(the difference is that the former respects spaces in the input because it switches to text mode) should be used for multiletter identifiers in math formulas. If our variable is called difference, then$difference=a-b$
is wrong, while$\mathit{difference}=a-b$
is right. However, it's much better to say, in the preambleso that it can be redefined at will, if we change our mind about how to denote those variables: just modify the definition of
\mli
, which avoids hunting in the document for\mathit
.If we want a uniform appearance of symbols for number sets, it's better to say
so that changing the meaning of
\numberset
will modify all renderings of\N
,\Z
and\Q
.The same considerations hold for the other font changing commands: look for what appears to be a logical unit in your document and define a macro (or environment) for it.
An exception is
\emph
which already is a high level command: it means emphasize and it's left to the class (or to the user) how to implement emphasis.Spacing commands
From a theoretical point of view,
\hspace
and\vspace
have no place in the document body. However, an occasional construction may need them without bothering to define an environment for a single usage. For instance, if we want a dedication page, it's easy to say(I assume that the package
emptypage
is loaded or the class allows for specifying that blank pages automatically inserted are really blank) rather than defining adedication
environment to be used only once.In the body of the document
\medskip
or\bigskip
may be used, but also for them it would be better to define our personal spacing commands that use them, so that we can rely on uniformity of appearance.Line and page breaking commands
One should avoid explicit line and page breaking commands, with the exception possibly for
\cleardoublepage
that's sometimes necessary when dealing with manually inserted hyperlinks.In the final revision of a document it may be necessary to insert some
\linebreak
,\pagebreak
or\clearpage
command. The choice is between defining a personal command that can so be disabled in case a new edition is prepared or inserting them accompanied with a clear and univocal comment line. In both cases, the final TeX document shouldn't have trace of such commands that reveal to be obsolete in the new edition, so a search for them has to be made anyway.The same considerations apply for
\enlargethispage
.Math mode commands
While
\limits
in inline math formulas should not be used, it reveals useful after\int
; if you have an integral over a domain rather than with bounds and the domain symbol is "large", saying\int\limits_{\partial A\int B}
is surely better than setting the domain on the side.I don't agree with the assertion that
\dfrac
and\displaystyle
should not be used in the document body. This is false and there are many situations in which they are useful: complicated formulas must be built from smaller pieces and some of them may need particular treatment.\tag
and\eqno
While
\eqno
is not a LaTeX command and should not be used,\tag
is surely something that can be employed. In many fields of mathematics, papers don't have numbered equations and only some of them need a reference. Many authors prefer not having numbers that would be difficult to find, but rather a symbol or word that can better identify a formula.(Prof. A. J. Roberts does Mathematical Analysis, where numbered equations are the norm. It's common among analysts to think that what's done in their field is done in the same way elsewhere. Well, it isn't.)