# Kerning between bold and non-bold text

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?
-
From Bringhurst Elements of Typographic Style p. 60: "When boldface is used to emphasize words, it is usually best to leave the punctuation in the background, which is to say, in the basic text font. It is the words, not the punctuation that merit emphasis ..." – Alan Munn Oct 10 '11 at 3:46
@Alan: I suggest adding this as an answer, even though it may only be partial, since it provides an additional source in answer to the question. – Werner Oct 10 '11 at 4:06
While, in principle, a kerning in your situation should be applied, the problem of kerning characters belonging to two different fonts is rather difficult: what kerning should be applied? That prescribed by the first font, the second or an average of the two? Take also into account that some letters change shape quite dramatically in different fonts even of the same family (think to an italic "f" or also to a "g"). No universal rule can cover all possible variations. – egreg Oct 10 '11 at 10:25

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.

-
I took the basic approach you demonstrated (comparing the width of boxes with and without the kern) and made a \highlight command that sets its argument in bold, but uses \futurelet to peek at the next token and insert an appropriate kern for it. So I can just write \highlight{why}, blah and I get a properly-kerned comma. – Wyzard Oct 11 '11 at 5:18
I also found that it looks better if I use \bfseries in the two boxes, so I get the (larger) kern from the bold font. – Wyzard Oct 11 '11 at 5:20
In your \highlight, why the special case to check whether the next character is a space? The box width comparison seems to work fine on spaces; you just get a difference of zero. (In my implementation I calculate the width difference using \@tempdima, and use \ifdim to skip inserting the kern if its size would be zero.) Am I overlooking something? – Wyzard Oct 11 '11 at 12:53
@Wyzard You're right; the space doesn't need to be treated differently. I wouldn't bother avoiding inserting a zero kern, as it doesn't make any substantial difference (words can't be hyphenated past explicit kerns). – egreg Oct 11 '11 at 13:06
One other issue I encountered in my version (and probably in yours, though I haven't tested it) is that when \textbf looks ahead to decide whether to add italic correction, it sees macro stuff instead of the "real" next character, so it adds the correction even when it shouldn't. I ended up using \unkern to remove that, then doing another box-width comparison to decide whether to add it back. (It'd be more straightforward to just check \hl@next against \nocorrlist to decide whether to \unkern, but I'm not sure how to do that and haven't researched it yet.) – Wyzard Oct 11 '11 at 15:08
1. TeX does not kern between two different fonts. The bold and plain variants are from a different font (cmr10, cmb10).
2. LuaTeX kerns across { and }, so {why}, gets its kerning, but not (see point 1) {\bf why},. PDFTeX (and older) behave as Knuth's TeX which does not kern across groups.
-
Re 1. nothing I know of can kern between different fonts, unless (hypothetically at least) using some sort of generated on-fly "optical" kerning. – Khaled Hosny Oct 11 '11 at 0:20
Right, I just wanted to explain that this is not only due to the braces. – topskip Oct 11 '11 at 4:42

Since now I've cooked up a potential solution, I'll requote Bringhurst from my comment:

From Bringhurst Elements of Typographic Style p. 60:

"When boldface is used to emphasize words, it is usually best to leave the punctuation in the background, which is to say, in the basic text font. It is the words, not the punctuation that merit emphasis ..."

So if you follow this, then your problem persists. Here's a solution using the xspace package. As egreg notes in the comments, each character requires a different kern, so the solution below is a compromise. As you can see from the comparisons below, you don't gain much by doing this, but if you make the kern bigger (to make the 'y,' or 'w,' case look nice) the comma will appear too close to most of the other characters.

As an actual solution to the problem, I would probably insert a manual kern in the few cases where this arises in your document. (Not to mention that the vast majority of people won't notice it.)

\documentclass[12pt]{article}
\usepackage{xspace}
\makeatletter
\DeclareRobustCommand\xspace{\def\mykern{\kern0pt}\@xspace@firsttrue
\futurelet\@let@token\@xspace}

\def\@xspace@check@token #1{%
\ifx#1,%
\def\mykern{\kern-.04em}% this is a compromise value
\else
\def\mykern{\relax}%
\fi%
\ifx\@xspace@q@nil#1%
\expandafter\@xspace@break@loop
\fi
\expandafter\ifx\csname @let@token\endcsname#1%
\let\@xspace@maybespace\expandafter\mykern
\expandafter\@xspace@break@loop
\fi
\@xspace@check@token
}

% Not part of the solution, just a quick way to generate output
\newcommand{\textbfk}[1]{\def\tmparg{#1}\textbf{\tmparg}\xspace, with kern\par\textbf{\tmparg}, without kern\par}

\makeatother
\parindent=0pt
\begin{document}
\textbfk{wha}
\textbfk{wha}
\textbfk{whb}
\textbfk{whc}
\textbfk{whd}
\textbfk{whe}
\textbfk{whf}
\textbfk{whg}
\textbfk{whh}
\textbfk{whi}
\textbfk{whj}
\textbfk{whk}
\textbfk{whl}
\textbfk{whm}
\textbfk{whn}
\textbfk{who}
\textbfk{whp}
\textbfk{whq}
\textbfk{whr}
\textbfk{whs}
\textbfk{wht}
\textbfk{whu}
\textbfk{whv}
\textbfk{whw}
\textbfk{whx}
\textbfk{why}
\textbfk{whz}

\end{document}


-

In answer to your second question, here's an extract from the Chicago Manual of Style (CMS) regarding the shape of punctuation (italics or bold) following text that has that particular shape:

6.2 PUNCTUATION AND ITALICS

All punctuation marks should appear in the same font--roman or italic--as the main or surrounding text, except for punctuation that belongs to a title in a different font (usually italics). So, for example, the word and, which in this sentence is in italics, is followed by a comma in roman type; the comma, strictly speaking, does not belong to and, which is italicized because it is a word used as a word [...]. Of course, it may be difficult to tell whether a comma is in italics or not (to say nothing of periods); for other marks it will be more evident. [...] In the first four examples that follow, the punctuation marks next to italic text belong with the surrounding sentence and are therefore presented in roman. In the last two examples, the two punctuation marks that belong with the italic titles--the exclamation mark following "Help" and the comma following "Eats"--are in italics (the comma following "Leaves" is in roman).

• For light amusement he turns to the Principia Mathematica!
• How can they be sure that the temperature was in fact rising?
• The letters a, b, and c are often invoked as being fundamental.
• I had yet to consider the central thesis of Malthus's Essay: the imperfectibility of humankind.

but

• The Beatles' Help! was released long before the heyday of the music video.
• I love Eats, Shoots & Leaves, [...]

6.3 PUNCTUATION AND BOLDFACE OR COLOR

The choice of boldface (or, by extension, type in a different color), unlike that of italics (see 6.2), is more often an aesthetic than a purely logical decision. Punctuation marks following boldface or color should be dealt with case by case, depending on how the boldface is used. In the first example, the period following "line spacing" belongs with the boldface glossary term and is therefore set in bold; the period following "leading" is part of the surrounding sentence and is therefore not set in bold. In the middle two examples, the punctuation next to the boldface terms belongs with them, like the first period in the first example. In the final example, the question mark belongs to the surrounding sentence and not to the boldface word.

• Figure 6. Title page from an apocryphal Second Poetics.
• For sale: three ten-year-old CPUs and five refurbished monitors.
• Will the installation remain stalled until I choose I accept?

6.4 PUNCTUATION AND FONT--AESTHETIC CONSIDERATIONS

According to a more traditional system, periods, commas, colons, and semicolons should appear in the same font as the word, letter, character, or symbol immediately preceding them if different from that of the main or surrounding text. In the third and fourth examples in 6.2, the commas following a and b and the colon following the Malthus title would be italic, as would the comma following the book title (Eats, Shoots & Leaves) in the last example. A question mark or exclamation point, however, would appear in the same font as the immediately preceding word only if it belonged to that word, as in the title Help! in 6.2. This system, once preferred by Chicago and still preferred by some as more aesthetically pleasing, should be reserved--if it must be used--for publications destined for print only. In electronic publications, where typeface may be determined by content as well as appearance (e.g., a book title might be tagged as such, separate from any surrounding punctuation), the more logical system described in 6.2 should be preferred.

It seems like both are valid, at least according to CMS. I would opt for the CMS' preferred option in both cases (bold and italics). That is, to keep punctuation formatting separate from text typesetting.

In agreeance with this, Wikipedia's Manual of Style (MoS) also suggests separating the shape of the text and succeeding punctuation.

-
Hmm. I think my situation is most similar to the first three examples in section 6.3, which do put the punctuation in bold. I used simplified dummy text in the question because I wanted to focus on the technical issue, but it's actually a bullet point in a resumé: Foobar University, Anytown, USA. – Wyzard Oct 10 '11 at 4:20