Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
Using \usepackage[OT1]{fontenc} is very dubious. Use only the first two lines of your code snippet. If this doesn't give you want you want: Make a small and complete example that demonstrates your problem. –  Ulrike Fischer Jun 6 '12 at 10:31
    
There is no need to do that: the default fonts used by fontspec are precisely the Latin Modern fonts (in OTF format, of course, but they are essentially identical to the Type1 format fonts). –  egreg Jun 6 '12 at 11:05

1 Answer 1

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

share|improve this answer
    
Hi, thanks for the quick reply. I've tried your code, but I find that as soon as I add \usepackage{fontspec} I get errors such as: ! Font EU1/lmr/m/n/10=[lmroman10-regular]:mapping=tex-text at 10.0pt not loadable: Metric (TFM) file or installed font not found. and the non-TTF text goes blank in the output PDF. Any thoughts? –  Tom Howe Jun 6 '12 at 20:40
    
which environment are your working on (OS, tex distribution and version...)? –  ArTourter Jun 6 '12 at 22:12
    
I'm on Mac OS X Lion, running xelatex [reports as XeTeX, Version 3.1415926-2.3-0.9997.5 (TeX Live 2011)] –  Tom Howe Jun 6 '12 at 23:16
1  
It really looks like your installation is incomplete. The otf version of the Latin Modern fonts should be located in /usr/local/texlive/2011/texmf-dist/fonts/opentype/public/lm/. If it isn't there I would suggest you install the lm package from the TeX Live Utility. As a general rule I would advise you to install the complete TeXLive distribution. –  ArTourter Jun 7 '12 at 0:11
1  
You're correct. A simple tlmgr install lm solved this. Thanks! –  Tom Howe Jun 7 '12 at 18:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.