Most commands in Latex are written with the arguments inside braces following the command: \command{arguments}
. But text formatting options, like \em
, or \scriptsize
are usually written inside the braces, like {\em some italicised text}
. Why is this? What makes these commands different? I'm particularly asking because it seems to make standardisation – of which LaTeX really appears badly in need – more difficult.
[Tex/LaTex] Why are text format commands used inside braces
bracesfontsizeformattingsyntax
Related Solutions
One thing to note is that TeX's only means of nesting arguments are braces. You can define a macro \def\whatever[#1]{...}
but when you call it as \whatever[oh[well]]
, things go down the drain awfully. Calling it as \whatever[{oh[well]}]
however works swimmingly, and \whatever
never notices it has been taken for a ride by slipping a ]
into its argument. So the braces can be used as a means of hiding occurences of the closing delimiter from TeX without actually affecting the intended argument.
It also means that whenever you call a macro using delimited arguments with a non-literal argument (more exactly, an argument not completely under your own control, as it often happens when you write a macro package to be used by others), you should always add a layer of braces around each delimited argument, like \whatever[{#1}]
or similar. There is no other way to ensure that arguments will not get chopped up into something different because they themselves may contain a closing bracket.
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 preamble
\newcommand{\mli}[1]{\mathit{#1}}
% macho programmer version
% \newcommand{\mli}{\mathit}
so 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
\usepackage{amssymb} % or amsfonts
\newcommand{\numberset}[1]{\mathbb{#1}} % or \newcommand{\numberset}{\mathbb}
\newcommand{\N}{\numberset{N}}
\newcommand{\Z}{\numberset{Z}}
\newcommand{\Q}{\numberset{Q}}
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
\cleardoublepage
\thispagestyle{empty}
\vspace*{\stretch{1}}
\begin{flushleft}
\sffamily\slshape
To the masters in directing,\\
John Ford, John Ford and John Ford
\end{flushleft}
\vspace*{\stretch{2}}
\cleardoublepage
(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 a dedication
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.)
Best Answer
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:
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: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
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.