This is another follow-up question to this one with @wipet's great answer on how to create "smart greek letters" which in math mode are normally italic, but recognize the \mathrm
command to become upright.
I was using this setup successfully with various math fonts, but since I'm newly supposed to use Open Sans as text font in my presentation, I'm not so happy with the sansmathfonts package anymore. So I thought give mathastext a try. I already successfully declared a greek math alphabet, but \usepackage{mathastext}
breaks @wipet's \setsmartgreek
.
I don't really understand why, as mathastext is supposed to leave the greek letters untouched by default. I also tried various other package options. Is there a way to get the smart greek letters working again?
Original 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}
Extended MWE
The answer of jfbu suggests to use the option defaultrm
or defaultalphabets
of mathastext, which works for the original MWE. But for normal letters the font falls back to sansmathfonts
inside \mathrm
. Without defaultrm
the smart greek letters don't work, but the fonts are right. Is there any solution?
The manual of mathastext is quite frustrating to read on that topic, there is an extended discussion, but it does not really get to point unfortunately.
\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}