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.
Compile this with XeLaTeX
\documentclass{article}
\usepackage{color,xesearch}
\SearchList*{redwords}{\textcolor{red}{#1}}{hello}
\begin{document}
This is hello and this is hellow.
\end{document}
But don't use it. If you want to specially mark a word, use a macro.
Why? Relying on automatic procedures is always risky: if I have a choice, I prefer explicit mark up. Another reason is that xesearch
is resource hungry and currently unmaintained.
There's nothing similar for pdflatex
; something can possibly be done with LuaLaTeX. I tried chickenize
but it doesn't seem to offer the feature.
Best Answer
If you want to switch to a fixed-width font, you can load any fixed-width font package (such as
tgcursor
orinconsolata
) and use\texttt
. You will rarely if ever want to use more than one fixed-width font in the same document.If you do need to use a specific font that isn’t one of the predefined font families, you either declare the command with
\newfontfamily
in the modern toolchain withfontspec
, or\fontfamily
in legacy NFSS.I would recommend that you use
\DeclareTextFontCommand
to define the command you will be using inside the body of your document. This both takes care of some of the fiddly little details, such as italic correction, and also lets you change how you load the font without changing the body in dozens of places. For example,could be replaced with
to compile with the legacy toolchain.