使用 mathastext-package 识别 \mathrm 的智能希腊字母

使用 mathastext-package 识别 \mathrm 的智能希腊字母

这是另一个后续问题这个@wipet 的回答很棒关于如何创建“智能希腊字母”,其在数学模式下通常是斜体,但可以识别\mathrm变为直立的命令。

我成功地使用了这个设置与各种数学字体,但由于我最近应该使用打开 Sans作为演示文稿中的文本字体,我不太满意字体包裹了。所以我想给数学文本试一试。我已经成功声明了希腊数学字母表,但\usepackage{mathastext}破坏了@wipet 的\setsmartgreek

我不太明白为什么,因为 mathastext 默认应该保留希腊字母不变。我还尝试了其他各种软件包选项。有没有办法让智能希腊字母再次发挥作用?

原始 MWE

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{microtype}
\usepackage{amsmath}

% Load fonts, sansmathfonts for symbols etc.
\usepackage{sansmathfonts}
\usepackage[default]{opensans}

% Declare greek letters for math
\DeclareFontEncoding{LGR}{}{}
\DeclareSymbolFont{osgreek}{LGR}{fos}{m}{n}
\SetSymbolFont{osgreek}{bold}{LGR}{fos}{bx}{n}
\DeclareSymbolFont{osgreekit}{LGR}{fos}{m}{it}
\SetSymbolFont{osgreekit}{bold}{LGR}{fos}{bx}{it}

\DeclareMathSymbol{\upmu}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\muup}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\mu}{\mathord}{osgreekit}{`m}

% set smart greek letters
\def\setsmartgreek{%
    \expandafter\ifx\csname muup\endcsname\relax 
    \else \def\stgreekC##1##2;{##2up}\fi
    \expandafter\ifx\csname upmu\endcsname\relax 
    \else \def\stgreekC##1##2;{up##2}\fi
    \ifx\stgreekC\undefined \errmessage{upright greek letters are not available}%
    \else \expandafter\setsmartgreekA \fi
}
\def\setsmartgreekA#1{\ifx#1\relax \else
    \expandafter \let \csname it:\string#1\endcsname = #1%
    \edef#1{\noexpand\ifnum\fam=0
        \expandafter\noexpand\csname\expandafter\stgreekC\string#1;\endcsname
        \noexpand\else \expandafter\noexpand\csname it:\string#1\endcsname
        \noexpand\fi}%
    \expandafter\setsmartgreekA \fi
}

\setsmartgreek\mu\relax

% substitute letters and numbers with text font in math mode
\usepackage{mathastext} %<--- comment out and smart greek letters work

% siunitx should keep working
\usepackage{siunitx}
\sisetup{math-micro=\mathrm{\upmu}}
\AtBeginDocument{\sisetup{detect-all}}

\begin{document}

Regular numbers and unit: \SI{42}{\micro\meter}

\textit{Regular numbers and unit: \SI{42}{\micro\meter}}

\textbf{Bold numbers and unit: \SI{42}{\micro\meter}}

\textbf{\textit{Bold italic numbers and unit: \SI{42}{\micro\meter}}}

\[
\int \sum X_\mathrm{\textcolor{red}{\mu\upmu}}\mu\upmu\mathrm{\textcolor{red}{\mu\upmu}}A + 2 = \SI{42}{\micro\meter\ampere\gram}
\]

All \textcolor{red}{red} should be upright. 

\end{document}

在此处输入图片描述

扩展的 MWE

jfbu 的答案建议使用选项defaultrmdefaultalphabets建议数学文本,它适用于原始 MWE。但对于普通字母,字体会返回到sansmathfonts内部\mathrm。没有defaultrm智能希腊字母不起作用,但字体是正确的。有什么解决办法吗?

关于这个主题,mathastext 的手册读起来相当令人沮丧,虽然有一个扩展的讨论,但不幸的是它并没有真正切中要点。

\documentclass{beamer}

\usepackage[utf8]{luainputenc}
\usepackage[LGR, T1]{fontenc}
\usepackage{amsmath}
\usepackage{sansmathfonts}
\usepackage[default]{opensans}
\usepackage{siunitx}

\DeclareFontFamily{T1}{fosrm}{}
\DeclareFontShape{T1}{fosrm}{m}{n}{<-> ssub * fos/l/n }{}
\DeclareFontShape{T1}{fosrm}{b}{n}{<-> ssub * fos/sb/n }{}
\DeclareFontShape{T1}{fosrm}{m}{it}{<-> ssub * fos/l/it }{}
\DeclareFontShape{T1}{fosrm}{b}{it}{<-> ssub * fos/sb/it}{}

\DeclareFontFamily{T1}{fossf}{}
\DeclareFontShape{T1}{fossf}{m}{n}{<-> ssub * fos/m/n }{}
\DeclareFontShape{T1}{fossf}{b}{n}{<-> ssub * fos/b/n }{}
\DeclareFontShape{T1}{fossf}{m}{it}{<-> ssub * fos/m/it }{}
\DeclareFontShape{T1}{fossf}{b}{it}{<-> ssub * fos/b/it}{}

\DeclareFontFamily{LGR}{fosgreek}{}
\DeclareFontShape{LGR}{fosgreek}{m}{n}{<-> ssub * fos/l/n }{}
\DeclareFontShape{LGR}{fosgreek}{b}{n}{<-> ssub * fos/sb/n }{}
\DeclareFontShape{LGR}{fosgreek}{m}{it}{<-> ssub * fos/l/it }{}
\DeclareFontShape{LGR}{fosgreek}{b}{it}{<-> ssub * fos/sb/it}{}

\renewcommand{\sfdefault}{fossf}
\renewcommand{\rmdefault}{fosrm}
\renewcommand{\familydefault}{\rmdefault}
\renewcommand{\mddefault}{m}
\renewcommand{\bfdefault}{b}
\renewcommand{\seriesdefault}{\mddefault}

\DeclareFontEncoding{LGR}{}{}
\DeclareSymbolFont{osgreek}{LGR}{fos}{l}{n}
\SetSymbolFont{osgreek}{bold}{LGR}{fos}{sb}{n}
\DeclareMathSymbol{\upmu}{\mathord}{osgreek}{`m}

