[Tex/LaTex] Kerning between bold and non-bold text

boldfontsgroupingkerning

I'd like to write a word in bold followed by a comma and a non-bold word, like this:

\textbf{why}, blah

If I just write why, TeX inserts a kern to shift the comma a little closer to the "y". But when I write {why}, (even without the \textbf), TeX doesn't insert the kern; apparently kerning doesn't happen across a group boundary. If I write {why,} then I get the kern, but I also get a bold comma.

What I'd like to know is:

  • Is there a way to make TeX recognize that a pair of letters should be kerned even when there's a group boundary in between?
  • Stylistically, should that comma be bold?

Best Answer

If you want to add a kerning based on the "outer" font, you can define

\expandafter\def\expandafter\nocorrlist\expandafter{\nocorrlist\?}
\newcommand{\?}[2]{%
  \sbox0{\hbox{#1}\hbox{#2}}%
  \sbox2{#1#2}%
  \kern\dimexpr\wd2-\wd0\relax#2}

and

\textbf{why}\?y,

will add the kerning and the comma (also ignoring the italic correction). The usual meaning of \nocorrlist is ,.; with the first line we add \? to the list without the need to know the current meaning.

A different approach (the hint was in Wyzard's comment) is to define a new command:

\makeatletter
\newcommand{\highlight}[1]{%
  \def\hl@text{#1}\futurelet\hl@next\hl@dokern}
\def\hl@dokern{%
  \sbox0{\textbf{\hl@text\/\hl@next}}%
  \sbox2{\textbf{\hl@text\hl@next}}%
  \textbf{\hl@text}%
  \kern\dimexpr\wd2-\wd0\relax
  }
\makeatother

that possibly applies the kerning only if the character following \highlight{word} is a space.