12

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 tried to set a prefix such as \tikzset{font/.prefix = {\sffamily}}, as well as to change de default font using \AtBeginEnvironment{tikzpicture}{\renewcommand*{\familydefault}{\sfdefault}} and \AtEndEnvironment, respectively. Also, I tried \tikzset{execute at begin picture={\renewcommand*{\familydefault}{\sfdefault}}}. However, nothing helped.

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}} – Symbol 1 Mar 21 '17 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 '17 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 '17 at 2:27
  • 1
    @cfr There is still a chance: node font. Otherwise one can do \tikzset{add font/.code={\preto\tikz@textfont{#1}}}. – Symbol 1 Mar 22 '17 at 2:38
  • @Symbol1 I didn't even know there was a node font. Thanks. – cfr Mar 22 '17 at 2:50
12

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}
|improve this answer|||||
  • What is /utils/exec? Thank you. – codeaviator Dec 11 '17 at 12:13
  • 1
    @codeaviator /utils/exec is a key that simply executes the given code (see p.893, pgfmanual, v3.0.1a). – Paul Gaborit Dec 11 '17 at 16:01
3

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}

all sans

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.