[Tex/LaTex] Font in tikz figures

fontstikz-pgfxetex

I would like to use the same font that I'm using in the whole document (Gentium Plus) for any text in figures (at the moment I only have tikz-figures) in the document. So far the standard font is used there.

A minimal example that I'm compiling with xetex (I took the picture from texample.net and simplified it a bit):

\documentclass{scrbook}
\usepackage{fontspec}
\setmainfont[Renderer=ICU,Mapping=tex-text]{Gentium Plus}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{figure}

\begin{tikzpicture}
\begin{axis}[
    xlabel=$Example label$,
    ylabel=$Example label$]
\addplot plot coordinates {
    (0,2)
    (2,3)
    (3,1)
};
\addlegendentry{Example text}

\end{axis}
\end{tikzpicture}

\end{figure}
This is some example text in Gentium Plus. Looks quite cool, doesn't it?

\end{document}

I only found answers on how to use sans serif fonts in tikz pictures, but none seemed to asked my question before, although I think it should be easy to do.

Best Answer

You haven't set up a mathematics font, so LaTeX will use the default Computer Modern for mathematics.

By wrapping your label text in dollar signs ($), you are asking LaTeX to typeset them as mathematics, so Computer Modern Italic (with large letter spacing to indicate multiplication of variables) is used.

Solution for axis labels only

Removing the dollar signs from around your labels prints them in Gentium as desired. Also, \usepackage{tikz} is not needed since pgfplots loads tikz on its own. You might consider specifying a version for pgfplots (using \pgfplotsset{compat=<version>}) to obtain better label spacing and ensure future compatibility.

\documentclass{scrbook}
\usepackage{fontspec}
\setmainfont[Renderer=ICU,Mapping=tex-text]{Gentium Plus}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    xlabel=Example label,
    ylabel=Example label]
\addplot plot coordinates {
    (0,2)
    (2,3)
    (3,1)
};
\addlegendentry{Example text}

\end{axis}
\end{tikzpicture}
\end{figure}
This is some example text in Gentium Plus. Looks quite cool, doesn't it?

\end{document}

enter image description here

Note that the tick labels are still typeset in Computer Modern. This is because pgfplots uses math mode for these elements. Read on if you'd like to fix this... ;-)

Solution for axis labels and tick labels

Adding \pgfplotsset{/pgf/number format/assume math mode=true} to the preamble will cause pgfplots to use text mode for the tick labels:

\documentclass{scrbook}
\usepackage{fontspec}
\setmainfont[Renderer=ICU,Mapping=tex-text]{Gentium Plus}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\pgfplotsset{/pgf/number format/assume math mode=true}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    xlabel=Example label,
    ylabel=Example label]
\addplot plot coordinates {
    (0,2)
    (2,3)
    (3,1)
};
\addlegendentry{Example text}

\end{axis}
\end{tikzpicture}
\end{figure}
This is some example text in Gentium Plus. Looks quite cool, doesn't it?

But now, notice that mathematics in the document will be inconsistent with the text and plots:
\[ e=mc^2 \qquad 0123456789 \qquad \gamma \Theta \omega \]

\end{document}

\documentclass{scrbook}
\usepackage{mathspec}
\setmainfont[Renderer=ICU,Mapping=tex-text]{Gentium Plus}
\setmathfont(Digits,Latin,Greek){Gentium Plus}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    xlabel=Example label,
    ylabel=Example label]
\addplot plot coordinates {
    (0,2)
    (2,3)
    (3,1)
};
\addlegendentry{Example text}

\end{axis}
\end{tikzpicture}
\end{figure}
This is some example text in Gentium Plus. Looks quite cool, doesn't it?

And here is a test of mathematics: $e=mc^2$, $0123456789$, $\Theta$, $\gamma$.

\end{document}

enter image description here

Solution for mathematics font setup

If you don't have mathematics in your document, or don't care about the discrepancy highlighted above, you may skip this part.

The best results are obtained when using a font with true maths support, but the mathspec package (used in place of fontspec) allows one to "fake" a maths font using a given text font. Please understand that results will be much better with a true math font, but this solution is acceptable if you have only basic mathematics in the document:

\documentclass{scrbook}
\usepackage{mathspec} % note change here
\setmainfont[Renderer=ICU,Mapping=tex-text]{Gentium Plus}
\setmathfont(Digits,Latin,Greek)[Numbers={Lining,Proportional}]{Gentium Plus}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    xlabel=Example label,
    ylabel=Example label]
\addplot plot coordinates {
    (0,2)
    (2,3)
    (3,1)
};
\addlegendentry{Example text}

\end{axis}
\end{tikzpicture}
\end{figure}
This is some example text in Gentium Plus. Looks quite cool, doesn't it?

Notice that mathematics and tick labels in the document now match the text.
\[ e=mc^2 \qquad 0123456789 \qquad \gamma \Theta \omega \]

But some symbols are unavailable and fall back to Computer Modern:
\[ \approx \infty \]

\end{document}

enter image description here