41

Is it possible to tell (La)TeX to adjust the kerning of a specific character combination? For example, suppose I want !! to be typeset as !\kern 1.2pt!, just like ff is automatically turned into a ligature, or -- is converted to an en-dash. I know I could create a command for this combination—I am asking for a way to just type !! and have it use my desired kerning.

6
  • 4
    ff or the en-dash (or em-dash, fi, fl, ffi) aren't multiple characters whose kerning/s were adjusted. They have their own separate places in the font tables.
    – Kit
    Commented Feb 7, 2011 at 5:16
  • @Kit So LaTeX can map multiple characters to special slots in the font table, but not to macros? That’s … weird. Commented Feb 7, 2011 at 8:08
  • 1
    @Konrad: it's not weird when you consider the processing stages; macro expansion and execution is performed until a stream of text is left (there's probably a more formal definition here I'm forgetting), only after which ligatures are applied and then hyphenation/justification occurs. Separate the idea between "fi" in the input as characters in a text file and "fi" in the output as a glyph from a font. Commented Feb 7, 2011 at 8:39
  • @Will Thanks for the explanation. I really need to read TeX book one of these days. Commented Feb 7, 2011 at 8:51
  • @Konrad: Keep in mind that ligatures are only partly a TeX thing; they're mainly a font thing. For the "old style" TeX fonts, they are defined in the tfm (TeX font metric) files. There you find, e.g., (LABEL C f) (LIG C i O 14), meaning that fi is mapped to the ligature in slot 14 (octal number). Commented Feb 7, 2011 at 13:54

3 Answers 3

32

Adjusting font kerning

If you need to adjust the kerning within a font, the short answers are:

  1. No, not from within TeX.

  2. Yes, if you're willing to create a virtual font.

  3. Yes, if you're able to edit the font with fontforge (OpenType fonts only, for use in XeTeX or LuaTeX).

  4. Yes, if you're using LuaTeX and OpenType fonts; see §11 of the fontspec manual: ‘OpenType font feature files’.

I've only experience in options 1 and 4.

XeTeX's interchartoks

XeTeX offers a feature known as ‘interchartoks’ that allows tokens to be inserted automatically between characters of different ‘class’. This feature was added to facilitate, say, automatic font and language switching between two different scripts (e.g., Japanese to Arabic).

This feature can be used to add kerning between characters, as shown in Philipp's answer. (As well as between !!, you might also choose to adjust spacing around : for French typography, say.) As an example, if this feature is used then when XeTeX comes across !! in the input, it will interpret it as !\exclamkern! instead. Any kerning that happens there will independent of the current font, which is generally not going to be appropriate for changing the kerning between letters. But for very specific use cases this technique is suitable for minor adjustments.

5
  • I think this should also be possible in XeTeX using char classes.
    – Philipp
    Commented Feb 7, 2011 at 12:27
  • @Philipp Possible, but not practical. Interchartoks are inserted regardless of the font, so you'd need to check for the current font for each kerning adjustment—it'd be a bit of a hack. And it'd then complicate edge cases with more practical interchartoks uses. But for very specific applications, yes, it'd work. Commented Feb 7, 2011 at 12:38
  • The OP didn't ask for font-specific adjustments, so I think interchar tokens would indeed be an option here.
    – Philipp
    Commented Feb 7, 2011 at 12:43
  • @Philipp sorry, I misread the question regarding !!, so yeah, this would indeed work. (but for text kerning I'm still less than enthusiastic.) Commented Feb 7, 2011 at 13:47
  • Could you modify your answer to include the interchartoks solution? Commented Feb 12, 2011 at 15:17
25

With LuaTeX, you can patch fonts on the fly in arbitrary ways:

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{luacode}

\begin{luacode*}
local function add_exclam_kern(fontdata)
  if fontdata then
    local chars = fontdata.characters
    if chars then
      local ch = chars[33]
      if ch then
        if not ch.kerns then
          ch.kerns = { }
        end
        ch.kerns[33] = -100000
      end
    end
  end
end
luatexbase.add_to_callback("luaotfload.patch_font",
  add_exclam_kern, "add_exclam_kern")
\end{luacode*}

\setmainfont{Arial}

\begin{document}
!!
\end{document}

With XeTeX, you can use inter-character tokens:

\documentclass{minimal}

\usepackage{fontspec}

\setmainfont{Arial}

\XeTeXinterchartokenstate=1
\newXeTeXintercharclass\ExclamClass
\XeTeXcharclass 33=\ExclamClass
\XeTeXinterchartoks\ExclamClass\ExclamClass={\kern-1.5pt }

\begin{document}
!!
\end{document}
9
  • Could one also just modify the kerning table in LuaTeX in a way similar as in (LABEL C P) (KRN C A R -0.083334)? Commented Feb 7, 2011 at 14:01
  • @Hendrik: What does that statement do?
    – Philipp
    Commented Feb 7, 2011 at 14:53
  • It's from cmr10.pl, and it says that between the Character P and the Character A a kern of -0.083334 times the design size (R=10pt) should be included. Commented Feb 7, 2011 at 14:58
  • 1
    @Philipp: Erm, yes, but the Lua code is quite long, and it's really code. In a pl file it's a lot simpler in my opinion; it's a kerning table indeed: You can, e.g., also do (LABEL C P) (KRN C A R -0.083334) (KRN C j R -0.05) to additionally get kerning in Pj. Commented Feb 7, 2011 at 15:33
  • 1
    @Hendrik: A table constructor is something like chars = { [33] = { kerns = { [33] = -100000 } } }
    – Philipp
    Commented Mar 3, 2011 at 13:10
5

Disclaimer: This is not thoroughly tested in a real document, applying none-standard category codes to the document might break other things, so use this with caution.

A TeX only way without changing the fonts would be to make the character active and define it to check the next character (which is sort of a hacky way). For the example of replacing !! with !\kern 1.2pt! one could use:

\documentclass[preview,border=5mm]{standalone}

\begin{document}
!\kern 1.2pt!\\
\makeatletter
\let\exclamationmarkbak!
\catcode`\!=13
\def!{\@ifnextchar!{\exclamationmarkbak\kern 1.2pt}{\exclamationmarkbak}}
\makeatother
!!\\
!a
\end{document}

enter image description here

4
  • Why this answer didn't gain any upvote? +1
    – Eric
    Commented Jul 8, 2018 at 7:39
  • @Eric because this might lead to instable behaviour and isn't recommendable. This fact is reflected by the votes of the community (though it does work in this very limited minimal working example).
    – Skillmon
    Commented Jul 8, 2018 at 15:13
  • This solves the problem. And it isn't dependent on using a specific type of font or TeX engine (presumably the only one that works with pdfTeX). And it's by far the simplest of anything here. So it's underrated in my view (though the low score is probably related to it being posted 6 years after the other answers).
    – lukeuser
    Commented May 7, 2021 at 8:43
  • @lukeuser because this might result in unstable behaviour, isn't well tested, and might break some macros using ! as an input variant (similar to many macros using a star as a variant).
    – Skillmon
    Commented May 7, 2021 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .