# \f@family in math mode

I am using this answer on TeX.SX to get multi-letter variables in math mode, so that $abc$ typesets as $\mathit{abc}$, instead of being typeset as if it was the product of a, b and c.

Unfortunately, that code forces the \mathit even if another math font command was used, i.e. $\mathtt{abc}$ typesets as $\mathtt{\mathit{abc}}$. My goal is therefore to detect what is the current font, and apply \mathit to multi-letter variables only if the default math font is used.

I tried using \f@family as shown in this answer, but it does not seem to work in math mode:

\documentclass{article}
\begin{document}
\makeatletter
\newcommand{\showfont}{
\message{encoding: \f@encoding,
family: \f@family,
series: \f@series,
shape: \f@shape,
size: \f@size
}
}
\makeatother

$foo \showfont bar$
$\mathtt{foo \showfont bar}$

\end{document}


The file above shows in the log:

encoding: OT1, family: cmr, series: m, shape: n, size: 10
encoding: OT1, family: cmr, series: m, shape: n, size: 10


It seems to me that \f@family and the other \f@xxx commands show the text mode font, instead of showing the math mode font.

Is there an equivalent to \f@family which gives the information concerning the math mode font?

• Commands like \mathit and \mathtt don't have cumulative effect and the font parameters such as \f@family have no role in math mode. – egreg Oct 18 '16 at 12:37
• @egreg I'm not trying to get a cummulative effect (italics teletype, if that's what you meant). I just want to detect the current math font, to disable the implicit \mathit when the current font is not the default one. As you said, \f@family is not supposed to work in math mode, but is there a similar command which would give me information about the current math mode font? – Georges Dupéron Oct 19 '16 at 18:28
• @GeorgesDupéron not a command a number \the\fam as I set to 4 actually in my answer to make things default to mathit, however the number allocation is dynamic it depends on the order that fonts are used in the document, as there are only 16 slots so if for example you declare \mathbb but never use it that font is never allocated a familiy number at all, they are just set up the first time they are used, which is why in my answer I use \mathit first to get the number and then set that for the rest of the expression. – David Carlisle Oct 19 '16 at 19:16
• @DavidCarlisle Oh I see, sorry I hadn't noticed the use of \the\fam. I should be able to build what I want with this and the other TeX.SX answer. Thanks! – Georges Dupéron Oct 19 '16 at 19:53
• if you get something working feel free to self answer and accept (gets this off the list of unanswered questions:-) – David Carlisle Oct 19 '16 at 19:55

Not a command, a number: \the\fam as I set to 4 actually in my answer below to make things default to \mathit. However the number allocation is dynamic: it depends on the order that fonts are used in the document, as there are only 16 slots (in classic TeX) so if for example you declare \mathbb but never use it that font is never allocated a family number at all, they are just set up the first time they are used. In the code below I use \mathit first to get the number and then set that for the rest of the expression.

You are looking at the text font settings which are not used here, you just want to set the default family to that of \mathit

\documentclass{article}
\begin{document}

$ffoo bar$
$ffoo \mathtt{ffoo bar} bar$

\everymath{\mathit{\xdef\tmp{\fam\the\fam}\aftergroup\tmp}}\sbox0{}

$ffoo bar$
$ffoo \mathtt{ffoo bar} bar$

\end{document}

• No, single letter should not be in \mathit, only runs of them. – egreg Oct 18 '16 at 13:00
• @egreg hmph, then use markup to distinguish the cases, that's the point of a markup language;-) – David Carlisle Oct 18 '16 at 13:01
• Exactly my answer to the linked question. – egreg Oct 18 '16 at 13:02
• @DavidCarlisle thanks for the answer. Like egreg mentioned, single-letter variables should still be typeset as multiplication, i.e. $a f f b$ should not be altered. I'd like to avoid markup, otherwise I need to write \mathit constantly, and regularly forget it. The answer I linked to solves that problem, and I'm just trying to disable that behaviour within an explicit \mathtt, \mathrm etc. So far, the only solution I see is to override the \mathit, \mathtt etc. commands with versions which set a flag while expanding their body. – Georges Dupéron Oct 19 '16 at 18:37