\DeclareSymbolFont{osgreekit}{LGR}{fos}{l}{it}
\SetSymbolFont{osgreekit}{bold}{LGR}{fos}{sb}{it}
\DeclareMathSymbol{\mu}{\mathord}{osgreekit}{`m}

\DeclareMathSymbol{\upmu}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\muup}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\mu}{\mathord}{osgreekit}{`m}

% set smart greek letters
\def\setsmartgreek{%
    \expandafter\ifx\csname muup\endcsname\relax 
    \else \def\stgreekC##1##2;{##2up}\fi
    \expandafter\ifx\csname upmu\endcsname\relax 
    \else \def\stgreekC##1##2;{up##2}\fi
    \ifx\stgreekC\undefined \errmessage{upright greek letters are not available}%
    \else \expandafter\setsmartgreekA \fi
}
\def\setsmartgreekA#1{\ifx#1\relax \else
    \expandafter \let \csname it:\string#1\endcsname = #1%
    \edef#1{\noexpand\ifnum\fam=0
        \expandafter\noexpand\csname\expandafter\stgreekC\string#1;\endcsname
        \noexpand\else \expandafter\noexpand\csname it:\string#1\endcsname
        \noexpand\fi}%
    \expandafter\setsmartgreekA \fi
}

\setsmartgreek\mu\relax

