[Tex/LaTex] How to make a TikZ graphic with small sans serif font

fontsizesans-seriftikz-pgf

I want the font in the graphic to be sans serif, and to be smaller than the main document. I tried

\begin{tikzpicture}[font=\sffamily]
 \tikzstyle{every node}=[font=\small]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle}
\end{tikzpicture}

The problem is that the font=\small option within the figure overrides the \font=\sffamily option from the picture declaration and I end up with a small serif font. How do I set both the font size and font family at once?

Best Answer

Two ways:

  1. Invoke \sffamily before entering tikz, or

  2. You can combine \small and \sffamily in the same font= directive.

Here is the MWE

\documentclass{article}
\usepackage{tikz}
\begin{document}
{\sffamily
\begin{tikzpicture}
 \tikzset{every node}=[font=\small]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle};
\end{tikzpicture}}

No longer in sffamily

\begin{tikzpicture}[font=\sffamily]
 \tikzset{every node}=[font=\small\sffamily]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle};
\end{tikzpicture}
\end{document}

enter image description here

Related Question