5

We have seen in another question how to set the tick numbers on logarithmic axes in a sans serif font when the style is for 10^x numbering. Pgfplots offers additional options for numbering log ticks, specifically log ticks with fixed point and the option to shift from exponents to fixed point selectively depending on the magnitude of the number \pgfmathprintnumber[std={-2:2}, precision=1]{...}.

In the first instance, fixed point, the code below produces the following chart: enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}[log ticks with fixed point,]
 \addplot [domain=1:1000, ultra thick, color=blue] {x^1.5};
\end{loglogaxis}
\end{tikzpicture}

\end{document}

Question 1: how can we set these tick numbers in a sans serif font (\mathsf{..})?

In the second instance, we desire to use the functionality of \pgfmathprintnumber[std={-2:2}, precision=1]{...} to set the numbers beyond 100 in exponential format with the smaller numbers in fixed point, again in a sans font.

Question 2: How can we exploit std={x:y} behavior on log axes using san serif fonts? The desired result might look like the image below (obtained in this case by editing the pdf file directly)

enter image description here

6
  • You can use to adjust the style to the tick labels with \pgfplotsset{every tick label/.append style={...}}, see How do I change the font size of the axis tick labels in pgfplots?. Maybe helpful \mathsf{} prints digits in serif font
    – Bobyandbob
    Commented Nov 21, 2017 at 16:43
  • @Bobyandbob -- Thanks for this reply, however, I'm not sure it addresses the problem. The essential matter is that ticks are set in math mode and a require a modification such as `xticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},' for non-log axes as illustrated in the earlier question, but log ticks and their variants introduce some complications.
    – John
    Commented Nov 21, 2017 at 19:45
  • Does How can I change the font family in pgfplots? help?
    – Bobyandbob
    Commented Nov 21, 2017 at 20:25
  • 2
    I have previously looked at that package (and that post), but sansmath, which is from 2003, provides limited font support. In the end, I am not going to use cmss as my sans family. The package also seems to have some challenges in shifting between fonts, e.g., reverting to normal math and back. So, I am seeking a solution that will guide pgfplots to call the sans math font that is currently loaded under unicode-math (running LuaLaTex).
    – John
    Commented Nov 21, 2017 at 23:09
  • 1
    @John did you have any luck finding a sane way to make it use current sans font? it's really annoying in beamer to have a different font for plots
    – filippo
    Commented Mar 20, 2018 at 8:13

4 Answers 4

3

The comment from filippo prompted me to look at this again, and I have an answer to Question 1.

