# How can I substitute a command when a particular character is not present in a font?

I have a document I'm working on that makes use of a somewhat unusual character, the letter "y" with a macron: ȳ

Recently, I decided to switch fonts for a certain part of the document, Latin Modern Typewriter Prop. The document itself is typeset with LuaLaTeX and fontspec. This font works perfectly for my purposes, save for one problem, ȳ isn't present in it! Now, I was able to come up with a hack-ish work around:

\newcommand{\yn}{%
\phantom{y}\kern -1ex%
\=i\kern -0.678ex%
\setlength{\fboxsep}{0.2pt}%
\colorbox{white}{\color{white}\i}\kern -1ex y}


Essentially, what this does is overlays the letter "i" with macron, ī, with a dotless i, ı, in white, leaving only the macron visible, and then overlays that with the letter "y".

The problem is that I already have a lot of code using \=y to typeset ȳ that would be incredibly time consuming to change over. On top of that, should I ever switch fonts again (or if the current font gets a character for ȳ), it would be nice to not have to change things back.

So basically, my question is this: using fontspec (or whatever), is there any way I can make it so that when a particular character is not found, a particular command is substituted in place?

• I don't understand the problem as to why \=y becomes unacceptable under the font change. – Steven B. Segletes Sep 21 '16 at 14:46
• no what I mean is that, I was using \=y but if I wanted to use my work around, I'd have to change it to \yn. What I would like is to somehow change the behavior of \=y such that it uses \yn – Kevin Keith Sep 21 '16 at 15:29

A better solution requires two steps: first you have to undeclare the composition \=y and then redefine ȳ to do the composition.

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}

\setmonofont{Latin Modern Mono Prop}

\UndeclareUTFcomposite{x0233}{\=}{y}
\newunicodechar{ȳ}{\=y}

\begin{document}

ȳ \textsf{ȳ} \texttt{ȳ}

\end{document}


For making the combination work in \url you have to teach it to the package:

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{hyperref}

\setmonofont{Latin Modern Mono Prop}

\UndeclareUTFcomposite{x0233}{\=}{y}
\newunicodechar{ȳ}{\=y}
\makeatletter % for url
\makeatother

\begin{document}

ȳ \textsf{ȳ} \texttt{ȳ}

\url{www.ȳ.com}

\end{document}


With the versions of XeTeX and fontspec current on January 2017, the input for XeLaTeX can be simplified, because the Harfbuzz library will take care of the composites by itself:

\documentclass{article}
\usepackage{fontspec}
\usepackage{url}

\setmonofont{Latin Modern Mono Prop}

\makeatletter % for url
\makeatother

\begin{document}

ȳ \textsf{ȳ} \texttt{ȳ}

\url{www.ȳ.com}

\end{document}


On the other hand, this won't work with LuaLaTeX, so it can be better to use code that works with both engines. So, let's assume we have a text fonts with the ȳ glyph, but the monospaced font doesn't.

One can define a new encoding for the problematic font, in this case I call it TUy (the name is unimportant, so long as it is unique), based on the now default TU encoding.

\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{url}

\makeatletter % reading tuenc.def needs it
\DeclareUnicodeEncoding{TUy}{
\input{tuenc.def}% the new encoding is based on TU
\UndeclareComposite{\=}{y}% but with a change
}
\makeatother

\setmainfont{Libertinus Serif}
\setsansfont{Libertinus Sans}

\setmonofont{Latin Modern Mono Prop}[
NFSSEncoding=TUy
]

\newunicodechar{ȳ}{\=y}
\makeatletter % for url
\makeatother

\begin{document}

ȳ \textsf{ȳ} \texttt{ȳ}

\url{www.ȳ.com}

\end{document}


• Thanks! Is there any way to get this work inside of the \url command with hyperref? Right now, if I enter \url{http://www.\=y.com}, the result is http://www.y\char"0304\relax.com – Kevin Keith Sep 21 '16 at 15:50
• @KevinKeith Did you try directly entering ȳ? – egreg Sep 21 '16 at 16:02
• well strangely, when I enter it directly, like so \url{http://www.ȳ.com}, the ȳ just doesn't show up at all. \texttt{ȳ} works fine though – Kevin Keith Sep 21 '16 at 16:04
• @KevinKeith I added the workaround for \url, but don't expect the string will point to the right URL. – egreg Sep 21 '16 at 16:51
• Sure, that's fine. – Kevin Keith Sep 21 '16 at 16:59