4

I’m trying to use unicode-math’s capabilities (\setmathfont[range=…]) to use a non-math font in math mode. For example:

\documentclass{scrartcl}

\usepackage{unicode-math}

\setmainfont{FreeSans}
\setsansfont{FreeSans}

\setmathfont{Latin Modern Math}
\setmathfont{FreeSans}[range=up/{greek,Greek,latin,Latin,num}]
\setmathfont{FreeSans Oblique}[range=it/{greek,Greek,latin,Latin,num}]
\setmathfont{FreeSans Bold}[range=bfup/{greek,Greek,latin,Latin,num}]

\begin{document}

abc123

\textit{abc123}

\textbf{abc123}

\bigskip

\begin{tabular}{ll}
    math        & $abc123$ \\
    mathit      & $\mathit{abc123}$ \\
    mathrm      & $\mathrm{abc123}$ \\
    mathup      & $\mathup{abc123}$ \\
    mathbf      & $\mathbf{abc123}$ \\
    mathbfup    & $\mathbfup{abc123}$
\end{tabular}

\end{document}

With this document, I would expect all text to be in FreeSans and the lines with mathrm and mathup as well as mathbf and mathbfup to be the same. However, my result is this:

result of test document

So, I have two questions about what is happening:

  1. Why are \mathit and \mathup different from \mathbfup? \mathbfup works as it should, producing text in FreeSans Bold. The other two, on the other hand, produce the default of Latin Modern Math.
  2. How do I get all of these commands to use FreeSans, just like \mathbfup does? I know about unicode-math’s command \setmathfontface⟨command⟩{⟨font name⟩}[⟨font features⟩], but that doesn’t let me specify a range like range=up/{greek,Greek,latin,Latin,num}, as far as I can tell. And shouldn’t unicode-math’s own commands like \mathup work correctly “out of the box”, anyway?

Version info: TeX Live 2017, LuaTeX v1.0.4, unicode-math 2017/01/27 v0.8d.

Socob
  • 1,693
  • Yes, it doesn't work properly. :( There's a (or more) question about this somewhere. – cfr Jun 16 '17 at 02:59
  • 1
    Free Sans isn't a maths font, is really the problem. There's no maths table. See https://tex.stackexchange.com/questions/326521/how-can-i-change-math-font-with-setmathfont-inside-a-group/327775#327775 and https://tex.stackexchange.com/questions/326876/is-it-possible-to-combine-version-and-range-when-specifying-a-maths-font-with-un. You aren't using versions, but I suspect the issue is the same. You are trying to feed unicode-math a font which is not a unicode maths font. It just isn't designed for that. – cfr Jun 16 '17 at 03:07
  • I thought the (part of) the whole point of the range option was to allow using non-math fonts? E.g. section 4.1.1 in the documentation. – Socob Jun 17 '17 at 17:00
  • I don't see anything in 4.1.1 which suggests that. It will 'fall through', yes, so a text font can be made to work that way, so it will fall through to ASCII (as in the Fraktur example there). But you are trying to do something which requires rather more in terms of specifying the ranges for specific bits of maths, and I don't see how that can possibly be expected to work for a non-maths font. And nothing in that section suggests that it is expected to work. – cfr Jun 17 '17 at 22:28
  • @cfr I’m not sure what makes you say that. I mean, yeah, my example is more complicated, but suppose I just specified (for example) bfup/{latin}. Why would that not work/what would be the purpose of being able to specify latin if it didn’t work this way? What’s more, it does work (as you can see in my example, Latin letters and numbers are in FreeSans in math mode, the first row in the table; what’s more, the text in \mathbfup is in FreeSans Bold as well). It’s just that some of the \math… commands don’t do what I’d expect. – Socob Jun 18 '17 at 01:22
  • Latin letters and numbers are ASCII. It isn't because it respects a font feature latin. It is because it falls through to ASCII. That's what the documentation says it does. But that won't work for anything outside those parts of the font. it uses a different part of a maths font. But the Oblique doesn't have anything there. You want it to use the equivalent of upright for italic, but to take it from the Oblique font. But that isn't going to work. – cfr Jun 18 '17 at 02:02
  • @cfr Sorry, but I’m still not getting your point. Yes, I know that FreeSans isn’t designed to be used as a math font and that proper math fonts have the different math alphabets encoded in specific places. I know that to use a font like FreeSans in this way, it has to fall back to ASCII, and that is what I want! – Socob Jun 18 '17 at 13:14
  • @cfr You say: “You want it to use the equivalent of upright for italic, but to take it from the Oblique font. But that isn't going to work.” But why not? Isn’t this the same case as with the Fraktur example in the documentation? I could make it explicit by saying \setmathfont{FreeSans Oblique}[range=it/{latin}->up], but that doesn’t change anything, and the documentation says that this fallback to ASCII happens automatically anyway if the glyphs are missing in the math plane. – Socob Jun 18 '17 at 13:14
  • I don't think so. Because italic isn't ASCII, so it can't fall through to ASCII the way it can for upright. Instead, it finds the font doesn't have the right characters and falls back to a font which does. – cfr Jun 18 '17 at 15:35
  • @cfr Ah, OK! So you mean \setmathfont{FreeSans}[range=up/{latin}] should work, but \setmathfont{FreeSans Oblique}[range=it/{latin}] shouldn’t? Then the question remains, why does \mathup produce Latin Modern Math and not FreeSans? (Again, it worked for \mathbfup). And again, “pure” math mode (without any \math... command) clearly shows FreeSans Oblique for letters, so \setmathfont{FreeSans Oblique}[range=it/{latin}] does seem to have worked in that case. – Socob Jun 19 '17 at 14:10
  • I don't know why it uses the oblique for letters. Probably my hypothesis is wrong. You've not defined anything for bf alone, I guess, so ... I'm not really sure. I wouldn't expect it to work, but I wouldn't expect it to fail in quite the way it fails, either. :( – cfr Jun 19 '17 at 22:50
  • The bug has been fixed with unicode-math version 0.8j, released 2018-01-07. – egreg Jan 10 '18 at 12:04