\makeatletter
\DeclareRobustCommand{\textmu}{{\usefont{LGR}{fosgreek}{\f@series}{\f@shape}\symbol{\string"6D}}}
\DeclareRobustCommand{\textohm}{{\usefont{LGR}{fosgreek}{\f@series}{\f@shape}\symbol{\string"5F}}}
\makeatother
\sisetup{math-rm=\textrm, math-sf=\textrm,
         math-micro=\upmu,text-micro=\textmu,
         math-ohm=\upOmega,text-ohm=\textohm}

\usepackage[italic,defaultrm]{mathastext}

\begin{document}

\sisetup{detect-all}

Regular numbers and unit: \SI{42}{\micro\meter}\par
\textit{Regular italic numbers and unit: \SI{42}{\micro\meter}}\par
\textbf{Bold numbers and unit: \SI{42}{\micro\meter}}\par
\textbf{\textit{Bold italic numbers and unit: \SI{42}{\micro\meter}}}\par

\[
\int \sum \sin X_\mathrm{\textcolor{purple}{xA}\textcolor{red}{\mu\upmu}}xA\mu\upmu\mathrm{\textcolor{purple}{xA}\textcolor{red}{\mu\upmu}} + 2 = \SI{42}{\micro\meter\ampere\gram}
\]

All \textcolor{red}{red} should be upright. 

All \textcolor{purple}{purple} should be in the same font as the rest. 


\end{document}

在此处输入图片描述

答案1

在我的初始回答中,我甚至没有检查@wipet\setsmartgreek代码。我最初的回答建议使用defaultrm选项,mathastext因为这可以防止包修改与之一起\mathrm工作的字体。但在评论中,有人报告说这取消了太多的mathastext影响。所以我检查了\setsmartgreek的代码,也许只需要使用这个修改后的版本:

% substitute letters and numbers with text font in math mode
\usepackage{mathastext}

% set smart greek letters (adapted to mathastext presence; see \ifnum test)
\def\setsmartgreek{%
    \expandafter\ifx\csname muup\endcsname\relax 
    \else \def\stgreekC##1##2;{##2up}\fi
    \expandafter\ifx\csname upmu\endcsname\relax 
    \else \def\stgreekC##1##2;{up##2}\fi
    \ifx\stgreekC\undefined \errmessage{upright greek letters are not available}%
    \else \expandafter\setsmartgreekA \fi
}
\def\setsmartgreekA#1{\ifx#1\relax \else
    \expandafter \let \csname it:\string#1\endcsname = #1%
    \edef#1{\noexpand\ifnum\fam=\symmtoperatorfont
        \expandafter\noexpand\csname\expandafter\stgreekC\string#1;\endcsname
        \noexpand\else \expandafter\noexpand\csname it:\string#1\endcsname
        \noexpand\fi}%
    \expandafter\setsmartgreekA \fi
}

\setsmartgreek\mu\relax

在我使用 OP 的 MWE 进行的测试中,它起作用了。

OP 补充:

上面的答案似乎只适用于-class 被使用。在其他情况下(例如 KOMA-script、文章),它会导致错误,而旧的宏定义不会出现这种错误。对已加载的类进行简单检查可以解决此问题:

\@ifclassloaded{beamer}{
    \def\familyidentifier{\symmtoperatorfont}
    }{
    \def\familyidentifier{0}
}

在宏中,相关行必须更改为:

\edef#1{\noexpand\ifnum\fam=\familyidentifier
        ...
        }%


以前的答案:

将选项传递defaultrmmathastext,或者更好的是包罗万象的defaultalphabets。(查看详细信息文档

\documentclass{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{microtype}
\usepackage{amsmath}

% Load fonts, sansmathfonts for symbols etc.
\usepackage{sansmathfonts}
\usepackage[default]{opensans}

% Declare greek letters for math
\DeclareFontEncoding{LGR}{}{}
\DeclareSymbolFont{osgreek}{LGR}{fos}{m}{n}
\SetSymbolFont{osgreek}{bold}{LGR}{fos}{bx}{n}
\DeclareSymbolFont{osgreekit}{LGR}{fos}{m}{it}
\SetSymbolFont{osgreekit}{bold}{LGR}{fos}{bx}{it}

\DeclareMathSymbol{\upmu}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\muup}{\mathord}{osgreek}{`m}
\DeclareMathSymbol{\mu}{\mathord}{osgreekit}{`m}

% set smart greek letters
\def\setsmartgreek{%
    \expandafter\ifx\csname muup\endcsname\relax 
    \else \def\stgreekC##1##2;{##2up}\fi
    \expandafter\ifx\csname upmu\endcsname\relax 
    \else \def\stgreekC##1##2;{up##2}\fi
    \ifx\stgreekC\undefined \errmessage{upright greek letters are not available}%
    \else \expandafter\setsmartgreekA \fi
}
\def\setsmartgreekA#1{\ifx#1\relax \else
    \expandafter \let \csname it:\string#1\endcsname = #1%
    \edef#1{\noexpand\ifnum\fam=0
        \expandafter\noexpand\csname\expandafter\stgreekC\string#1;\endcsname
        \noexpand\else \expandafter\noexpand\csname it:\string#1\endcsname
        \noexpand\fi}%
    \expandafter\setsmartgreekA \fi
}

\setsmartgreek\mu\relax

% substitute letters and numbers with text font in math mode
\usepackage[defaultrm]{mathastext}

% siunitx should keep working
\usepackage{siunitx}
\sisetup{math-micro=\mathrm{\upmu}}
\AtBeginDocument{\sisetup{detect-all}}

\begin{document}

Regular numbers and unit: \SI{42}{\micro\meter}

\textit{Regular numbers and unit: \SI{42}{\micro\meter}}

\textbf{Bold numbers and unit: \SI{42}{\micro\meter}}

\textbf{\textit{Bold italic numbers and unit: \SI{42}{\micro\meter}}}

\[
\int \sum X_\mathrm{\textcolor{red}{\mu\upmu}}\mu\upmu\mathrm{\textcolor{red}{\mu\upmu}}A + 2 = \SI{42}{\micro\meter\ampere\gram}
\]

All \textcolor{red}{red} should be upright. 

\end{document}

在此处输入图片描述

确实,mathastext默认情况下不会对希腊字母进行任何更改。但它确实定义了自己的数学字母表版本(以便使用正确的——在它看来!——字体)。

相关内容