5

I'm using fourier and erewhon as my main (serif) text and math fonts. I want to use TeX Gyre Heros as sans font. Because this font has no math support, I use mathastext to fake it only for the sans font without changing the serif font.

However, despite using the subdued option of mathastext, it changes the digits of the serif font in math mode.

Adding the nodigits option to mathastext fixes the problem for the serif font, but no longer changes the digits for the sans font.

Does anyone know how to fix this?

MWE:

\documentclass{article}

\usepackage{fourier}
\usepackage[no-math]{fontspec}
\setmainfont{erewhon}
\usepackage[subdued,italic]{mathastext}

\setsansfont{Tex Gyre Heros}
\renewcommand\familydefault\sfdefault
\Mathastext[sans]
\renewcommand\familydefault\rmdefault

\begin{document}
123 $123$
\textsf{123 \MTversion{sans}{$123$}}
\end{document}

enter image description here

  • AFAIK "sans" is a shorthand for "sansserif". So your aim is somewhat confusing. .. – Jhor Aug 5 '18 at 15:00
  • @Jhor Oh well, thanks for the heads up. I fixed my question. – Anedas Aug 5 '18 at 17:19
  • (quick summary for passers-by) This was a bug in mathastext which has been fixed in version 1.3t. – user4686 Sep 8 '18 at 8:07
3

This appears to be a bug in mathastext relative to its "subdued" mode. Brief explanation: mathastext reassings mathcodes of letters and digits and some ascii characters. When "subdued" mode was added, this was done not by reverting the mathcode changes (the package does not keep memory of earlier situation) but by hacking into the LaTeX math versions in order for the Font to be used to match the original, un-modified, fonts for either letters or operators.

For some reason I have forgotten, when copying over the pristine "letters" and "operators" status to the mathastext own math groups, mathastext deliberately modifies the font encoding to be the \encodingdefault (at time of loading mathastext). In the MWE where mathastext is loaded after fontspec, this encoding default will be TU, hence, rather than storing \T1/futs/m/n (as set up by fourier package for digits) in its own digits font for subdued mode, it stores \TU/futs/m/n. This then causes:

LaTeX Font Warning: Font shape `TU/futs/m/n' undefined
(Font)              using `TU/lmr/m/n' instead on input line 37.

which explains the altered looks of digits in math mode.

In the case where mathastext is loaded before fontspec, and after fourier, the \encodingdefault is the T1, not TU and all goes well.

I have forgotten the rationale behind this usage of \encodingdefault. There is also some code to deliberately replace OT1 by T1 if \encodingdefault is OT1. All of this was done at a stage of developement of mathastext when it had never been tested with xelatex/lualatex/fontspec anyhow.

One could imagine for the OP's problem modifying \encodingdefault back from TU to T1 for the duration of loading mathastext after fontspec but from brief look at code this would give unexpected consequences due to some other usages of \encodingdefault in the package code.

Of course, one can also fix this by re-doing the mathastext hacks, but this is a bit scary code. It would look something like this:

\documentclass{article}

