我在用TeX.SX 上的这个答案在数学模式下获取多字母变量,以便$abc$
将其排版为$\mathit{abc}$
,而不是排版为a
和b
的乘积c
。
\mathit
不幸的是,即使使用了另一个数学字体命令(即$\mathtt{abc}$
排版为) ,该代码也会强制执行$\mathtt{\mathit{abc}}$
。因此,我的目标是检测当前字体是什么,并且\mathit
仅在使用默认数学字体时才应用于多字母变量。
我尝试使用\f@family
如下方法这个答案,但它似乎在数学模式下不起作用:
\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}
上述文件在日志中显示:
encoding: OT1, family: cmr, series: m, shape: n, size: 10
encoding: OT1, family: cmr, series: m, shape: n, size: 10
在我看来,\f@family
其他\f@xxx
命令显示文本模式字体,而不是显示数学模式字体。
是否有相当于\f@family
提供有关数学模式字体的信息?
答案1
不是一个命令,而是一个数字:\the\fam
正如我在下面的回答中实际设置为 4 以使默认设置为\mathit
。但是数字分配是动态的:它取决于字体在文档中的使用顺序,因为只有 16 个插槽(在经典 TeX 中),所以例如,如果您声明\mathbb
但从未使用过该字体,则该字体永远不会分配系列编号,它们只是在第一次使用时设置。在下面的代码中,我\mathit
首先使用获取数字,然后为其余表达式设置该数字。
您正在查看此处未使用的文本字体设置,您只想将默认系列设置为\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}
答案2
谢谢大卫·卡莱尔的回答至和评论,我使用以下方法为原始问题构建了一个解决方案\the\fam
检测当前的数学字体,基于Heiko Oberdiek 的回答:
\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}%
}%
\g@addto@macro\m@check@letter{%
\ifx\@let@token X\else
}%
}%
\g@addto@macro\m@check@fi{\fi}%
\advance\count@\@ne
}
% 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{%
\let\m@next\m@add
\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
}%
}
\newcommand*{\m@add}[1]{%
\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}