[Tex/LaTex] Fonts: using XeTeX, why do fontspec and setmonofont versus sourcecodepro differ in Unicode support

fontsfontspecunicodeverbatim

I am trying to deal with an issue with rendering unicode characters (including combining characters) inside a verbatim environment. I am using xetex as my compilation engine.
(For more context see this github issue).

I have found that the default lmtt does not seem to support β, , or β̂ if they are encoded literally in a verbatim block.

I have discovered that Source Code Pro has support for these characters, but it seems to only be accessible from certain manners of loading it.

In particular, if I try to use

\usepackage{sourcecodepro}

it successfully displays the characters, though the combining circumflex above the β fails to align properly.

Because I'm already using fontspec to get access to other unicode supporting fonts elsewhere, I'd prefer to use fontspec's utilities directly. But I find that when instead I use:

\usepackage{fontspec}
\setmonofont{Source Code Pro}

β's do not display (though the combining circumflexes do display).

MWEs:

SourcecodePro

\documentclass[11pt]{article}
\usepackage{sourcecodepro}
\begin{document}
\begin{verbatim}
β 
â 
β̂
\end{verbatim}
\end{document}

fontspec + setmonofont

\documentclass[11pt]{article}
\usepackage{fontspec}
\setmonofont{Source Code Pro}
\begin{document}
\begin{verbatim}
β 
â 
β̂
\end{verbatim}
\end{document}

Any insight as to why this occurs would be greatly appreciated.

Image for illustration purposes:
Different rendering behaviour depending on which is loaded

Per request: List files from testme1.tex (on the left)

article.cls 2014/09/29 v1.4h Standard LaTeX document class
size11.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
fontspec.sty 2016/02/01 v2.5a Font selection for XeLaTeX and LuaLaTeX
expl3.sty 2016/05/18 v6512 L3 programming layer (loader)
expl3-code.tex 2016/05/18 v6512 L3 programming layer
l3xdvipdfmx.def
xparse.sty 2016/05/18 v6512 L3 Experimental document command parser
fontspec-xetex.sty 2016/02/01 v2.5a Font selection for XeLaTeX and LuaLaTeX
fontenc.sty
eu1enc.def 2010/05/27 v0.1h Experimental Unicode font encodings
eu1lmr.fd 2009/10/30 v1.6 Font defs for Latin Modern
xunicode.sty 2011/09/09 v0.981 provides access to latin accents and many oth
er characters in Unicode lower plane
eu1lmss.fd 2009/10/30 v1.6 Font defs for Latin Modern
graphicx.sty 2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
keyval.sty 2014/10/28 v1.15 key=value parser (DPC)
graphics.sty 2016/05/09 v1.0r Standard LaTeX Graphics (DPC,SPQR)
trig.sty 2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg 2016/01/02 v1.10 sample graphics configuration
xetex.def 2016/04/06 v4.08 LaTeX color/graphics driver for XeTeX (TeX Liv
e/RRM/JK)
infwarerr.sty 2016/05/16 v1.4 Providing info/warning/error messages (HO)
ltxcmds.sty 2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
fontspec.cfg
t3cmr.fd 2001/12/31 TIPA font definitions

List files from testme2.tex (on the right)

article.cls 2014/09/29 v1.4h Standard LaTeX document class
size11.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
sourcecodepro.sty 2015/10/09 v2.6 Adobe's Source Code Pro typeface
ifxetex.sty 2010/09/12 v0.6 Provides ifxetex conditional
ifluatex.sty 2016/05/16 v1.4 Provides the ifluatex switch (HO)
xkeyval.sty 2014/12/03 v2.7a package option processing (HA)
xkeyval.tex 2014/12/03 v2.7a key=value parser (HA)
fontspec.sty 2016/02/01 v2.5a Font selection for XeLaTeX and LuaLaTeX
expl3.sty 2016/05/18 v6512 L3 programming layer (loader)
expl3-code.tex 2016/05/18 v6512 L3 programming layer
l3xdvipdfmx.def
xparse.sty 2016/05/18 v6512 L3 Experimental document command parser
fontspec-xetex.sty 2016/02/01 v2.5a Font selection for XeLaTeX and LuaLaTeX
fontenc.sty
eu1enc.def 2010/05/27 v0.1h Experimental Unicode font encodings
eu1lmr.fd 2009/10/30 v1.6 Font defs for Latin Modern
xunicode.sty 2011/09/09 v0.981 provides access to latin accents and many oth
er characters in Unicode lower plane
eu1lmss.fd 2009/10/30 v1.6 Font defs for Latin Modern
graphicx.sty 2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty 2016/05/09 v1.0r Standard LaTeX Graphics (DPC,SPQR)
trig.sty 2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg 2016/01/02 v1.10 sample graphics configuration
xetex.def 2016/04/06 v4.08 LaTeX color/graphics driver for XeTeX (TeX Liv
e/RRM/JK)
infwarerr.sty 2016/05/16 v1.4 Providing info/warning/error messages (HO)
ltxcmds.sty 2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
fontspec.cfg
t3cmr.fd 2001/12/31 TIPA font definitions

Best Answer

@UlrikeFischer In a comment helped identify the solution.

The issue arises if you have a separately installed Source Code Pro (from Adobe Type Manager) as well as the Source Code Pro that you also have as part of the TeX distribution.

If you declare the font using \setmonofont and Source Code Pro it will find the Source Code Pro that is installed from the Adobe Type Manager, which appears to have inferior Unicode support in contrast to the TeX-included version.

If you want to explicitly specify the TeX version of Source Code Pro using fontspec, you will need to instead use the following declaration:

\usepackage{fontspec}
\setmonofont{SourceCodePro-Regular.otf}

If you don't include the .otf it will attempt to load the ttf which may produce other problems.

However, that won't give you the same behaviour as just using \usepackage{sourcecodepro}.

The problem is that with that command, you only get the upright version of the font, meaning italics, bold and bold italics will not work.

If you want instead to replicate that font family coverage you would need to use:

\usepackage{fontspec}
\setmonofont[Extension=.otf,UprightFont =*-Regular,ItalicFont =*-RegularIt,
BoldFont=*-Bold,BoldItalicFont=*-BoldIt]{SourceCodePro}

where the star fills in with the value of the font, and the extension is specified by the Extension option.

If you wanted to fully replicate the behaviour of sourcecodepro (as included by default) you would need to specify a few more options:

\usepackage{fontspec}
\setmonofont[Ligatures = TeX,Numbers =, Scale = 1,Extension = .otf, 
WordSpace = {1,0,0}, PunctuationSpace = WordSpace, UprightFont =*-
Regular,ItalicFont =*-RegularIt, BoldFont=*-Bold,BoldItalicFont=*-BoldIt] 
{SourceCodePro}

which will then fully recreate the behaviour of \usepackage{sourcecodepro}.

If you are running into a similar problem for a different font, use the \XeTeXtracingfonts=1 and \listfiles commands in your document, and look at your *.log file associated with compiling your document.

A related helpful debugging trick, if you want to see what font is actually being used at any point in your document, you can use a trick from test current font:

\makeatletter
\newcommand{\showfont}{encoding: \f@encoding{},
  family: \f@family{},
  series: \f@series{},
  shape: \f@shape{},
  size: \f@size{}
}
\makeatother

and then wherever you want to see the current font, you include the command \showfont. It will display the font information in the document at that point.

NB: this doesn't address the fact that the combining character is not correctly combining with β but it is correctly combining with a. But, that is for a different question.