[Tex/LaTex] Command \nobreakspace unavailable when switching to T1 encoding under XeLaTeX

font-encodingsxetex

I'd like to use the luximono face for monospaced typesetting under XeLaTeX. Since it is already installed in the MikTeX distribution under T1 encoding, I switch to the latter when I need monospaced type. In order to do this, I have defined corresponding commands, as shown in the MWE below. Now, if I do not redefine the control sequence \nobreakspace in a form such as, e.g., I have done for my MWE, I get the error message Command \nobreakspace unavailable in encoding T1. My redefiniton is a workaround to this problem, and it has to it more of a patch than of a satisfactory solution. Could anyone possibly explain what is going on and how to solve it in a more appropriate way?

\documentclass[11pt]{memoir}

\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{fontspec}
\usepackage{xunicode}

\defaultfontfeatures{Numbers=OldStyle,Ligatures=TeX}
\setmainfont[BoldFont=texgyrepagella-bold.otf,
             ItalicFont=texgyrepagella-italic.otf,
             BoldItalicFont=texgyrepagella-bolditalic.otf]{texgyrepagella-regular.otf}

\usepackage{microtype}

\newcommand{\xlxmono}{
            \def\nobreakspace{\nobreak\space\nobreak}
            \fontfamily{ul9}\fontencoding{T1}
            \selectfont
           }
\renewcommand*{\ttfamily}{\xlxmono}
\renewcommand*{\texttt}[1]{
            {\xlxmono #1}
           }

\begin{document}

    {\ttfamily This is one line set in a monotype face with a nobreakspace command~here.}

    \texttt{This makes for yet another line with~it.}

\end{document}

Best Answer

If you modify your example to

\show\nobreakspace
\usepackage{fontspec}
\show\nobreakspace

You will see

> \nobreakspace=macro:
->\protect \nobreakspace  .
l.9 \show\nobreakspace

...

> \nobreakspace=macro:
->\EU1-cmd \nobreakspace \EU1\nobreakspace .
l.11 \show\nobreakspace

which means that the EU1 encoding setup has changed \nobreakspace from being an encoding-independent command to an encoding-dependent command, but without setting up a default definition so it works in all encodings. Re-instating the original definition as the default is probably the simplest thing, however note you should not be mixing fontenc and fontspec encoding definitions in a single document.

The original definition is

\DeclareRobustCommand{\nobreakspace}{%
   \leavevmode\nobreak\ }

So you could declare a default via

 \DeclareTextCommandDefault{\nobreakspace}{\leavevmode\nobreak\ } 

as noted in the comments.

Note that your definition would not work if it was used in vertical mode, eg the start of a paragraph as in that position the \nobreak would prevent a page break not a line break.

Unrelated to the question you have lots of white space tokens in your definition which will produce white space in the output: put a % at the end of every line within the definitiosn that ends with } or {.

Note also you need to use xelatex to use fontspec but you also load microtype which generates a warning that it does not work with xelatex:

Package microtype Warning: You don't seem to be using pdftex.
(microtype)                `microtype' only works with pdftex.
(microtype)                Try running `pdflatex' instead of `xelatex'.

Unless you have the beta version as pointed out by @UlrikeFischer. See. http://tlcontrib.metatex.org/cgi-bin/package.cgi/action=view/id=423

Related Question