8

I've defined a macro for an initialism (abbreviation) with periods, making sure that an end-of-sentence period is not printed twice: \gp prints "G.P." and \gp. prints "G.P." Here's an M.W.E.:

\documentclass{article}
\makeatletter\def\gp{G.P\@ifnextchar.{}{.}}\makeatother
\begin{document}
\gp{} (bad) \par \gp. (bad) \par G.P. (O.K.)
\end{document}

enter image description here

However, this breaks the kerning between the final letter and the final period — see the attached screenshot. This is a pretty minor issue of course, but I find it kind of interesting. The lack of kerning is more pronounced with some other fonts, including Palatino (\usepackage{mathpazo}). Am I abusing \@ifnextchar or is there some other issue? (The same happens with ligatures, although I can't think of a useful application for the following: \makeatletter\def\doublef{f\@ifnextchar f{}{f}}\makeatother and ff --- \doublef f --- \doublef i --- ffi.) enter image description here

10
  • 4
    Any assignment between two characters breaks kerning. – egreg Dec 15 '15 at 18:19
  • 2
    @egreg Not true in LuaTeX! – Joseph Wright Dec 15 '15 at 18:29
  • 5
    \newcommand*\gp{\@ifnextchar.{G.P}{G.P.}} – cgnieder Dec 15 '15 at 18:32
  • 5
    \def\gp{G.P.\@ifnextchar.{\@gobble}{}} – user2478 Dec 15 '15 at 18:52
  • 1
    @tanh you can load \usepackage{ltxcmds} and then use \newcommand*{\fixdot}[1]{#1\ltx@ifnextchar@nospace.{\@gobble}{}} – cgnieder Dec 16 '15 at 9:50
4
\def\gp{G.P.\@ifnextchar.{\@gobble}{}}

\@gobble is defined in the latex kernel as \def\@gobble#1{}

3
  • Thank you! Reading the definition of \@gobble also makes it clear wow it works. This can be generalized to take an argument using \ltx@ifnextchar@nospace from the ltxcmds package, as suggested by @clemens in their comment to my question. – tanh Dec 16 '15 at 15:51
  • 1
    not relevant here but \@gobble is declared as \long\def\@gobble#1{}, thus with LaTeX syntax as \newcommand{\@gobble}[1]{}. – user4686 Dec 16 '15 at 16:56
  • yes, because #1 can include paragraphs – user2478 Dec 17 '15 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.