# Macros that can lead to double periods at end of sentence

Suppose that a document makes a heavy use of one of the abbreviations PhD, Ph.D., DPhil and D.Phil., and for the sake of consistency the author decides to define a \PhD macro and use it whenever they need the abbreviation. So, they pick up on of these definitions:

\def\PhD{PhD}%
\def\PhD{Ph.D.}%
\def\PhD{DPhil}%
\def\PhD{D.Phil.}%


Now, the problem is that a sentence like

A "graduate student" is a student pursuing education past the bachelor's degree, such as a master's degree or a \PhD.

could be typeset with two periods at the end depending on the definition of \PhD. How can we avoid this problem without giving up the idea to use a \PhD macro? (Assume that we can change the macro definitions.)

• \makeatletter\def\PhD{Ph.D\@ifnextchar.{}{.}}\makeatother – Manuel Dec 15 '14 at 11:28

Periods are a strange beast, because the space factor must be set correctly (of course, if you use \nonfrenchspacing).

\catcode@=11
\def\checkperiod{\futurelet\next\check@period}
\def\check@period{%
\if\noexpand\next.%
\spacefactor\sfcode.
\expandafter\@gobble
\fi}
\long\def\@gobble#1{}
\def\@{\spacefactor\@m}
\catcode@=12

\xspaceskip=4em % just to verify

\def\PhD{PhD\@}

A \PhD{} and another \PhD. End.

\def\PhD{Ph.D.\checkperiod}

A \PhD{} and another \PhD. End.

\def\PhD{DPhil}

A \PhD{} and another \PhD. End.

\def\PhD{D.Phil.\@\checkperiod}

A \PhD{} and another \PhD. End.

\bye


The setting of \xspaceskip is just to show that the space factor is computed correctly.

In the case of “no period in abbreviation”, the macro \@ borrowed from LaTeX sets the space factor to 1000, so a period after \PhD with the first definition will be still considered sentence ending.

• It should be possible to leave out the {} when calling the macro, right? So A \PhD and another…, instead of \PhD{}. – jmc Dec 15 '14 at 14:20
• @jmc Not really. In LaTeX there's the xspace package, but it's complicated to port to Plain and not really useful. Instead of {} one can use “control space”. – egreg Dec 15 '14 at 14:30
• — I see. Thanks! I didn't really register that xspace-like behaviour is not (easily) available in Plain-TeX. – jmc Dec 15 '14 at 15:09

I don't know if this was your intend by I try it:

\long\def\isnextchar#1#2#3{\def\tmpa{#2}\def\tmpb{#3}%
\let\tmp=#1\futurelet\next\isnextcharA
}
\def\isnextcharA{\ifx\tmp\next\expandafter\tmpa\else\expandafter\tmpb\fi}

\def\PhD{\isnextchar.{PhD}{Ph.D.}}
\def\PhD{\isnextchar.{PhD}{Ph.D.}}

The \PhD\ inside the sentence is different than \PhD.
gives:
The Ph.D. inside the sentence is different than PhD.

\bye

• this will work for "Ph.D.", but for "D.Phil.", unless \frenchspacing is used, the space after the the substitution in the middle of the sentence will be treated as a sentence-ending space. (and for \PhD, the space after it will be just an "ordinary" space at the end of a sentence.) – barbara beeton Dec 15 '14 at 13:29
• @barbarabeeton After we will know more about OP's intend then we can tune details. At this moment, the usage of \PhD\  or \PhD. (at the end of the sentence) works OK, because the \ ` space is independent of the spacefactor. But I doubt that OP needs this. Maybe I didn't understand something... – wipet Dec 15 '14 at 13:41