# True Type Fonts in LaTeX: a brief guide

One of the things I love about $\LaTeX$ is how customisable it is.  Separating content from design a long time before web design cottoned on to this.  However, out of the box, $\LaTeX$ comes with very limited fonts and most people just use these defaults, mainly because setting up other fonts isn’t as easy as it should be.

One of the great things about drawing diagrams in $\LaTeX$ is that the fonts match, it’s always a little jarring to my eye when I see papers with a mismatch between diagrams and main text.  However, sometimes you just can’t control what’s in your diagram or you want something a little more modern than Times New Roman for whatever you’re putting together.

So how do you go about doing this?  Like most things, the answer is “it depends”… let’s start with an assumption that you’re starting from scratch and if you’re already a few steps down the process then that’s just less work for you to do 🙂

1. You’ve downloaded your ttf and saved it to your OS font folder (i.e. in Ubuntu 16.04  this is /usr/share/fonts/truetype) and you can see it’s available in the OS.  You may need to enable the font (again in Ubuntu 16.04 either open in font viewer and install or run sudo fc-cache -fv to make it available)1.
2. If you’re using XeLaTeX or LuaTex then you can call system wide fonts 2 directly using the fontspec package3.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Arial}
\begin{document}
Test text....
\end{document}


If you can use this then you’re good to go…. if not then we need to try some other things… 4

3. Copy your ttf file to somewhere where you can play with it (i.e. a subdirectory of home) rather than changing the original file in situ5. Rename the file so that it is all lowercase and doesn’t include any non-alpha characters.  $\LaTeX$ doesn’t play well with modern ways of describing files so keep everything in 8.3 and you’ll have a much easier ride 😉
4. Get the encoder relevant to the ttf you’re using.  As a rule of thumb, T1-WGL4.enc is what you’ll need and is available from the link on the filename6.
5. Check ttf2tfm is installed on your system, which should have been included in your TexLive install.  If not, update your setup as it no longer appears to be available separately.
6. Make the tfm:
ttf2tfm myfont.ttf -p T1-WGL4.enc

This will give you a “myfont.tfm” output.

7. You’re not done yet 😉  Create a font definition file.  Pdflatex needs this to render the font correctly and without this you will get warnings.  Create a file called “t1myfont.fd” and put the following inside it:
\ProvidesFile{t1myfont.fd}[My NewFont font]
\DeclareFontFamily{T1}{myfont}{}
\DeclareFontShape{T1}{myfont}{m}{n}{  myfont}{}
\DeclareFontShape{T1}{myfont}{bx}{n}{ssub * myfont/m/n}{}
\DeclareFontShape{T1}{myfont}{m}{it}{ssub * ptm/m/it}{}
\DeclareFontShape{T1}{myfont}{m}{sl}{ssub * ptm/m/sl}{}
\DeclareFontShape{T1}{myfont}{m}{sc}{ssub * ptm/m/sc}{}
\DeclareFontShape{T1}{myfont}{bx}{it}{ssub * ptm/b/it}{}
\DeclareFontShape{T1}{myfont}{bx}{sl}{ssub * ptm/b/sl}{}
\DeclareFontShape{T1}{myfont}{bx}{sc}{ssub * ptm/b/sc}{}
\pdfmapline{+myfont\space <myfont.ttf\space <T1-WGL4.enc}

8. Assuming all your files are now in the same directory, make a sample $\LaTeX$ file:
% File 'test.tex'
\documentclass{article}
\renewcommand{\encodingdefault}{T1}
\renewcommand{\rmdefault}{myfont}
\renewcommand{\sfdefault}{phv}
\renewcommand{\ttdefault}{pcr}
\title{MyFont test}
\begin{document}
\maketitle
Testing that my font displays as expected
\end{document}

9. If this works then you’re good to go.  If not, then you may need to check the troubleshooting steps below, or the excellent detailed guide by Damir Rakityansky.

Naming is very important, the font system isn’t very tolerant of deviation.  Make sure your ttf and fd files are all lowercase with no special characters (underscores, hyphens, spaces etc) or you will spend far too long chasing error messages that don’t relate to the problem.

For the process above to work, you need the ttf, tfm, fd and enc files all available.  The easiest way to test this is to set a local font folder and continue until it’s working.  It’s neater to get everything in the default font directory but if you’re submitting your code to a repository to share with others you may want to keep it as a subfolder of the document tree.  This way you can guarantee that any other users will pick it up7.

If you treat $\LaTeX$ like any other programming language, then debugging is just the same: simplify as far as possible to isolate the problem.

1. You could also save this in your hidden .font/ directory locally, but stuff in there has a habit of being forgotten and when it’s not accessible later on, it will be annoying to find the cause of the problem.
2. And anything you put in your ~/.fonts/ directory
3.   Thanks to Caramdir on Stackexchange for this
4.   There are many reasons why both XeLaTeX and LuaTex may not be suitable, you’ll know whether it’s appropriate better than I will.
5.   There’s a special circle of hell reserved for people who make changes in default system locations just because an online guide tells them – it’s a bad habit, don’t do it.
6.  I’m not knowledgeable in fonts to know when to use the other encoders, so if this doesn’t work for the font you have, then fire up Google and get your answer from the hive mind.
7. Of course, if you’re sharing a paid for font, make sure it’s only within an organisation that has rights to use it and never make it freely available without the permission of the rights holder – this is why a lot of the nice modern fonts just aren’t available to $\LaTeX$ but are in the OS.
Dr Janet is a Molecular Biochemistry graduate from Oxford University with a doctorate in Computational Neuroscience from Sussex. I’m currently studying for a third degree in Mathematics with Open University.

During the day, and sometimes out of hours, I work as a Chief Science Officer. You can read all about that on my LinkedIn page.