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
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?
\mathtt{init}-\mathtt{cmd}
work for you?\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.\AtBeginDocument{\def\mathhyphen{\textrm{-}}}
?