# How does latex restore font after section heading?

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?

-
Welcome to TeX.SE! You mention that you've created an alternative version of the \selectfont command; please consider adding the new code to your posting so that others may understand what you're doing. Thanks. – Mico May 4 '12 at 16:27
Are you familiar with \usepackage[debugshow]{tracefnt}? Maybe this can help with part of what you are doing. Note that it can occasionally change the layout of your document a little, so it's best to use it only for debugging, not for the final output. – Lev Bishop May 4 '12 at 19:13

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.

-
Thank You very much, that brought some light to it, I guess now I just need to find a way, how to detect the end of a group. – Igor Liska May 4 '12 at 16:39
@IgorLiska Look into \aftergroup – Lev Bishop May 4 '12 at 19:02