# Why is my circumflex \^ mapping to a non-breakable space U+00A0 when followed by {} using a custom encoding \DeclareUnicodeEncoding?

Using \DeclareUnicodeEncoding, I defined my own encoding that adjusts the \^ such that it maps to a circumflex accent U+005E rather than to a combining circumflex accent U+0302 (which is already set by xunicode to represent the modified letter accent U+02C6).

I hit a few bumps/snags/hurdles.

1. The first problem was caused by a circumflex appearing in listings environments that was solved by adding \input{tuenc.def} (with \makeatletter).
2. The second problem appears when typing \^{}, which causes a log error.

# Log Error

file.log:505:Missing character: There is no   in font [/usr/local/texlive/2016/texmf-dist/fonts/opentype/rozynski/comicneue/ComicNeue_Regular.otf]/OT:mapping=tex-text;!


# Code

See comments in the code. Note that I picked ComicNeue_Regular.otf specifically because it does not support U+0302 or U+00A0, and it is contained within TeX Live.

\documentclass{article}
\usepackage{fontspec}
\usepackage{listings}
\makeatletter
\DeclareUnicodeEncoding{preventcombinedcircumflex}{
\input{tuenc.def}
\EncodingAccent{\^}{"005E}
}%
\makeatother

\setmainfont[
Path = /usr/local/texlive/2016/texmf-dist/fonts/opentype/rozynski/comicneue/ ,
Extension = .otf ,
UprightFont = *_Regular,
NFSSEncoding= preventcombinedcircumflex,
]{ComicNeue}

\begin{document}
% Solved the general issue by adding \EncodingAccent{\^}{"005E}, or so I thought.
\^2
% Solved the lstlisting issue was solved by adding \makeatletter\input{tuenc.def}
\begin{lstlisting}
|^2
\end{lstlisting}
% Unsolved
\^{}
\end{document}


# Notes

\catcode160=15


then you find

(/usr/local/texlive/2017/texmf-dist/tex/latex/base/tuenc.def
! Text line contains an invalid character.
l.84   \if\relax\detokenize{#2}\relax
\else#2\fi
?


which is... interesting and translates as: it's not your fault..

The offending line is

  \if\relax\detokenize{#2}\relax^^a0\else#2\fi


which says if the argument of the accent command is empty use a non breaking space. You need something as when using unicode combining characters you don't want a\"{} to put the accent on the a because the following combining character had no base to act on.

But your example shows it's not safe to use ^^a0 . Instead of inputting tuenc.def input a local copy where

\def\add@unicode@accent#1#2{%
\if\relax\detokenize{#2}\relax^^a0\else#2\fi
\char#1\relax}


has been changed to (I guess)

\def\add@unicode@accent#1#2{%
\if\relax\detokenize{#2}\relax\space\else#2\fi
\char#1\relax}


perhaps we should use space always. I'll raise it with the team....

• Very interesting indeed. \catcode160 is a non-breakable space which you set to category code 15, correct? Oct 18 '17 at 18:39
• @JonathanKomar just as a debugging aid catcode 15 is invalid character so tex stops at the point the character first got into the system. I added a fix to my answer. Oct 18 '17 at 18:42
• Wait, wouldn't it be much better to put the redefinition in @JonathanKomar's code after loading tuenc.def rather than producing yet another .def file that would be a slight variant of tuenc.def?? Oct 19 '17 at 14:37