[Tex/LaTex] How does latex restore font after section heading

fonts

What I basically want to achieve is to know about every font change in the document. I have overriden the \selectfont command as it is used to activate new font according to LaTeX NFSS doc. My version of \selectfont does exactly the same as the original version and uses \message{\fontname\font} to print new font name to the log file.

Before the section heading I can see the font change for bigger bold font, but after the section heading there is no font change using \selectfont command. So how does LaTeX change the font back to normal size?

Best Answer

When you say {\bfseries text} (notice the braces), LaTeX will execute a \selectfont behind the scenes, but the chosen font will be used only until the closing brace is scanned.

Changing the current font is an assignment and all (well, almost all) assignments executed in a group are silently undone when the group ends.

This is very handy: you can change the current font without having to remember what was the previous one. So

Some text in roman type {\itshape and this is in italic,
{\bfseries while this is in bold italic} and this reverts to
italic} and back to normal.

will behave as expected. (Not that I recommend using commands in this way, it's just an example.) This feature is exploited, for instance, by the sectioning commands, that typeset the sectional title in a group, so that the font will be assigned locally.

What's a group in TeX sense? A pair {...} delimits a group (if the braces are not used to delimit the argument to a command); also \begingroup...\endgroup performs the same duty, and all LaTeX environments issue such commands at their beginning and end, so LaTeX environments form a group.

There are also "implicit" groups: the argument to \hbox and \vbox is implicitly grouped, so font changes in a \mbox or a \parbox (which are wrappers around the primitivies) don't spill out.

Note. There are some assignments which don't respect the group structure, but this is a rather esoteric topic. Font assignments are not among them.

Related Question