How can I change the default math-mode 'f' in the cochineal package?

I think the font provided by the cochineal package is excellent, and I want to write documents using it, so I use the following in my preamble:

\usepackage[osf, p]{cochineal}
\usepackage[cochineal]{newtxmath}


However I dislike one particular change that was made in the math font, namely the way that italic f is changed. See the difference in the line \textit{f} versus $f$:

As far as I can tell this is one of three changes to the lowercase italic alphabet in math mode: the letters v and w are also changed to have better distinction from the greek letter nu.

My issue is that I strongly dislike this new letter f. I understand this may have been a stylistic choice by the designer but I much prefer the reclining, two-tailed f to this one.

I can force the math f to display as the regular italic one by writing \mathit{f} rather than just f, but this is obviously not very convenient.

How can I change the way that the lowercase letter f is displayed in math mode?

Here is a minimal document to reproduce this: (compiled with pdfTeX)

\documentclass{article}

\usepackage[osf, p]{cochineal}
\usepackage[cochineal]{newtxmath}

\begin{document}
Comparison: \textit{f} versus $f$.

Default: $f(x + y) = f(2x) + f(2y) - 1$.

Forced italic: $\mathit{f}(x + y) = \mathit{f}(2x) + \mathit{f}(2y) - 1$.
\end{document}


Declare a new math symbol font.

\documentclass{article}

\usepackage[osf, p]{cochineal}
\usepackage[cochineal]{newtxmath}

\DeclareSymbolFont{cochinealit}{\encodingdefault}{\familydefault}{m}{it}
\DeclareMathSymbol{f}{\mathalpha}{cochinealit}{f}
\DeclareSymbolFontAlphabet{\mathit}{cochinealit}

\begin{document}

Comparison: \textit{f} versus $f$. Also $f^2$.

Default: $f(x + y) = f(2x) + f(2y) - 1$.

Forced italic: $\mathit{f}(x + y) = \mathit{f}(2x) + \mathit{f}(2y) - 1$.

Beware: $ff+f\/f$

Math roman: $\mathrm{f}$

\end{document}


There's a small catch, shown in the last line: you need something like f\/f if two consecutive f's appear in a formula.

• This is very explanatory. I wonder if there is any way to automatically supress ligatures here?
– AJF
Sep 4, 2019 at 23:19
• @AJFarmar Not without much labor: the font needs to be duplicated to be loaded at a slightly (but unnoticeable) different size and its internal parameter be changed. Sep 4, 2019 at 23:22

\documentclass{article}
\showoutput
\usepackage[osf, p]{cochineal}
\usepackage[cochineal]{newtxmath}
\sbox0{$\mathit{abc}$}

\mathcodef=\numexpr\mathcodef+"700\relax

\begin{document}

Comparison: \textit{f} versus $f$.

Default: $f(x + y) = f(2x) + f(2y) - 1$.

Forced italic: $\mathit{f}(x + y) = \mathit{f}(2x) + \mathit{f}(2y) - 1$.

Forced roman: $\mathrm{f}(x + y) = \mathrm{f}(2x) + \mathrm{f}(2y) - 1$.
\end{document}

• Did you forget something? Sep 4, 2019 at 22:07
• @egreg there are lots of things I didn't mention, if that is what you mean. Sep 4, 2019 at 22:17

One hacky way is to change the letter f to an active character, but only in math mode. This is described in this post on the TeX FAQ.

We do this by inserting this:

% Warning: there are serious issues with this; see below.
\begingroup
\lccode~=f
\lowercase{\endgroup
\def~{\text{\textit{f}}}%
}%
\mathcodef="8000


Put simply, this replace all occurences of the solitary letter f with \text{\textit{f}} in math mode.

So, here's a full example:

\documentclass{article}

\usepackage[osf, p]{cochineal}
\usepackage[cochineal]{newtxmath}

\begingroup
\lccode~=f
\lowercase{\endgroup
\def~{\text{\textit{f}}}%
}%
\mathcodef="8000

\begin{document}
Comparison: \textit{f} versus $f$.

Default: $f(x + y) = f(2x) + f(2y) - 1$.

Forced italic: $\mathit{f}(x + y) = \mathit{f}(2x) + \mathit{f}(2y) - 1$.
\end{document}


This produces the desired result:

There are serious issues with this. In particular, \mathrm{f} displays strictly in italic, so for instance \liminf has a jarring italic f at the end.

For this reason, it may be preferable simply to define \newcommand\f{\mathit{f}} and simply write \f(x) instead of f(x)`.