3

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}

enter image description here

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}

enter image description here

  • @wipet this is a follow-up question to your answer. – thewaywewalk Oct 28 '17 at 9:16
1

In my initial answer I did not even check the @wipet \setsmartgreek code. My initial answer suggested using defaultrm option to mathastext because this prevents the package of modifying the font with which \mathrm works. But in comments it was reported this cancels too much of mathastext influence. So I checked \setsmartgreek's code and perhaps one only needs to use this modified version:

% 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

It works in my testing with the OP's MWE.

Addition by OP:

The above answer just seems to work in case the -class is used. In other cases (e.g. KOMA-script, article) it causes an error, not apparent with the old macro defintion. A simple check for the loaded class can solve this issue:

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

An within the macro the related line has to be changed to:

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


former answer:

Pass option defaultrm to mathastext, or probably better the all-encompassing defaultalphabets. (check details in the doc)

\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}

enter image description here

Indeed, mathastext by default changes nothing to Greek letters. But it does define its own versions of the math alphabets (in order to use the correct -- in its view! -- fonts).

  • defaultrm works, defaultalphabets makes siunitx use the math font again in math mode. thanks! – thewaywewalk Oct 29 '17 at 7:58
  • the mathastext customized math alphabet is still available as \Mathrm (with capital M) in case you need it. Thanks for the infor about impact with siunitx of defaultalphabets. – user4686 Oct 29 '17 at 9:21
  • I realized, the effect of defaultrm and defaultalphabets depends on the siunitx settings. However, both cause \mathrm to use sansmathfont again. Do you know an easy solution for this? Otherwise I ask a new question. The manual is quite confusing on this. – thewaywewalk Oct 30 '17 at 11:54
  • unfortunately I have to unaccept this answer, as defaultrm breaks more than it fixes. A new question would be surely a duplicate though. Do you mind if I offer a bounty on the extended question? – thewaywewalk Oct 30 '17 at 12:19
  • 1
    @thewaywewalk fine with me, however \def\familyidentifier{0} should be \def\familyidentifier{0 } with a space after the 0 else the \ifnum test might misbehave (not in this particular case, but just to have good practice). The \symmtoperatorfont is not a macro but a chardef thing, and no space is needed after it to properly terminate it. – user4686 Nov 13 '17 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.