Thanks David Carlisle's answer to and comment, I built a solution for my original problem using \the\fam to detect the current math font, based on Heiko Oberdiek's answer:

\documentclass[fleqn]{article}

\makeatletter
\usepackage{etexcmds}
\newtoks\m@toks@math
\newtoks\m@toks@text

\edef\m@tmp@restore{%
\lccode\number\X=\the\lccode\X\relax
\lccode\number\~=\the\lccode\~\relax
}

\newcommand*{\m@activate}{}
\newcommand*{\m@check@letter}{}
\newcommand*{\m@check@fi}{}

\newif\ifm@single

\let\m@start\relax
\def\m@loop{%
\lccode\X=\count@
\lccode\~=\count@
\lowercase{%
\expandafter\mathchardef\csname m@code@X\endcsname=\mathcode\count@
\edef\m@activate{%
\etex@unexpanded\expandafter{\m@activate}%
\mathcode\the\count@="8000\relax
\def\noexpand~{\m@start\csname m@code@X\endcsname X}%
}%
\ifx\@let@token X\else
}%
}%
}
% A-Z
\count@=\A\relax
\@whilenum\count@<\numexpr\Z+1\relax\do{\m@loop}
% a-z
\count@=\a\relax
\@whilenum\count@<\numexpr\z+1\relax\do{\m@loop}

\newcommand*{\m@start}[2]{%
\bgroup
\m@toks@math{#1}%
\m@toks@text{#2}%
\m@singletrue
\futurelet\@let@token\m@check
}
\edef\m@check{%
\etex@unexpanded{%
\ifx\@let@token\space
\let\m@next\m@finish
\else
\ifx\@let@token\egroup
\let\m@next\m@finish
\else
}%
\etex@unexpanded\expandafter{%
\m@check@letter
}%
\etex@unexpanded{%
\let\m@next\m@finish
}%
\etex@unexpanded\expandafter{%
\m@check@fi
}%
\etex@unexpanded{%
\fi
\fi
\m@next
}%
}

\m@singlefalse
\m@toks@math\expandafter{%
\the\expandafter\m@toks@math
\csname m@code@#1\endcsname
}%
\m@toks@text\expandafter{%
\the\m@toks@text
#1%
}%
\futurelet\@let@token\m@check
}

\newcommand*{\m@finish}{%
\ifm@single
\expandafter\mprintsingle\expandafter{%
\the\expandafter\m@toks@math\expandafter
}\expandafter{%
\the\expandafter\m@toks@text\expandafter
}%
\else
\expandafter\mprintmulti\expandafter{%
\the\expandafter\m@toks@math\expandafter
}\expandafter{%
\the\expandafter\m@toks@text\expandafter
}%
\fi
\egroup
}

% \let\mprintsingle\@firstoftwo
% \let\mprintmulti\@firstoftwo
\def\mprintsingle#1#2{{\color{blue}#1}}%
\def\mprintmulti#1#2{%
% Test whether we are using the default math font or not:
\ifnum\the\fam=-1\relax%
\mathit{\color{red}#1}%
\else%
{\color{green!50!black}#1}%
\fi%
}

\everymath{\m@activate}
\everydisplay{\m@activate}

\m@tmp@restore
\makeatother

\usepackage{xcolor}
\usepackage{amstext}

\begin{document}
\textcolor{blue}{Single letters are set in blue},
\textcolor{red}{multiple letters in red},
\textcolor{green!50!black}{multiple letters inside \texttt{\textbackslash mathtt}
or \texttt{\textbackslash mathit} etc. in green}.

$F_{force (implicit\ mathit)} = m_\mathit{mass (explicit\ mathit)} * a_\mathtt{acceleration (explicit\ mathtt)}$

Multiplication of single-letter variables
$a f f b$

Multiplication of multi-letter variables
$aaa fff fff bbb$

\end{document}