6

In my document, I want all labels in tikz and pgfplots graphics to use sans-serif, so I use \tikzset{every node/.append style={font=\sffamily}}. This works. However, when I use this, inside my axis environment, legend style={font=\tiny} no longer has any effect. The document below produces an axis where the legend has the normal font size. How do I set all nodes to use sans-serif fonts, while still being able to tweak the font size for legends on a per-case basis?

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\tikzset{
    every node/.append style={font=\sffamily}}
\begin{document}
\begin{tikzpicture}%
\begin{axis}[
    legend style={font=\tiny}]
\addplot coordinates {(0,0) (1,1) (2,1) (3.5, -1)};
\addlegendentry{Test}
\end{axis}%
\end{tikzpicture}%
\end{document}

True effect (sans-serif fonts, but instruction on legend font size ineffective):

Figure does not use a tiny legend font

Effect with the font=\sffamily instruction disabled (serif fonts with tiny legend font size):

Figure does use a tiny legend font

0

2 Answers 2

4

Do you really need this for every node from every TikZ-picture? Your approach is quite harsh.

Please have a look on my MWE and see, if this is feasable for you.

Off-topic: You have been loading TikZ for three times. \documentclass[tikz]{standalone}, \usepackage{tikz}, and \usepackage{tikz-cd} which loads it as well.

% arara: pdflatex

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\pgfplotsset{%
    ,tick label style = {font=\sffamily} % see http://tex.stackexchange.com/a/33329 if also needed for numbers.
    ,every axis label = {font=\sffamily}
    ,legend style = {font=\tiny\sffamily}
    ,label style = {font=\sffamily}
}

\begin{document}
    \begin{tikzpicture}%
    \begin{axis}
    \addplot coordinates {(0,0) (1,1) (2,1) (3.5, -1)};
    \addlegendentry{Test}
    \end{axis}%
    \end{tikzpicture}%
\end{document}

The problem with your approach was that you have been using the option font twice. Once you use font=\sffamily and once font=\tiny. You are using the .append style macro, but this will append the whole option to your drawing. If an option is set twice, TikZ will take the last occurrence.

If you need to set all nodes in your document to \sffamily, you will have to define your own options such as fontsize= and fontfamily= (maybe already existing) or you define a real "font append" as you have been trying to do. This will look like the following (credits to Henry DeYoung):

% arara: pdflatex

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\makeatletter
\tikzset{%
    ,font append/.style={font/.expand once=\tikz@textfont #1}
    ,font append/.value required
    }
\makeatother
\tikzset{every node/.append style={font append=\sffamily}}

\begin{document}
    \begin{tikzpicture}%
    \begin{axis}[%
    legend style={font=\tiny}
    ]
    \addplot coordinates {(0,0) (1,1) (2,1) (3.5, -1)};
    \addlegendentry{Test}
    \end{axis}%
    \end{tikzpicture}%
\end{document}

Both MWEs result in:

enter image description here

4
  • That might work, but why does my approach not?
    – gerrit
    May 11, 2015 at 11:53
  • 1
    @gerrit In your approach \tiny\sffamily would work. By setting it to just \tiny you'Re overwriting the \sffamilycommand you used before.
    – Ronny
    May 11, 2015 at 11:59
  • @gerrit Please see my update. I hope, it gets clear. Ronny's suggestion is absolutely right. I just suggested that you do not want to set that manually for a second time.
    – LaRiFaRi
    May 11, 2015 at 12:02
  • In general is \sffamily preferred to be used in tikzpictures?
    – alper
    Dec 24, 2022 at 23:47
1

The legend is set as a matrix. So you could change the style of the cell nodes using nodes={...}.

You could define a new command

\newcommand\mynodefont{\sffamily}

and use this command for all nodes

\tikzset{every node/.append style={font=\mynodefont}}

and inside the nodes key for the legend:

legend style={nodes={font=\mynodefont\tiny}}

enter image description here

Code:

\documentclass[margin=10pt]{standalone}
\newcommand\mynodefont{\sffamily}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\tikzset{every node/.append style={font=\mynodefont}}

\begin{document}
\begin{tikzpicture}%
\begin{axis}[
    legend style={nodes={font=\mynodefont\tiny}}
    ]
\addplot coordinates {(0,0) (1,1) (2,1) (3.5, -1)};
\addlegendentry{Test}
\end{axis}%
\end{tikzpicture}%
\end{document}   

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .