5

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?

  • Put \sffamily prior to \begin{tikzpicture} – Steven B. Segletes Jul 6 '16 at 11:37
  • 1
    off-toppic ... \tikzstyle is depreciated, instead it use \tikzset – Zarko Jul 6 '16 at 11:40
  • @Zarko thank you, this is my first tikz graphic and I am simply using whatever advice I can find on the Internet, that's where I saw \tikzstyle. – rumtscho Jul 6 '16 at 11:43
6

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

  • Thanks - I didn't realize I can use two assignments in the same \font= option. I will use the second one, as the first one changes it for the rest of the document and enclosing the graphic in \textsf{} broke the whole thing. – rumtscho Jul 6 '16 at 11:42
  • 1
    @rumtscho You can limit the scope of the \sffamily by enclosing the affected code in braces {}, as I edited to show in the first case. – Steven B. Segletes Jul 6 '16 at 11:43
4

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

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.