Change default font within all tikzpicture environments

I want to have all tikz graphics in \sffamily font. However, I cannot set a base font for tikz, as it will be overridden, whenever I user, e.g., font=\itshape, for a node locally.


I'm simply looking for a proper way to set the base font in all tikz environments to \sfdefault.

In the following MWE, the first node should be sans, oblique, the other two nodes should be similar:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [font=\sffamily\itshape] {I should be in sans italic};
\node [font=\itshape,below] {I should be in sans italic};
\node [font=\bfseries,above] {I should be in sans oblique};
\end{tikzpicture}
\end{document}

• \tikzset{nodes={font=\sffamily}} Mar 21, 2017 at 13:58
• @Symbol1 That won't work. If you subsequently say \node [font=\itshape] you'll get italic serif. The OP wants the result to be italic sans (probably substituted as oblique sans, but that's a different matter).
– cfr
Mar 22, 2017 at 2:20
• execute at picture begin returns an error, so I don't know where that is from or how you defined it.
– cfr
Mar 22, 2017 at 2:27
• @cfr There is still a chance: node font. Otherwise one can do \tikzset{add font/.code={\preto\tikz@textfont{#1}}}. Mar 22, 2017 at 2:38
• @Symbol1 I didn't even know there was a node font. Thanks.
– cfr
Mar 22, 2017 at 2:50

Here is a simple solution (if you don't already use every picture style). Add the following line in your preamble:

\tikzset{every picture/.style={/utils/exec={\sffamily}}}


Example:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\tikzset{every picture/.style={/utils/exec={\sffamily}}}
\begin{document}
\begin{tikzpicture}
\node [font=\sffamily\itshape] {I should be in sans italic or sans oblique};
\node [font=\itshape,below] {I should be in sans italic or sans oblique};
\end{tikzpicture}
\end{document}

• What is /utils/exec? Thank you. Dec 11, 2017 at 12:13
• @codeaviator /utils/exec is a key that simply executes the given code (see p.893, pgfmanual, v3.0.1a). Dec 11, 2017 at 16:01

This changes the definition of the TikZ font option and adds an additional base font option. It is (almost) completely untested. It is probably extremely fragile. It may cause your cat to do as you say or otherwise destroy the world as you know it.

Caveat emptor

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\makeatletter
\tikzoption{base font}{\def\tikz@base@textfont{#1}}
\tikzoption{font}{\def\tikz@textfont{\tikz@base@textfont#1}}
\tikzset{
base font=\sffamily,
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node [font=\sffamily\itshape] {I should be in sans italic or sans oblique};
\node [font=\itshape, below] {I should be in sans italic or sans oblique};
\end{tikzpicture}
\end{document}