Fixed point, sans serif, log axis ticks can be achieved using by modification of the approach for 10n axes as follows:

 \begin{tikzpicture}
 \begin{loglogaxis}[
   log number format basis/.code 2 args={$\pgfmathparse{#1^(#2)}\mathsf{\pgfmathprintnumber{\pgfmathresult}}$},
 ]
 \addplot [domain=1:1000, ultra thick, color=blue] {x^1.5};
 \end{loglogaxis}
 \end{tikzpicture}

enter image description here

So with this, we have solutions for linear axes, 10n in either semi-log or log-log axes, and fixed point on either semi-log or log axes. To summarize these (combine as necessary for semi-log):

Linear axis:

xticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},

Log axis, 10n ticks:

log number format basis/.code 2 args={$\mathsf{#1^{\pgfmathprintnumber{#2}}}$},

Log axis, fixed-point ticks:

log number format basis/.code 2 args={$\pgfmathparse{#1^(#2)}\mathsf{\pgfmathprintnumber{\pgfmathresult}}$},

We still lack a direct solution for Question 2 above. However, a brute force approach is possible by simply manually coding the ticks:

\begin{tikzpicture}
\begin{loglogaxis}[
   xtick = {1, 10, 100, 1000},
   xticklabels={$\mathsf{1}$, $\mathsf{10}$, $\mathsf{100}$, $\mathsf{10^3}$}, 
   ytick = {1, 10, 100, 1000, 10000},
   yticklabels={$\mathsf{1}$, $\mathsf{10}$, $\mathsf{100}$, $\mathsf{10^3}$, $\mathsf{10^4}$},        
]
\addplot [domain=1:1000, ultra thick, color=red] {x^1.5};
\end{loglogaxis}
\end{tikzpicture}

enter image description here

(Note: My figures have a few style options set on the tick marks themselves, which are not displayed here; these are irrelevant to the numbering issue.)

1

Building on Stefan's suggestion to use \ifdim, I've added keys for an upper and lower limit (eupper, elower) beyond which an exponent will be used rather than fixed point tick labeling. This code will create exponents for log bases other than 10 and incorporates \sffamily for the text font. This comes close to answering Question 2 above, although it has not truly adjusted the std={x:y} functionality of pgfplots.

\pgfkeys{/tikz/.cd,
  elower/.store in=\elower,
  elower=-1,   %% default, so (base)^0 will print as 1
  eupper/.store in=\eupper,
  eupper=1   %% default 
   }

\pgfplotsset{    
log number format basis/.code 2 args={
            \ifdim#2pt>\elower pt
                \ifdim#2pt<\eupper pt
                   \pgfmathparse{#1^(#2)}%
                   $\mathsf{\pgfmathprintnumber{\pgfmathresult}}$
                \else
                   $\mathsf{#1^{\pgfmathprintnumber{#2}}}$
                \fi
             \else
                $\mathsf{#1^{\pgfmathprintnumber{#2}}}$
             \fi
},
/tikz/font={\sffamily},
typeset ticklabels with strut,
}

\begin{tikzpicture}
\begin{loglogaxis}[
    elower=-2, 
    eupper=1,
    log basis  x = 5, %% just to illustrate for a base other than 10
    log basis  y = 10,
    xlabel= {Abscissa label [units]}, 
    ylabel= {Ordinate label [units]},
]
 \addplot [domain=0.01:10, ultra thick, color=red] {x^1.5};
  \node[right, fill=white, rectangle, thin, draw] at (0.5,0.01) {log-log};
\end{loglogaxis}
\end{tikzpicture}

\vspace*{3em}

\begin{tikzpicture}
\begin{semilogyaxis}[
     eupper = 3,
     minor x tick num=1, 
     xticklabel={$\mathsf{\pgfmathprintnumber[zerofill, precision=1]{\tick}}$},
     xlabel= {Abscissa label [units]}, 
     ylabel={Ordinate label [units]},
]
 \addplot [domain=0:10, ultra thick,color=blue] {exp(x)};
 \node[right, fill=white, rectangle, thin, draw] at (5,8) {semi-log};
\end{semilogyaxis}
\end{tikzpicture}

The examples from this code follow. The font used is Lucida Sans OT (under LuaLatex) and as before there are some unrelated stylistic adjustments to the tick marks and plot sizes.
Log-log and semi-log plots with sans serif tick marks

0

I assume that you want to change the font in general to sans serif when you are inside a tikzpicture environment, right? (Otherwise it would look quite strange.) Then please find a solution where only inside the environment the font is changed, but not for the "normal" text and math stuff.

And please find also an automated way to create the ticklabels as you requested, although this is quite inconsistent and I don't like it.

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt,varwidth]{standalone}
% needed to execute stuff "automatically"
\usepackage{etoolbox}
    % change fonts to sans serif then `tikzpicture' environment is started
    \BeforeBeginEnvironment{tikzpicture}{
        \begingroup
            \changeFonts
    }
    % end scope (for font change)
    \AfterEndEnvironment{tikzpicture}{
        \endgroup
    }
\usepackage{sansmath}
    % create a command to switch fonts in `tikzpicture' environments to a
    % sans serif font
    \newcommand*\changeFonts{
        \sffamily\sansmath
    }
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.3,
        % change `log number format basis' so that all numbers below 10^3
        % are written in "fixed" number format and all others in "sci" number
        % format
        log number format basis/.code 2 args={
            \ifdim#2pt<3pt
                \pgfmathparse{10^(#2)}%
                $\pgfmathprintnumber{\pgfmathresult}$
            \else
                $10^{\pgfmathprintnumber{\pgfmathresult}}$
            \fi
        },
    }
\begin{document}
The following plot shows $f(x) = x^{1.5}$.\par

\begin{tikzpicture}
    \begin{loglogaxis}[
        xlabel=$x$-axis,
        ylabel=$y$-axis,
    ]
        \addplot [domain=1:1000, ultra thick, color=blue] {x^1.5};
    \end{loglogaxis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • -Thank you for this. You raise several points. I switch the other sans fonts (for labels, etc.) using a command like \pgfplotsset{/tikz/font={\sffamily}}, which will point directly to the current sans serif text font. As I mentioned to Bobyandbob in an earlier comment, the sansmath package has limitations in terms of font selection (e.g., aims for cmss), so I have found it problematic (since I am not using different font families loaded under LuaLatex). However, your code for log number format is very interesting, and I will try it out later and follow up.
    – John
    Commented Mar 26, 2018 at 22:52
0

As a further follow-up, when sans-serif unicode math fonts are available, we may use fontspec and unicode-math to change the plot fonts, as in the following example. This approach is easier when the axis labels include sans-serif math expressions, especially those with Greek letters. I've taken the two such fonts and paired them with STIX Two as the main font (not an ideal match, but makes the point).

    \documentclass{article}
    \usepackage[svgnames,x11names]{xcolor}

    \usepackage{pgfplots} 
    \pgfplotsset{compat=1.15}

    \pgfplotsset{%
         width=0.9\textwidth,%
         height=0.6\textwidth,%
        every axis/.append style={thick},
        every minor tick/.append style={very thin,black},
        every minor grid/.append style={very thin, color=Snow4},
        every major tick/.append style={thin, black},
        every major grid/.append style={thin, color=Snow4},
        major tick length={1.2em},
        minor tick length={0.5em},
    }

    \usepackage{unicode-math}

    \setmainfont{STIX Two Text}
    \setmathfont[Scale=MatchUppercase, RawFeature = {-ss01, +ss02, +ss08}]{STIX Two Math}  

    \newfontfamily\GFS[Scale=MatchUppercase]{GFS Neohellenic}
    \newfontfamily\FiraGO[Scale=MatchUppercase, RawFeature = {+ss05,+ss06}]{FiraGo}

    \setmathfont[version = Fira, Scale=MatchUppercase]{Fira Math}
    \setmathfont[version = GFSMath, Scale=MatchUppercase]{GFS Neohellenic Math}

    \newcommand*{\Sphinx}{Sphinx of black quartz, judge my vow.}

    \begin{document}

    \Sphinx

    \vspace*{1em}

    {
    \mathversion{Fira}\FiraGO

    \begin{tikzpicture}
    \begin{loglogaxis}[
         xlabel= {Abscissa label, $\lambda(\beta)^2$ [units]}, 
         ylabel={Ordinate label, $gf_{\kern-0.05em u}(\pi)$ [units]},
    ]
     \addplot [domain=1:1000, ultra thick, color=blue] {x^1.5};
     \node[right, fill=white, rectangle, thin, draw] at (30,7) {FiraGO, Fira Math};
    \end{loglogaxis}
    \end{tikzpicture}

    \mathversion{normal}
    }

    \vspace*{1em}

    \Sphinx

    \[
    \frac{d\Gamma}{dt}  = \iint_{\symcal{S}}  \nabla p \times \nabla \left( \frac{1}{\rho}\right) \cdot d\symbf{A}
    \]

    \vspace*{1em}


    {
    \mathversion{GFSMath}\GFS
    \begin{tikzpicture}
    \begin{loglogaxis}[
         xlabel= {Abscissa label, $\lambda(\beta)^2$  [units]}, 
         ylabel={Ordinate label, $gf_{u}(\pi)$ [units]},
    ]
     \addplot [domain=1:1000, ultra thick, color=blue] {x^1.5};
     \node[right, fill=white, rectangle, thin, draw] at (30,7) {GFS Neohellenic};
    \end{loglogaxis}
    \end{tikzpicture}
    \mathversion{normal}
    }

    \Sphinx

    \end{document}

enter image description here

You must log in to answer this question.

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