4

This post summarizes a digression that began in comments to a different post with User JFBU.

The basic question here is what commands are needed to get the full character-set of a non-math font (in this case, TeX Gyre Termes) to be accessible in math mode, for the sake of the diverse formatting that is possible in math mode.

I would like to open this question to the TeX.SE community now.

2 Answers2

2

This will be more an answer to your answer than to your question! ;-) Indeed I just want to clarify this issue of mathastext vs fontspec with or without its option no-math. What you say in your answer is due to the use of mathspec in your code. Indeed, here is an extract from mathspec.pdf:

Actually, mathspec ordinarily loads fontspec with the no-math option. To cancel this, explicitly use the math option: e.g. \usepackage[math]{mathspec}.

Package mathspec (for XeLaTeX) is specially designed to set up the math fonts (for the Latin letters, Digits, and Greek letters) and it provides commands for defining also the math alphabets. It automatically loads fontspec with option no-math. It can be used to set up the Greek letters, and then one can load mathastext to set up the Latin letters and Digits (as well as a few more punctuation characters from the Basic Latin set).

Package mathastext was designed for traditional TeX engines, but it is usable with Unicode engines; as clearly stated in its docs it only deals with the Basic Latin subset of the Unicode universe. Thanks to your question/answer, I realize that it is possible to use it together with mathspec (I initially thought they would work one against the other). For example one can use mathspec to set up the font for the Greek letters, and let mathastext do its modest job with the Basic Latin characters. Already with standard LaTeX and T1 encoding, mathastext does nothing special for letters with diacritics such as é or à directly input in the source in math mode (I believe the same applies to mathspec).

Here is now a small document allowing you to test mathastext under XeTeX (or LuaLaTeX) with various Unicode fonts of your choice, with or without mathspec (XeTeX only), and also, when not using mathspec with or without the option no-math to fontspec. As it stands, the source does not input mathspec and you will be able to see the importance of no-math option of fontspec when using mathastext. But to see the Greek letters, also when input directly in math mode as $α$ for example, you will need to uncomment the mathspec loading (XeTeX only). Hope it helps!

\documentclass[12pt]{article}

\usepackage{amsmath} % problem with `\- !!! (see below)

%% \usepackage{mathspec}
%% \setmathsfont(Greek)[Uppercase=Regular,Lowercase=Regular]{Arial}

\usepackage[no-math]{fontspec} 
% The no-math option should be used for not interfering with
% mathsastext. Important: mathspec loads by default fontspec with
% the no-math option.

%\defaultfontfeatures{Ligatures=TeX} % syntaxe pour luatex
\defaultfontfeatures{Mapping=tex-text} % syntaxe pour xetex

\usepackage[noendash]{mathastext} 
% noendash because of amsmath, see first line
% after \begin{document}

%%% mathastext allows to define multiple math versions,
%%% beyond the default "normal" one:

\setmainfont{Times New Roman} % or any other font
\Mathastext[times]            % no space in the math version name

\setmainfont{Impact}          % this is a very heavy upright
                              % font
\Mathastext[impact]           % available on the computer at
                              % my office

\setmainfont{Arial}           
\Mathastext[arial]       

\newcommand{\textandmathandgreek}{   
   \centerline{abcdefghijklmnopqrstuvwxyz\rlap{ (text)}}
   \centerline{$abcdefghijklmnopqrstuvwxyz$\rlap{ (math)}}

   \centerline{ABCDEFGHIJKLMNOPQRSTUVWXYZ\rlap{ (text)}}
   \centerline{$ABCDEFGHIJKLMNOPQRSTUVWXYZ$\rlap{ (math)}}

   \centerline{0123456789\rlap{ (text)}}
   \centerline{$\mathrm{0123456789}$\rlap{ (math with \texttt{\symbol{92}mathrm})}}
   \centerline{$0123456789$\rlap{ (math)}}

   \centerline{!\,?\,*\,,\,.\,:\,;\,+\,\textendash\,=\,(\,)\,[\,]\,/\,\#\,%
   \$\,\%\,\&\,<\,>\,|\,\{\,\}\,\symbol{92}\rlap{ (text)}}
   \centerline{$!\,?\,*\,,\,.\,:\,;\,+\,-\,=\,(\,)\,[\,]\,/\,\#\,%
   \$\,\%\,\&\,<\,>\,|\,\{\,\}\,\backslash$\rlap{ (math)}}

   \centerline{αβγΔΓΦ\rlap{ (text)}}
   \centerline{$αβγΔΓΦ$\rlap{ (math, direct input)}}
   \centerline{$\alpha\beta\gamma\Delta\Gamma\Phi$\rlap{
       (math, named letters)}}

   \medskip
}

\pagestyle{empty}

\begin{document}
% FIX FOR THE AMSMATH PROBLEM WITH MINUS SIGN
%\luatexUmathcode`\-="2 \symmtoperatorfont "2013 \relax
\XeTeXmathcode`\- ="2 \symmtoperatorfont "2013 \relax

\MTversion{normal}  % font at the time of loading mathastext
\centerline{Default Text Font}
\textandmathandgreek

\MTversion{times}
\centerline{Times New Roman}
\textandmathandgreek

\MTversion{impact}
\centerline{Impact}
\textandmathandgreek

\MTversion{arial}
\centerline{Arial}
\textandmathandgreek

The main job of mathastext is to allow to
type the characters from the Basic Latin set \emph{as is} in math mode,
and to have them without further ado be in the same font as
the document text (possibly in italic, with
\emph{italic} passed to the package, and when the font used
does exist in Italic Style). But for Unicode
characters not in the Basic Latin set (such as é, or æ or ŋ)
they should be input in math mode within a \verb|\mathrm| or
similar math alphabet commands $\zeta=\int
\frac{\mathrm{é}}{\mathbf{æ}}d\mathit{ŋ}$, or alternatively
within a \verb|\text| command (from package amsmath)
$\zeta=\int \frac{\text{é}}{\text{\bfseries
    æ}}d\text{\itshape ŋ}$ (this offers more possibilities
as the \verb|\math..| commands are not cumulative).

Mathastext has options related to Greek letters but only in
relation to fonts in \TeX\ format (fonts in LGR encoding, or
the Euler font, or the Symbol font). The Greek letters can
be set up arbitrarily by another package: with Xe\TeX{}
one can use the package \emph{mathspec} to specify the Greek
letters, this is compatible with loading also mathastext for
dealing with Latin letters and Digits (a job which can also
be done by mathspec). 

\end{document}
  • I chose the Impact font in the code sample above, because it is not available with an Italic Style. So if you try the above code adding the option italic to package mathastext you will see that it does work with Times New Roman, or any other font with an italic version, but not with Impact, or say, Arial Black. This is normal behavior of mathastext and would be the same with traditional TeX fonts (the absence of italic shape provoking probably a substitution warning in the LaTeX log file). –  May 04 '11 at 14:28
  • @brannerchinese: I edited my earlier answer, because I made therein a wrong assertion regarding mathspec. Thanks to your answer which made me finally realize that! –  May 04 '11 at 14:51
  • in fact mathspec automatically loads fontspec and pass options to it, so in the code above, if one un-comments the mathspec line, one can comment-out the fontspec one. –  May 04 '11 at 15:03
  • in the code above it would be better for readability to use \textbackslash rather than \symbol{92} –  May 04 '11 at 16:13
  • @JFBU: All this is clear. However, to use Roman characters with diacritics (and other non-lower ASCII Roman characters) in math mode, it seems one does still need \rm, correct? Also, if one wants formulas to have italic Roman script, then mathastext needs option italic, but in that case when math formatting is applied to non-formulas within the same document, they will be italicized unless they are explicitly turned off, for example with \rm. Correct? – brannerchinese May 04 '11 at 19:09
  • @brannerchinese: I think the answer is yes on both counts, but I am not sure I understand what you mean by math formatting. By the way \rm is frowned upon, although perhaps in math mode it is not that bad (as I never use it I forgot its defects), because the \mathrm, \mathit, etc... share the defects of \rm etc... of not cumulating effects. The \text macro of amsmath is better. –  May 04 '11 at 20:26
  • and with \setmainfont{Times} \usepackage[noendash,italic]{mathastext} \let\olditdefault\itdefault \renewcommand{\itdefault}{\rmdefault} \Mathastext[Upright] \renewcommand{\itdefault}{\olditdefault} in your preamble you can alternate in your document between upright Basic Latin letters in math mode and italic ones: after \MTversion{Upright} letters will be as if you had used \rm on them, and after \MTversion{normal} you return to the italic. This macro has to be called outside of math mode. –  May 04 '11 at 20:30
  • it seems \rm ab in math mode is about exactly like \mathrm{ab} so I shouldn't have denigrated it; but this is only for short variable names, if one wants spaces one has to use \textrm which behaves better if amsmath is loaded (or only amstext which would avoid the Unicode difficulty we talked about elsewhere), and even has the shorter form \text (which takes the style of the surrounding text off math mode). –  May 04 '11 at 20:46
  • @jfbu: Thanks for your suggestions about itdefault. And you're quite right about \rm; I just use it for speed and readability. – brannerchinese May 04 '11 at 21:20
1

One answer, the one that JFBU and I have been discussing in the comments to the other post, is to use package mathastext to accomplish this. mathastext is very good, but it does not appear to me that works exactly as described in the documentation.

I am using XeLaTeX and fontspec in order to get full CJK functionality (not illustrated here). P. 8 of the mathastext documentation says

note to users of XeTeX/LuaLaTeX with fontspec: it has to be loaded with the option no-math, and before mathastext.

JFBU has also shown that the option noendash must be passed to mathastext in order to avoid a small conflict that presently exists with amsmath. (There is a different solution to the same problem by User Philipp, here.)

However, I do not see any obvious effects of the no-math option. Below is minimal code designed to show the difference in output when no-math is passed to fontspec. A string of numerals plus the non-math character ā is output three times: first as text, then formatted simply as math, and finally formatted as math but with \textrm.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt]{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{mathspec}            % replaces amssymb; do not use unicode-math
\usepackage{xeCJK}
\usepackage{fontspec}

\defaultfontfeatures{Mapping=tex-text}
\setmainfont{TeX Gyre Termes}
\usepackage[italic,noendash]{mathastext}  % propagates document text font to math mode

\begin{document}

\centerline{0123456789ā}\centerline{\(0123456789ā\)}\centerline{\(\textrm{0123456789ā}\)}

\end{document}

Here is the output:

With the option no-math passed to fontspec:

with no-math 4

Without the option no-math passed to fontspec:

without no-math 4

I see no difference between the two outputs. The presence or absence of no-math does not affect the appearance of the text, and the non-math character ā does not appear in math mode unless \textrm is used.

So it appears to me that mathastext alone is not sufficient to get all the characters in a non-math font to appear with math formatting. I'd be grateful to know if there is a simpler way to accomplish this task than with mathastext plus \textrm in each math environment.