5

Preface:

An axis plotted with is usually using the current math font for its ticklabels. But if they are overwritten by e.g. xticklabels={-8,-6,...,8} then the current text font is used. Also an axis label ylabel={Y} would be in text font unless put explicitly in math mode ylabel={$Y$}. However it would be nice, if one hadn't to do so.

Question:

I'm aiming to ensure that pgfplots always uses the current math font. But please assume that the classic font commands like \rmfamily are screwed up by the corporate design I'm supposed to use, e.g. assume: \renewcommand{\rmdefault}{\sfdefault}

It works if I explicitly define a font

\newcommand{\pgfmathfont}{\fontfamily{pxfonts}\selectfont}

and pass it to pgfplots together with assume math mode=true:

\begin{axis}[
font = \pgfmathfont, 
tick label style={/pgf/number format/assume math mode=true}
]

But it requires that I insert the name of the current math font. How can I do that automatically? How to get the currently used math font to insert it into the macro?


MWE

\documentclass{beamer}

\usetheme{Warsaw}
\usefonttheme{professionalfonts}
\usepackage{pxfonts}
\usepackage{pgfplots}

% screwed up by Corporate Design
\renewcommand{\rmdefault}{\sfdefault}

% I'd like this line to be generic!
\newcommand{\pgfmathfont}{\fontfamily{pxfonts}\selectfont}
%           put current math font here   X

\begin{document}

\begin{frame}

This is a basic equation:   
\[ x + y = 42\]

\begin{tikzpicture}
    \begin{axis}[
%   font = \rmfamily, % makes no sense here
    font = \pgfmathfont, 
    xlabel={$x$-label: I want to be in math font!},
    ylabel={Y},
    xtick={-4,-3,...,4},
    xticklabels={-8,-6,...,8},
%   xticklabels={$-8$,$-6$,...,$8$}, % does not work
    tick label style={/pgf/number format/assume math mode=true}
    ]

    \addplot {x^2};

    \end{axis}
\end{tikzpicture}

\end{frame}

\end{document}

Everything related to is no option at all!

Thank you!

  • 2
    Well it can't be automatically in math mode for a good reason because labels are 99% of the time plain text. When you have the explicit labels you can xticklabels={$-8$,$-6$,$...$,$8$} to make it work. (notice the dollar signs around the ellipsis...) – percusse Dec 4 '15 at 8:10
  • This is a nice hint -> $...$! Well I'm aware that if in my example above probably the text shape of pfxonts is used and not the math shape. But in my above example pgfplots uses pxfonts by default, until the labels are overwritten, then it changes to the beamer font used for text. So in this particular case it should be possible to determine what font is loaded (if not pxfonts) which then is used by pgfplots and should be used also in the ` font = ... ` key? – thewaywewalk Dec 4 '15 at 8:43
  • Does this answer tex.stackexchange.com/a/14378/69445 help you? – cryingshadow Dec 4 '15 at 10:07
  • @cryingshadow It looks promision, but then I'd expect something like \newcommand{\pgfmathfont}{\fontfamily{\the\font}\selectfont} to work, but I get a Missing \endcsname inserted. \end{frame} error. Any idea on that? Coudl you may try make it an answer for the MWE? – thewaywewalk Dec 4 '15 at 11:09
  • @thewaywewalk Sorry, I also don't know why this does not work. However, you already have good answers. ;) – cryingshadow Dec 4 '15 at 13:55
5

The ticklabels could be calculated from the ticks using

xticklabel={\pgfmathparse{2*\tick}\pgfmathprintnumber{\pgfmathresult}}

But if you really want to use tick label style={/pgf/number format/assume math mode=true} you have to surround \pgfmathprintnumber{} by $...$. Note that assume math mode=true should only be used if you are sure that math mode is active. From the pgfmanual: " ... the final number is typeset as-is, no further checking is performed."

xticklabel={\pgfmathparse{2*\tick}$\pgfmathprintnumber{\pgfmathresult}$},
tick label style={/pgf/number format/assume math mode=true}

enter image description here

\documentclass{beamer}
\usetheme{Warsaw}
\usefonttheme{professionalfonts}
\usepackage{newpxtext,newpxmath}% as suggested by @egreg
\usepackage{pgfplots}

% screwed up by Corporate Design
\renewcommand{\rmdefault}{\sfdefault}

