2

Compile the following MWE with LuaLaTeX

\documentclass{article}

\usepackage{libertinus}
% Set the same teletype font for text, \mathtt and \symtt
\setmonofont{Inconsolatazi4-Regular.otf}[Scale=MatchLowercase]
\setmathtt{Inconsolatazi4-Regular.otf}[Scale=MatchLowercase]
\setmathfont{Inconsolatazi4-Regular.otf}[range={tt/{latin,Latin}->up},Scale=MatchLowercase]

% I hoped the following line would do the trick, but it only triggers a compilation error
% \setmathfont{Inconsolatazi4-Regular.otf}[range={tt/{latin,Latin}->up,"2010->"002D},Scale=MatchLowercase]

\begin{document}
\begin{tabular}{lcc}\hline
                                & \texttt{\textbackslash mathhyphen}  &  normal \texttt{-} \\\hline
\texttt{\textbackslash mathrm}  &  $\mathrm{init\mathhyphen cmd}$  &  $\mathrm{init-cmd}$ \\
\texttt{\textbackslash mathsf}  &  $\mathsf{init\mathhyphen cmd}$  &  $\mathsf{init-cmd}$ \\
\texttt{\textbackslash mathit}  &  $\mathit{init\mathhyphen cmd}$  &  $\mathit{init-cmd}$ \\
\texttt{\textbackslash mathtt}  &  $\mathtt{init\mathhyphen cmd}$  &  $\mathtt{init-cmd}$ \\\hline
\end{tabular}
\end{document}

The result is

result

Note the missing hyphen for the teletype math font.

The log says

Missing character: There is no ‐ (U+2010) in font [Inconsolatazi4-Regular.otf]:mode=base;script=latn;language=DFLT;+tlig;!

This is understandable, because unicode-math maps the Latin teletype symbols from the 1D4xxx range to the ASCII range and takes the glyphs from Inconsolata. (Note the range=tt->up option of \setmathfont) This re-mapping seems to fail for the hyphen.

However, the font Inconsolata contains a glyph for the hyphen at position 002D. If the normal hyphen is input, this glyph is used but with the spacing of a binary operator.

I hoped the line

\setmathfont{Inconsolatazi4-Regular.otf}[range={tt/{latin,Latin}->up,"2010->"002D},Scale=MatchLowercase]

would do the trick, but it doesn't.

How can I tell fontspec to use the same glyph as a math hyphen and apply the correct spacing?

4
  • Does \mathtt{init}-\mathtt{cmd} work for you?
    – Davislor
    Commented Feb 17, 2020 at 17:38
  • 2
    @Davistor I assume you wanted to write \mathtt{init}\mathhyphen\mathtt{cmd}. (The simple hyphen translates into a minus sign.) Of course, this works. But: a) The hyphen is taken from the wrong font. The hyphen is printed in Libertine Math and not in Consolas. b) This is only a MWE. In reality, I have a macro \foo, which is defined to be \ensuremath{some\mathhyphen text}, and later in the document I have \mathtt{\foo}. Hence your workaround is not an option.
    – nagmat84
    Commented Feb 17, 2020 at 17:47
  • Since the "literal" hyphen is in a math string, it is being interpreted as a minus sign, with spaces around it that, in the context shown, seem excessive. Commented Feb 17, 2020 at 19:54
  • 1
    What about \AtBeginDocument{\def\mathhyphen{\textrm{-}}} ? Commented Feb 19, 2020 at 19:15

1 Answer 1

4
+200

You could locally reset the Umathcode. Or you could use the (very new and experimental) fallback method to pull in a glyph from another font. This doesn't work for a math alphabet so you would have to use a text command, which is a bit cheating.

\documentclass{article}
\usepackage{unicode-math}
\directlua
{luaotfload.add_fallback
  ("myfallback",
   {
     "file:LibertinusMath-Regular.otf:mode=node;color=FF0000;" %red color for better view ...
   }
  )
}

\setmonofont{Inconsolatazi4-Regular.otf}[Scale=MatchLowercase,RawFeature={fallback=myfallback}]

\begin{document}
\let\orimathtt\mathtt
\renewcommand\mathtt[1]{\orimathtt{\Umathcode `\^^^^2010="7"0"2D #1}}

$\mathtt{init\mathhyphen cmd}$ 

$\texttt{init\mathhyphen cmd}$
\end{document}

enter image description here

3
  • Why does \mathhyphen need an argument?
    – nagmat84
    Commented Feb 20, 2020 at 21:52
  • ? there is no argument. Commented Feb 20, 2020 at 21:54
  • Sorry, my fault. I scanned over the lines too fast and I am obviously tired. The line in question states \renewcommand\mathtt[1]…, but I read \renewcommand\mathhyphen[1]…. I erroneously read what I thought should be there, not what is there. But it is all clear now and makes perfect sense.
    – nagmat84
    Commented Feb 20, 2020 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .