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 offontspec
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 thefontenc
package (either use one or the other, not both, otherwise things might get rather funny. Whatfontspec
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:
Giving you:
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: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:
Giving you: