# XeLaTeX italic/quote kerning problem

I'm using xelatex. I would like, I think, to alter the kerning between certain italic/math characters, and apostrophes and quotation marks. Here's my problem:

\documentclass[a4paper,12pt]{minimal}
\usepackage{mathspec}
\begin{document}
$F$' $F$''
\end{document}


This, with xelatex, gives me:

That clearly seems wrong.

My question is whether it's possible to tell xelatex to alter the kerning here automatically, without inserting extra commands within the document body itself. I have tried to alter the code given here, but to no avail. The following doesn't work:

\documentclass[a4paper,12pt]{minimal}
\usepackage{mathspec}

\makeatletter
\XeTeXinterchartokenstate=1
\newXeTeXintercharclass\LetterClass
\newXeTeXintercharclass\QuoteClass
\@tempcnta=\A
\loop\unless\ifnum\@tempcnta>\Z
\XeTeXcharclass \@tempcnta \LetterClass
\repeat
\@tempcnta=\a
\loop\unless\ifnum\@tempcnta>\z
\XeTeXcharclass \@tempcnta \LetterClass
\repeat
\XeTeXcharclass \'=\QuoteClass
\XeTeXinterchartoks\LetterClass\QuoteClass={\kern 2pt} % deliberately big
\makeatother

\begin{document}
$F$' $F$'' $F'$ F'
\end{document}


I can see what's going wrong, but I'm not sure what I should do to fix it, or whether this sort of method is wrong-headed from the start.

The solution should let me do this automatically, to whole classes of italic/math letters.

• Welcome to TeX.SX! – Bobyandbob Nov 21 '17 at 15:15
• I'd say $"F$' – egreg Nov 21 '17 at 15:32
• @egreg Thanks. But I wanted to do this without inserting extra commands within the document body itself. (One reason being that I use the same body, sometimes, with both pdflatex and xelatex.) So adding an extra '' won't work for me. Perhaps I should also say that I want to apply the solution to a whole class of italic characters. – vacantnorthernerboy Nov 21 '17 at 15:55
• This is caused by combining math and text quotes. The kerning is correct if you use \textit{F}' for example. \XeTeXinterchartoks might be a solution. Could you post what you tried for that solution too? – Alan Munn Nov 21 '17 at 16:26
• @RobertCraven Not with mathspec. – egreg Nov 21 '17 at 16:36

Here are two solutions to your problem.

## Use proper italics

The first questions why you would ever want to have a text quote following a math element. Either the math element contains the quote (in which case it should be realized as a prime in math mode) or the math element isn't really math, and you're using $...$ to create text italics, which is incorrect. If the latter is the problem, then it suffices just to use \textit or \emph for your italics and the kerning will come out correctly:

\documentclass[a4paper,12pt]{minimal}
\usepackage{mathspec}
\begin{document}
$F$' $F$''

\textit{F}'
\textit{F}''
\end{document}


## Use \XeTeXinterchartoks

If you absolutely need to do exactly what you are doing, it is possible to do it, but comes with some caveats, and I don't really recommend it. We can use \XeTeXinterchartoks to insert space between the end of a group and the quotation marks. Since there's no way to distinguish the end of a math group from the end of any other group in this case, this solution might add extra space in places you don't want. Here's a full example:

% !TEX TS-program = XeLaTeX
\documentclass{article}
\makeatletter
\chardef\CharBound=\e@alloc@intercharclass@top
\makeatother
\XeTeXinterchartokenstate=1 % enable character classes
\newXeTeXintercharclass\mathquote % create a new class
\XeTeXcharclass ’ \mathquote
\XeTeXcharclass ' \mathquote
\XeTeXcharclass ” \mathquote
% between any character of class 0 and \mathquote
\XeTeXinterchartoks \CharBound \mathquote = {\hspace{3pt}} % purposefully large for testing
\begin{document}
$F$’
$A$’
$F$”
F’   % this is ok because no end of group.
{F}’ % fails here because you can't distinguish end-of-math from end-of-group
$F'$ % this is still ok.

\XeTeXinterchartoks \CharBound \mathquote = {\hspace{.2pt}} % Now make it more reasonable
$F$’
$A$’
$F$”
F’   % this is ok because no end of group.

\end{document}


• Thank you. Your code's very helpful. As to why I want it done: I am mentioning mathematical expressions, so the expression itself (as it were) is written within , but the quotation marks are regular text quotes. So neither a math 'prime', nor regular italics, would be appropriate. – vacantnorthernerboy Nov 21 '17 at 21:05
• @RobertCraven I suspected that might be the case, but I've seen too many people misuse $...$ to get italics... :) – Alan Munn Nov 21 '17 at 21:06
• I've also added the following, to deal with the kerning of $F'$ (which in the Garamond I'm using was wrong). If there's a better way, then I don't know of it: \def\active@math@prime{^\bgroup\primfirst@s} {\catcode`\'=\active \global\let'\active@math@prime} \def\primfirst@s{\hspace{1pt}\prime\futurelet\@let@token\pr@m@s} – vacantnorthernerboy Nov 22 '17 at 9:54