\usepackage{fourier}
\usepackage[no-math]{fontspec}
\setmainfont{erewhon}
\usepackage[subdued,italic]{mathastext}
\makeatletter
  \def\mst@reserved#1\getanddefine@fonts\symletters#2#3\@nil{%
        \def\mst@normalmv@letter{#2}}
  \expandafter\mst@reserved\mv@normal\@nil
  \def\mst@reserved#1\getanddefine@fonts\symletters#2#3\@nil{%
        \def\mst@boldmv@letter{#2}}
  \expandafter\mst@reserved\mv@bold\@nil
  \def\mst@reserved#1\getanddefine@fonts\symoperators#2#3\@nil{%
        \def\mst@normalmv@operator{#2}}
  \expandafter\mst@reserved\mv@normal\@nil
  \def\mst@reserved#1\getanddefine@fonts\symoperators#2#3\@nil{%
        \def\mst@boldmv@operator{#2}}
  \expandafter\mst@reserved\mv@bold\@nil
%%
  % \edef\mst@tmp{\encodingdefault} % NO!
  \def\mst@reserved#1/#2/#3/#4/{\gdef\mst@debut{#1}\gdef\mst@reste{#2/#3/#4}}
  \begingroup\escapechar\m@ne
      % \xdef\mst@funnyoti{\expandafter\string\csname OT1\endcsname}
      \expandafter\expandafter\expandafter
          \mst@reserved\expandafter\string\mst@normalmv@operator/
  \endgroup
  % \ifx\mst@debut\mst@funnyoti\ifx\mst@tmp\mst@oti\def\mst@tmp{T1}\fi\fi
  \edef\mst@normalmv@operator{\expandafter\noexpand\csname
                               %\mst@tmp/\mst@reste\endcsname}
                               \mst@debut/\mst@reste\endcsname}
  \begingroup\escapechar\m@ne
      \expandafter\expandafter\expandafter
          \mst@reserved\expandafter\string\mst@boldmv@operator/
  \endgroup
  % \ifx\mst@debut\mst@funnyoti\ifx\mst@tmp\mst@oti\def\mst@tmp{T1}\fi\fi
  \edef\mst@boldmv@operator{\expandafter\noexpand\csname
                             %\mst@tmp/\mst@reste\endcsname}
                             \mst@debut/\mst@reste\endcsname}
%  \AtEndOfPackage{
  \typeout{** ...correcting subdued mode...}
  \expandafter\SetSymbolFont@ \expandafter\mv@normal\mst@normalmv@letter\symmtletterfont
  \expandafter\SetSymbolFont@ \expandafter\mv@bold\mst@boldmv@letter\symmtletterfont
  \expandafter\SetSymbolFont@ \expandafter\mv@normal\mst@normalmv@operator\symmtoperatorfont
  \expandafter\SetSymbolFont@ \expandafter\mv@bold\mst@boldmv@operator\symmtoperatorfont
  \typeout{** ...done.}
%  }
\makeatother

\setsansfont{TeX Gyre Heros}
\renewcommand\familydefault\sfdefault
\Mathastext[sans]
\renewcommand\familydefault\rmdefault

\begin{document}

123 $123$
% \textsf{123 \MTversion{sans}{$123$}}

\MTversion{sans}
123 $123$

\MTversion{normal}

\thispagestyle{empty}\showoutput
\end{document}

Of course it is not practical to add such a big hack to preamble (I could reduce its size but kept close to original, just to modify it here and there). Hopefully some update to mathastext will fix this, but the author will have to remember why in first place the code insisted on using \encodingdefault (there must be a reason...).

enter image description here

  • 1
    a fix has been incorporated to release 1.3s – user4686 Aug 21 '18 at 19:33
  • Thank you very much for the detailed explanation and the fix! I've just tried version 1.3s and it works with the MWE from my original question! – Anedas Aug 22 '18 at 7:00
  • thanks for testing the 1.3s 2018/08/21 and reporting back! hopefully it will work in other contexts as well. – user4686 Aug 22 '18 at 7:16
  • ah sorry. I make a mistake in 1.3s which did not show in your MWE (due to presence of fourier package). Hence there will be 1.3t 2018/08/22 ... – user4686 Aug 22 '18 at 9:14
  • 1
    Thanks! I can confirm that 1.3t still works for me :) – Anedas Aug 24 '18 at 8:56
2

The problem with the wrong digits is fixed, if mathastext is loaded before fontspec:

\documentclass{article}

\usepackage{fourier}
\usepackage[subdued,italic]{mathastext}
\usepackage[no-math]{fontspec}
\setmainfont{erewhon}

\setsansfont{Tex Gyre Heros}
\renewcommand\familydefault\sfdefault
\Mathastext[sans]
\renewcommand\familydefault\rmdefault

\begin{document}
123 $123$
\textsf{123 \MTversion{sans}{$123$}}
\end{document}

However, the documentation of mathastext clearly states that this should not be done (p. 21)!

Despite fixing the digits, mathastext still changes some math mode font metrics for the serif font, but I can live with that.

  • 1
    side note: use \MTversion*{sans} if you don't want to change text font. With \MTversion{sans} as in your MWE you don't need the \textsf (but may still need the {..} to limit the scope). Also, on Mac OS with xelatex, I need TeX Gyre Heros not Tex Gyre Heros spelling with TL2018. – user4686 Aug 21 '18 at 15:37
  • I didn't know about the starred version \MTversion* - thanks for pointing that out. I used \textsf for comparison with the sans font in text mode, but you're right, it's not required for \MTversion. The name Tex Gyre Heros is used by TL2016 (yeah, I should probably perform an update, but I want to finish my thesis first). – Anedas Aug 22 '18 at 7:04

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.