0

I have an OpenType font that has #b5 (micro sign) undefined but #3bc (Greek small letter mu) defined. Since the font doesn't have any other Greek letters the reason it has that glyph at all is surely mostly to be used as "micro" and because that is in Latin-1. I think that the font ought to have put that glyph as character 0xb5 but it doesn't.

Because of that \textmu doesn't render anything. I would like that and plain #b5 characters in the text to use that glyph. Finding this question I tried this:

% -*- TeX-engine: luatex; -*-
\documentclass{article}
\usepackage{fontspec}

\directlua
{
  local function patch(fontdata)
    if string.match(fontdata.psname or "", "^Berling") then
      fontdata.characters[0xb5] = fontdata.characters[0x3bc]
    end
  end
  luatexbase.add_to_callback("luaotfload.patch_font", patch, "missing glyph")
}

\setmainfont{Berling}

\begin{document}
b5 = µ, 3bc = μ, textmu = \textmu
\end{document}

Now I get the same glyph regardless if I write the 0xb5 character, the 0x3bc character (as an added bonus) or \textmu in the file.

Is this a good solution, or is there a better way? (In my use case I don't want to change the font file.) One way I think would be better is if it explicitly checks if the code point is undefined (and the other one is defined!), but I don't know how to do that. But maybe there is something generally better in some other way? (First I tried to use the "substitution" feature, but discovered that I could only use that to replace an existing character.)

Note that I don't want to stop anything working for other fonts. With the suggestion above for example

\textmu\ \& \texttt{\textmu}

shows the character in two different fonts. Just redefining \textmu to always pull up character 3bc wouldn't do.

1 Answer 1

1

You can use newunicodechar:

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\newunicodechar{µ}{α}
\newunicodechar{μ}{β}

\setmainfont{DejaVu Serif}

\begin{document}
b5 = µ, 3bc = μ, textmu = \textmu
\end{document}

enter image description here


In order to check if a glyph exists you can use \iffontchar, which is the built-in command for checking availability of glyphs. This allows for a more font-independent solution that takes all possibilities into account. In the MWE below I used α and β as substitutes for the different versions of μ, to make the example more clear.

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\setmainfont{DejaVu Serif}
\iffontchar\font`α
   \iffontchar\font`β
      \relax % both glyphs exist, do nothing
   \else
      % α exists but β doesn't, define β to be α
      % and set \textmu to be α
      \newunicodechar{β}{α}
      \def\textmu{α}
   \fi
\else % α does not exist
   \iffontchar\font`β
      % β exists but α doesn't, define α to be β
      % and set \textmu to be β
      \newunicodechar{α}{β}
      \def\textmu{β}
   \else % neither α or β exists
      \GenericError{Glyph error}{No alpha or beta is defined in font}
   \fi
\fi

\begin{document}

Text with α and β and \textmu

\end{document}
5
  • Ah, that's simpler. With \newunicodechar{µ}{μ} I indeed get b5 to look as 3bc, but still \textmu turns out empty and yields a warning about missing character in the log. Ideally I'd have a way to define this in a way that will also will satisfy the check that \textmu does – if needing to redefine \textmu explicitly it needs to should check so that it only does something other for this font.
    – pst
    Commented Apr 23, 2021 at 16:27
  • @pst I'm not sure I understand exactly what you mean. Are you looking for a redefinition of \textmu that shows correctly with your font, similar to your Lua function? In that case \def\textmu{μ} with the right μ (so 3bc) should do it. Or are you looking for an implementation of \textmu that checks specifically which glyph is defined and then choose that one? (if yes, why?) Or still something else?
    – Marijn
    Commented Apr 23, 2021 at 19:49
  • I'm looking for a way to use this font so that any kind of µ uses the glyph that the font has, But not in a way to stops things working for other fonts. The problem with a simple redefinition is that it doesn't work for other fonts, but that is true for this solution with \newunicodechar anyway, so it was misguided of me to focus just on that.
    – pst
    Commented Apr 23, 2021 at 20:09
  • I have now edited the question, hopefully making it clearer. Maybe it would be better if I write the second half of the question as an answer to the question to compare with other approaches?)
    – pst
    Commented Apr 23, 2021 at 20:31
  • @pst I have added an edit, maybe this is what you were thinking about?
    – Marijn
    Commented Apr 23, 2021 at 20:44

You must log in to answer this question.

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