How do I make a TikZ graphic with small sans serif font?

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


Option font and node font can be combined:

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

Also, an option add to font can be defined to put additional font settings to the current setting of option font as shown in the following example, which includes the code from above:

\documentclass{article}
\usepackage{tikz}

\makeatletter
% Definition of option "font" in "tikz.code.tex":
%   \tikzoption{font}{\def\tikz@textfont{#1}}
% New TikZ option "add to font":
\tikzoption{add to font}{%
  \providecommand*{\tikz@textfont}{}%
  \expandafter\def\expandafter\tikz@textfont\expandafter{%
    \tikz@textfont
    #1%
  }%
}
\makeatother

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

\begin{tikzpicture}[font=\sffamily]
  \draw[add to font=\small]
    (0,0) rectangle (4, 2) node[midway] {I am a rectangle};
\end{tikzpicture}
\end{document}

Result