# How to Use Google Fonts in LaTeX?

Hey guys!

Today's tutorial is about using google fonts in LaTeX and as a challenge we will create a beautiful cover page using TikZ. We will learn together how to:

• use a custom font in LaTeX,
• add a multiline text nodes,
• and change the font size.

A new story about text alignment is created, check it here: Text alignment story !

## Problem simplified

The cover page that we would like to create in TikZ can be simplified to the following small tasks:

• Add an image scaled to the page size as a background (section 2).
• Three text nodes with different font style and size
• Node 1 (TikZ tutorials) is a multiline node text positioned below the top of the page by 1cm. The top of the page has the coordinates (current page.north). Text is left aligned (section 4).
• Node 2 (How to change ...) is also a multiline node text. The node is positioned at the right of the center of the page. The latter has the coordinates (current page.center) (section 4).
• Node3 (@Latexdraw.com) is positioned at 6.5 cm in the x axis direction and 1.5cm in the y axis direction from the bottom left corner of the page. The latter has the coordinates (current page.south west) (section 4).

## Add a background image in TikZ

Let's start by addresing the question "how to set a background image in LaTeX using TikZ?" through these points:

### Inserting an image in LaTeX

Using graphicx package, images can be included in LaTeX by the mean of \includegrapics command. In our case, we would like the image to have the size of the page. This can be done by specifying the width and the height of the inserted image as follows:

\includegraphics[width=\paperwidth,height=\paperheight]{imageName}


But this is not sufficient as the image will be shifted by the page margins:

To remedy this issue, we will use TikZ to create a background layer that we can add text over it.

### Create a background layer in TikZ

In TikZ, when you would like to include an image, you need to put it inside a \node. The node position should be set with respect to the page (absolute positioning).

In this example, the node image with a page size is positioned at the center of the page:

\documentclass{article}

\begin{document}

\pagestyle{empty}

\begin{tikzpicture}[remember picture,overlay]

% Background Image
\node[inner sep=0pt] at (current page.center){
\includegraphics[
width=\paperwidth,
height=\paperheight
]{background.jpg}
};

\end{tikzpicture}

\end{document}

Absolute positioning

We have added the options $\verb|remember picture|$ and $\verb|overlay|$ to the tikzpicture environment in order to work on a lower layer of the current page. Here is the obtained results:

Background image in LaTeX

Now let's add details to the cover page and play around with google fonts.

## Use Custom fonts in LaTeX

In this part, we will learn step-by-step how to use custom fonts in LaTeX by the mean of fontspec package, which is dedicated to XeLaTeX/LuaLaTeX compilers.

### Step 1: Choose the font

The first step is to download your prefered font from Google fonts, if you do not have one. To highlight the ideas, let's consider the example of Pacifico font:

Once we download the font family, we get ttf files of different styles such as: thin 100, regular 400 and bold 700 italic. In this example, only one style is available which corresponds to the file " Pacifico-Regular.ttf ". Then, we upload this file to the current LaTeX project.

### Step 2: Declare it!

Using ttf (TrueType font) or otf (OpenType font) in LaTeX necessitates the fontspec package which is loaded as follows:

\usepackage{fontspec}


In this case, we would like to use this font at specific situations such as the title of the cover page and not as the main font of the document. This can be achieved by creating a new font family as follows:

\newfontfamily{\pacifico}{Pacifico-Regular.ttf}


which requires the command name to call the font and the font name that has to be the same as the uploaded file.

### Step 3: Use it!

To apply the font, we use the predefined command $\verb|{\pacifico your text here}|$. Here is an illustrative example:

\documentclass[border=0.2cm]{standalone}

\usepackage{fontspec}
\newfontfamily{\pacifico}{Pacifico-Regular.ttf}

\begin{document}

{\pacifico This is an example written using Pacifico regular font!}

\end{document}


which yields the following results:

It should be noted that XeLaTeX compiler is selected as the project's compiler. In overleaf, it can be chosen from Menu (top left) as follows:

## Add a multiline text nodes in TikZ

Adding a text at specific position (x,y) can be achieved by the mean of a \node command as follows:

\node at (x,y) {Text here!};


All the text between braces is put in one line. It may happen that we wish to add line breaks or just fix the text width. Three methods have been presented in the PGF/TikZ manual (pp. 234-235):

• The first method is about adding a standard environment that produces multi-line and aligned text (e.g. tabular environment) inside a node. This method is not considered in this tutorial.
• The second method is an automatic one which aims to fix the text width. Text alignment can be used to achieve the desired results.
• The third method is a manual one where we put \\ inside the text for line breaks

### Text width

Text width can be fixed by using the option $\verb|text width=|$ in the node command. Here is an example:

\node at (0,0) {Here is a piece of text!};


### Text alignment

Text can be aligned using the option \verb|align=| in the node command. Here is a full list of text alignment options: $\verb|left|$, $\verb|flush left|$, $\verb|right|$, $\verb|flush right|$, $\verb|center|$, $\verb|flush center|$ and $\verb|justify|$

To distinguish between these options, we used a new style of information presentation: Stories. Don't miss it, check it here: "Text alignment story"!

### Manual line breaks

Using \\ within a text yields to line break. However, this can be done only if \verb|align| option is ued. For an optional extra space between lines, we can use $\verb|\\[spaceValue]|$, it works also with negative values. Here is an illustrative example which combines the automatic method and the manual one:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node[
text width=3cm,
align=left
] at (0,0) {
This is a an example of text, left aligned.
\\ Here we added a line brea k
\\[1cm] Here we added a line brea k and a space of 1 centimeter};
\end{tikzpicture}

\end{document}


## Cover Page LaTeX code

Here is the final version of the cover page latex code:

\documentclass{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{fontspec}
\newfontfamily{\playfair}{PlayfairDisplay-Regular.ttf}
\newfontfamily{\robotoCondensed}{RobotoCondensed-Regular.ttf}

\begin{document}

\pagestyle{empty}

\begin{tikzpicture}[remember picture,overlay]

% Background Image
\node[inner sep=0pt] at (current page.center){
\includegraphics[
width=\paperwidth,
height=\paperheight
]{background}
};

% Node Text (TikZ Tutorials)
\begin{scope}
{\fontsize{90}{100} \playfair
\node[
below=1cm,
align=left] (Title1) at (current page.north){
\textcolor{black!90!cyan}{Ti\it{k}Z}
\\ \textcolor{Mahogany}{TUTORIALS}};
}
\end{scope}

% Node Text (Title)
{\fontsize{30}{36} \robotoCondensed
\node[
right,
align=left] (Title2) at (current page.center){
\textcolor{black!90!cyan}{How to change font}
\\ \textcolor{black!90!cyan}{style in \LaTeX ?}};
}

% Node Text (Author)
{ \fontsize{25}{30} \playfair
\node (Title3) at ($(current page.south west)+(6.5cm,1.5cm)$){ \textcolor{white}{@LaTeXdraw.com}};
}

\end{tikzpicture}

\end{document}


Don't forget to set XeLaTeX as a projects compiler (section 3).

We have reached the end of this tutorial, If you liked it ❤️ don't forget to rate it 👇👇👇!

Thanks!