3

I am trying to correct a spacing issue in mathspec. I wish to add an extra space after the letter f in math mode, but only if it is not followed by a subscript. I don't want to do this manually every time, so I have tried this code:

\makeatletter
\mathcode`f="8000
\DeclareMathSymbol{\f}{\mathalpha}{letters}{`f}
\newcommand{\mathf}{\@ifnextchar_{\f}{\f \,}}
\begingroup
    \lccode`\~=`\f
    \lowercase{\gdef~{\ifnum\the\mathgroup=\m@ne \mathf \else \f \fi}}
\endgroup
\makeatother

But it doesn't work. It adds the space regardless! How could I fix this?

1
  • the token after \mathf is always \else so never _` (you are missing some \expandafter Nov 2, 2014 at 18:51

1 Answer 1

3

When \ifnum is evaluated, \else is not removed, so \mathf sees it and it never scans further for a next _. You should have \expandafter\mathf instead, so the \else is removed up to the matching \fi before \mathf is expanded.

However, I suggest you some better code:

\documentclass{article}
\usepackage{mathspec}
\setmainfont{Hoefler Text}
\setmathfont(Digits,Latin)[Numbers=Lining]{Hoefler Text}
\setmathrm[Numbers=Lining]{Hoefler Text}

\makeatletter
\AtBeginDocument{%
  \edef\SAVEfCODE{\the\Umathcodenum`f } % just for the test
  \Umathcharnumdef\math@f\Umathcodenum`f 
  \mathcode`f=\string"8000 }
\begingroup\lccode`~=`f
\lowercase{\endgroup
  \def~{%
    \math@f
    \ifnum\mathgroup=\m@ne
      \expandafter\@firstofone
    \else
      \expandafter\@gobble
    \fi
    {\@ifnextchar_{\relax}{\,}}%
  }
}
\makeatother

\begin{document}

$f(x)f_1(x)\mathbf{f}(x)\mathbf{f}_1(x)\mathbf{f_1}(x)$

%% Just to show the difference
\Umathcodenum`f=\SAVEfCODE\relax
$f(x)f_1(x)\mathbf{f}(x)\mathbf{f}_1(x)\mathbf{f_1}(x)$

\end{document}

The parts with \SAVEfcode are just for showing the difference.

enter image description here

Note: in \ifnum\the\mathgroup, \the is redundant, because \mathgroup is a number. I used \Umath... because its safer in the context of mathspec.

2
  • Then how to add space to the both side of f?
    – M. Logic
    Aug 27, 2020 at 4:09
  • @Mogic Add \, or \mkern2mu (experiment with the value) before \math@f
    – egreg
    Aug 27, 2020 at 9:10

You must log in to answer this question.

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