\begin{document}
\begin{frame}
This is a basic equation:   
\[ x + y = 42\]
\begin{tikzpicture}
    \begin{axis}[
    font = \rmfamily,
    xlabel={$x$-label: I want to be in math font!},
    ylabel={Y},
    xtick={-4,-3,...,4},
    xticklabel={\pgfmathparse{2*\tick}\pgfmathprintnumber{\pgfmathresult}},
    ]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

Another possibility is

xticklabels={$-8$,$-6$,$...$,$8$}

as suggested by @percusse in a comment to your question. Note that $...$ is used.


If the tick labels can not be calculated from the tick values you can define an array

\def\myxticklabels{{1.9, 7, 42, 5, 5, 6, 8, 5, 0.720}}

and use

xticklabel={\pgfmathparse{\myxticklabels[\ticknum]}\pgfmathprintnumber{\pgfmathresult}},

enter image description here

Code:

\documentclass{beamer}
\usetheme{Warsaw}
\usefonttheme{professionalfonts}
\usepackage{newpxtext,newpxmath}% as suggested by @egreg
\usepackage{pgfplots}

% screwed up by Corporate Design
\renewcommand{\rmdefault}{\sfdefault}

\begin{document}
\begin{frame}
This is a basic equation:
\[ x + y = 42\]
\begin{tikzpicture}
    \def\myxticklabels{{1.9, 7, 42, 5, 5, 6, 8, 5, 0.720}}
    \begin{axis}[
    font = \rmfamily,
    xlabel={$x$-label: I want to be in math font!},
    ylabel={Y},
    xtick={-4,-3,...,4},
    xticklabel={\pgfmathparse{\myxticklabels[\ticknum]}\pgfmathprintnumber{\pgfmathresult}},
    /pgf/number format/use comma
    ]
    \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
  • Great solution If you have a constant factor to multiply the ticks with, should work in most of my cases! But would it be also a possible to pass an array to \pgfmathparse ? e.g: \pgfmathparse{ {1, 7, 42, 5} } – thewaywewalk Dec 4 '15 at 12:04
  • 1
    @thewaywalk See my updated answer. – esdd Dec 4 '15 at 12:43
  • I finally used your approach as it takes \pgfkeys{/pgf/number format/.cd,use comma} into account, but put it into the \pgfplotsset as suggested by egreg. Thank you! – thewaywewalk Dec 6 '15 at 20:55
6

Define a new style:

\documentclass{beamer}

\usetheme{Warsaw}
\usefonttheme{professionalfonts}
\usepackage{newpxtext,newpxmath}
\usepackage{pgfplots}

% screwed up by Corporate Design
\renewcommand{\rmdefault}{\sfdefault}

\pgfplotsset{
  typeset ticklabels as math/.style={
    /pgfplots/typeset ticklabel/.code={$##1$},
  },
}


\begin{document}

\begin{frame}

This is a basic equation:   
\[ x + y = -8\]

\begin{tikzpicture}
    \begin{axis}[
    xlabel={$x$-label: I want to be in math font!},
    ylabel={Y},
    xtick={-4,-3,...,4},
    xticklabels={-8,-6,...,8},
    typeset ticklabels as math,
    tick label style={/pgf/number format/assume math mode=true}
    ]

    \addplot {x^2};

    \end{axis}
\end{tikzpicture}

\end{frame}

\end{document}

enter image description here

Note that the combination newpxtext with newpxmath should be preferred to pxfonts, because it cures some drawbacks from the unmaintained older package.

  • ìt is a nice solution for the ticklabels. But for the x- and y-labels it would still require some quirky things like xlabel={$t_0 ~\text{in}~ \si{\second}$} . or in case you could define similary typeset labels as math one would need xlabel={t ~\text{in}~ \si{\second}} . But the aim would be to use juse xlabel={$t_0$ in \si{\second}} – thewaywewalk Dec 4 '15 at 11:14
  • I finally used a combination of both answers, your idea with the new style, but the ticklabel/.code={...} filled with esdd's answer as it takes \pgfkeys{/pgf/number format/.cd,use comma} into account. Thank you! – thewaywewalk Dec 6 '15 at 20:58
  • Very elegant answer ! It seems this style option typeset ticklabel/.code={} is not in the pgfplots manual. Do you confirm ? Where did you find it if you do not mind ? – BambOo Nov 14 '18 at 8:41
  • @BambOo I don't remember. – egreg Nov 14 '18 at 9:09

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.