[Tex/LaTex] Using sans font for tick labels in pgfplots

pgfplotssans-serifunicode-math

I am using a sans font for my figures. Hence I also want the tick labels of the pgfplots to be in sans. The tick labels, however, are set in a math enviroment. So changing the text font does not change anythings here. I have seen a possible solution here on tex.SE. The problem is that get many error messages like that:

! LaTeX Error: Symbol font `um_fam1' not defined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.23        \end{axis}

I think the problem is that a non-math font is used with unicode-math, which is not the intended use case for unicode-math. I really tried many ways to change the math font to sans, see for example here, but the essence is that a sans math font is not possible.

Since it is not possible to use a sans math font, maybe it is possible to change the pgfplots to use not an mathematical enviroment. I'm asking here, if it is possible to patch pgfplots to use an non mathematical enviroment for the tick labels? I understand this enviromnent is needed for fractions or some log scales. But I actually don't use those. So I'm fine with a solution ignoring those cases. The only important characters are digits and commas.

% !TeX program = lualatex
\documentclass{article}

\usepackage{siunitx}
\sisetup{locale = DE}

\usepackage{unicode-math}
\setmainfont[Ligatures=TeX, Numbers=OldStyle]{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
% In reality I do not use Adventor but another sans font
\setsansfont{TeX Gyre Adventor}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\SendSettingsToPgf

\begin{document}
    \begin{tikzpicture}[font=\sffamily]
        \begin{axis}[xlabel=x direction, ylabel=y direction, xmin=-1, xmax=1,domain=-1:1]
            \addplot {x^2};
        \end{axis}
    \end{tikzpicture}

    \textsf{1,2,3,4,5,6,7,8,9,0}
\end{document}

enter image description here


Best Answer

This can be achieved using a setting in the tick label style key. By default (without getting into too much details), pgf/pgfplots uses some "\ensuremath-like" business internally when typesetting the tick labels. We can disable this by setting tick label style={/pgf/number format/assume math mode=true}. What this does is tells pgf to assume that the input is already in math mode (though it is not in this case) and that the "\ensuremath-like" function is not required. So the numbers are passed in directly and typeset in the specified \sffamily.

Code

% !TeX program = lualatex
\documentclass{article}

\usepackage{siunitx}
\sisetup{locale = DE}

\usepackage{unicode-math}
\setmainfont[Ligatures=TeX, Numbers=OldStyle]{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
% In reality I do not use Adventor but another sans font
\setsansfont{TeX Gyre Adventor}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\SendSettingsToPgf

\begin{document}
\begin{tikzpicture}[font=\sffamily]
\begin{axis}[%
  xlabel=x direction,
  ylabel=y direction,
  xmin=-1, xmax=1,
  domain=-1:1,
  tick label style={/pgf/number format/assume math mode=true} % <=== added here
]
  \addplot {x^2};
\end{axis}
\end{tikzpicture}

\textsf{1,2,3,4,5,6,7,8,9,0}
\end{document}

Output

enter image description here

Related Question