1 Answers1

4

It you want really to change \mathbf and similar: This is done easily by (re)declaring the math alphabets.

The problem starts if you want to adapt the \symXX commands which normally map chars to the glyphs on the mathematical plan. Here the implementation is rather broken. It works more or less if you set your fonts as the last "math font", but this breaks various mathematical font dimens. If you reset them by setting a real math font as the last font, then the symXX fonts break. See also this issue https://github.com/wspr/unicode-math/issues/331.

\documentclass{scrartcl}

\usepackage{unicode-math}

\setmainfont{FreeSans}[NFSSFamily=FreeSans]
\setsansfont{FreeSans}

\setmathfont{Latin Modern Math}



\setmathfont{FreeSans}[range=up/{greek,Greek,latin,Latin,num}]
\setmathfont{FreeSans Bold}[range=bfup/{greek,Greek,latin,Latin,num}]
\setmathfont{FreeSans Oblique}[range=it/{greek,Greek,latin,Latin}]

% better a real math font at the end, but breaks the ranges above:
\setmathfont{Latin Modern Math}[range=\int]

 %Or with `\setmathfontface`, see documentation
\DeclareMathAlphabet{\mathrm}{TU}{FreeSans}{m}{n}
\DeclareMathAlphabet{\mathbf}{TU}{FreeSans}{bx}{n}
\DeclareMathAlphabet{\mathit}{TU}{FreeSans}{m}{it}
\let\mathbfup\mathbf


\begin{document}

abc123 αβ

\textit{abc123 αβ}

\textbf{abc123 αβ}

\bigskip

\begin{tabular}{ll}
    math        & $abc123$ \\
    mathrm      & $\mathrm{abc123αβ}$ \\
    mathup      & $\mathup{abc123αβ}$ \\
    mathbf      & $\mathbf{abc123αβ}$ \\
    mathbfup    & $\mathbfup{abc123αβ}$\\
    symup       & $\symup{aα} \mupalpha $\\
    symit       & $\symit{a}\symit{α}\symit{β} \mita  \mitalpha  \mitvarTheta $\\
    symbf       & $\symbf{a}\symbf{b}\symbf{c}\symbf{α}\symbf{β} \mbfa \mbfb \mbfc \mbfvarTheta \mbfalpha \mbfbeta$ \\
\end{tabular}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • 1
    What is the optional NFSSFamily for? I have never seen that before. – Henri Menke Jun 16 '17 at 10:01
  • 2
    @HenriMenke: With it I can use the name FreeSans as family name in \DeclareMathAlphabet (it could also be "frS" or some other string). – Ulrike Fischer Jun 16 '17 at 10:23
  • Nice, thank you! Some questions out of interest, though: Why does declaring \mathrm in this way also cause \mathup to work correctly? And is the line \let\mathbfup\mathbf actually necessary (it seems to work for me without it, as well)? Finally, any clue on why \mathup and \mathbfup behave differently in this situation (aren’t they both unicode-math commands)? – Socob Jun 19 '17 at 14:18
  • \mathup and \mathbfup are defined in a bit special way in unicode-math. Imho you can add \mathup with \let\mathup\mathrm. – Ulrike Fischer Jun 19 '17 at 14:26
  • Actually, now that I think about it: I originally didn’t want to use \setmathfontface because I couldn’t specify ranges like with \setmathfont. With your solution, all glyphs from FreeSans would be used, not just Greek, Latin etc. For example: \mathrm{\nabla} or \mathbf{\nabla}. – Socob Jun 19 '17 at 14:37
  • You will have to think about your input. In this case you probably want \symbf{\nabla}. Btw: it is not a good idea to use \mathbf and similar command a lot directly in a document. You should define commands which reflect the sematic meaning, and there you can use \mathbf and \symbf. – Ulrike Fischer Jun 19 '17 at 14:45
  • In other words, there is no easy way? ;-) Ideally, yes, but if I’m designing a template for others, the built-in commands should keep working as my users expect them to. If they define some semantic command, they have to use \mathbf etc. to implement it. – Socob Jun 19 '17 at 14:51
  • Addendum to my first comment above: \let\mathbfup\mathbf is necessary if \setmathfont{Latin Modern Math}[range=\int] is used, i.e. \mathbfup is one of the commands that (buggily) follows the last set math font. – Socob Jun 19 '17 at 14:56