5

I've always been confused by the differences in behavior of font commands.

For example, we have \mathrm{} and \texttt{}, which can be used like this: \texttt{hello} world.

And then we have \it and \bf which must be used as {\it hello} world. If one uses \it{hello} world, both words are italicized.

Internally, what is the difference here? And was this difference a conscious design decision? If so, why?

2 Answers2

8

Primitive font commands (none of which you show in your example) work like \bf and affect all text for the rest of the current group so the usual syntax is {\it hello\/} although the outer group isn't needed, you could do \it hello\/ \rm world with no grouping.

The syntax is efficient but somewhat counter-intuitive but in plain TeX and LaTeX2.09 all font commands were that way. LaTeX2e introduced and encouraged the \textit form which uses an argument syntax and automatically handles italic correction \/. They are actually simple wrappers around the declaration forms such as \itshape. The old "2-letter" names like \bf that were defined in plain and LaTex2.09 are not defined at all in the latex2e format but many classes include a compatibility package that defines them for old (ie pre 1993) document fragments. \bf is then defined to be more or less \normalfont\bfseries.

David Carlisle
  • 757,742
5

You're confusing switches with macros. While the former is still a macro (in the TeX sense), it does not take an argument. As such (and I'm using the preferred choice as contained within Does it matter if I use \textit or \it, \bfseries or \bf, etc), using

\itshape{hello} world

makes \itshape span within the group that it's used, and there's none there. However,

{\itshape hello} world

prevents the switch \itshape to span outside the group in which it's used. \texttt (and friends) take an argument, and then place it inside a group. You can see this by viewing the .log file after compiling the following MWE:

\documentclass{article}
\def\pshow#1{{\let\protect\show #1}}% https://tex.stackexchange.com/q/117169/5764
\begin{document}
\pshow\texttt

test
\end{document}

The .log shows

\texttt =\long macro:
#1->\ifmmode \nfss@text {\ttfamily #1}\else \hmode@bgroup \text@command {#1}
\ttfamily \check@icl #1\check@icr \expandafter \egroup \fi .

where you can clearly see \texttt{<stuff>} translates to the grouped {\ttfamily <stuff>} (simplified).

Werner
  • 603,163
  • 2
    There is a difference between {\itshape abc} and \textit{abc}, which is the reason why the latter form is to be preferred. – egreg Dec 17 '13 at 21:18