[Tex/LaTex] XeLaTeX Fontspec: restore original fonts

fontspecxetex

I'm trying to use a TTF font for my code excerpts, but want to use the standard latex fonts for the rest of the document. I'm using the following code, as recommended by this question.

\usepackage{fontspec}
\newfontfamily\codefont{Andale Mono}
\usepackage[OT1]{fontenc}

\renewcommand\rmdefault{lmr}
\renewcommand\sfdefault{lmss}
\renewcommand\ttdefault{lmtt}

This approach of putting the fonts back to normal to work okay, as the rendered fonts are all correct, but the document has lost all bold and italic formatting. How do I restore these?

Many thanks.

Best Answer

The main difference between your requirements and the post you pointed at is that they changed the main font of the document with the \setmainfont, \setsansfont and \setmonofont in their preamble and they then want to redefine these fonts mid document. It is indeed these three commands that define the document font not simply the use of fontspec

Your requirements, if I understand them correctly, are to have use the normal font throughout the document except in a few places where you want to use the TrueType font.

What fontspec does, broadly, is facilitate the use of fonts within LaTeX using a new engine and compatible with XeTeX and LuaTeX. Fontspec also replaces the fontenc package (either use one or the other, not both, otherwise things might get rather funny. What fontspec also does it use by default OTF fonts instead of Type1 fonts and will default to the Latin Modern set which is basically the OTF equivalent of the Computer Modern type1 fonts.

From the information you have given it is hard to know exactly what you are after. Andale Mono is a monospaced font so one could interpret your question as "how do I replace the mono font?". In which case something like this should work for you:

\documentclass[12pt,preview]{standalone}
\usepackage{fontspec}

%\setmainfont{Latin Modern Roman} %this is not needed as this should be the default
%\setsansfont{Latin Modern Sans}
\setmonofont{Andale Mono}

\begin{document}

This text using the {\bfseries Latin} {\sffamily Modern} {\itshape Font}

{\ttfamily This text is using the \textbf{Andale} \textit{Mono} font}

The is now back to the latin Modern Font

\end{document} 

Giving you:

enter image description here

note that the Andale Mono font does not come with the bold and italic glyphs so it reverts back to the normal glyphs instead. However fontspec allows you to fake this using options to the font definition command:

\setmonofont[AutoFakeBold,AutoFakeSlant]{Andale Mono}

enter image description here

If on the other hand you wish to keep the default fonts (roman, sans and mono) to the default and add a paragraph or something in Andale Mono, then the approach is slightly different and you need to declare a new font family as your sample code does:

\documentclass[12pt,preview]{standalone}
\usepackage{fontspec}

\newfontfamily\codefamily[AutoFakeBold,AutoFakeSlant]{Andale Mono}
\DeclareTextFontCommand{\codefont}{\codefamily}

\begin{document}

This text using {\ttfamily the} {\bfseries Latin} {\sffamily Modern} {\itshape Font}

{\codefamily This text is using the \textbf{Andale} \textit{Mono} font}

The is now back to the latin Modern Font

\end{document}

Giving you:

enter image description here