4

I'm trying to define a document template, in which I have a serif-font for the main text and math, but just for my plots (tikz/pgfplots) I'd like to have a sans font, Fira Sans namely.

I can load the Fira Sans package in the preamble, and successfully switch between serif and sans serif fonts. If I enable \usepackage(sfmath}, the plot looks good, while the equation in the main text is also set using the sans-font. On the other hand, if I disable % \usebackage{sfmath}, the math in the plot is set in serif-font.

Here a small example...

\documentclass{article}

\usepackage{Fira Sans}
% \usepackage{sfmath}     <-- enable/disable here!
\usepackage{units}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\lipsum[2]

\begin{equation}
   \Gamma(\phi) = \oint \alpha(\phi)\,\textrm{d}\phi    
\end{equation}

\begin{figure}[!h]
    \centering
        \begin{tikzpicture}
            \sffamily
            \def\curvea{287*100/x/1000}
            \def\curveb{287*300/x/1000}
            \def\curvec{287*500/x/1000}

            \begin{axis}[
                minor x tick num=3, minor y tick num=3,
                xlabel={spec. Volume $v$ in $\unitfrac{m^3}{kg}$},
                ylabel={Pressure $p$ in $\unit{kPa}$},
                domain=0:3, y domain=0:300,
                xmin=0, xmax=3, ymin=0, ymax=300,
                samples=100,
            ]
                  
            \addplot[thick, solid]{\curvea} node[inner sep=2, fill=white, rotate=-10, pos=0.982] {\tiny $T=\Ta~\unit{K}$};
        
            \addplot[thick, solid]{\curveb} node[inner sep=2, fill=white, rotate=-10, pos=0.993] {\tiny $T=\Tb~\unit{K}$};
        
            \addplot[thick, solid]{\curvec} node[inner sep=2, fill=white, rotate=-10, pos=0.998] {\tiny $T=\Tc~\unit{K}$};
        
          \end{axis}
        \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\lipsum[4]

\end{document}

Result WITHOUT SFMATH


Result WITH SFMATH

Please, could anyone point me into the right direction on how to use Fira Sans for the math inside the plot also, by keeping the serif-fonts for the main text?

2
  • Just for info: There exists Fira Math package which provides Unicode support (to be used with XeLaTeX/LuaLaTeX). Commented Aug 8, 2022 at 14:10
  • Hi, thanks for your reply! I want to stay in pdflatex, thus luatex/xelatex packages/features are not an option, sorry! In addition, I know how to typeset a whole document in Fira Sans with math, nut here I only want to use Fira Sans (including the math) in the figures, i.e. tikz/pgfplots scope in the above example.
    – George
    Commented Aug 8, 2022 at 14:53

3 Answers 3

1

You probably want to use sansmath instead and also siunitx instead of the older and less flexible units.

\documentclass{article}

\usepackage{Fira Sans}
\usepackage{sansmath}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\lipsum[2]

\begin{equation}
\Gamma(\phi) = \oint \alpha(\phi)\,\textrm{d}\phi    
\end{equation}

\begin{figure}[!h]
\centering

\begin{tikzpicture}
  \sffamily\sansmath
  \sisetup{detect-all}
  \def\curvea{287*100/x/1000}
  \def\curveb{287*300/x/1000}
  \def\curvec{287*500/x/1000}

  \def\Ta{3}\def\Tb{4}\def\Tc{5}% guess, fix it

  \begin{axis}[
    minor x tick num=3,
    minor y tick num=3,
    xlabel={spec.\@ Volume $v$ in \unit{m^3/kg}},
    ylabel={Pressure $p$ in \unit{kPa}},
    domain=0:3,
    y domain=0:300,
    xmin=0,
    xmax=3,
    ymin=0,
    ymax=300,
    samples=100,
  ]
  \addplot[thick, solid]{\curvea} node[inner sep=2, fill=white, rotate=-10, pos=0.982]
    {\tiny $T=\qty{\Ta}{K}$};
  \addplot[thick, solid]{\curveb} node[inner sep=2, fill=white, rotate=-10, pos=0.993]
    {\tiny $T=\qty{\Tb}{K}$};
  \addplot[thick, solid]{\curvec} node[inner sep=2, fill=white, rotate=-10, pos=0.998]
    {\tiny $T=\qty{\Tc}{K}$};
  \end{axis}
\end{tikzpicture}

\caption{Caption}
\label{fig:my_label}

\end{figure}

\lipsum[4]

\end{document}

enter image description here

1
  • Great, works semless, and in addition to the answer of sergei (above) it does not need external processing. Please, could you explain: I thought that in order to use Fira Sans in math I would have to use \sfmath package!? Is the sansmath package using the default sans font (Fira Sans here)? Could you shed a bit of light on this?
    – George
    Commented Aug 9, 2022 at 14:08
1

As far as I know, it's not easy to switch back and forth between math fonts in LaTeX without unicode-math (which is not available for pdflatex). I can suggest the following workaround: use the external Tikz library and add the sfmath package only when processing figures, like this:

\documentclass{article}

\ifx\tikzexternalrealjob\undefined
\else
  \usepackage[lining]{Fira Sans}
  \usepackage{sfmath}
\fi
\usepackage{units}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{external}
\tikzexternalize

\begin{document}
\lipsum[2]

\begin{equation}
   \Gamma(\phi) = \oint \alpha(\phi)\,\textrm{d}\phi    
\end{equation}

\begin{figure}[!h]
    \centering
        \begin{tikzpicture}
            \sffamily
            \def\curvea{287*100/x/1000}
            \def\curveb{287*300/x/1000}
            \def\curvec{287*500/x/1000}

            \begin{axis}[
                minor x tick num=3, minor y tick num=3,
                xlabel={spec. Volume $v$ in $\unitfrac{m^3}{kg}$},
                ylabel={Pressure $p$ in $\unit{kPa}$},
                domain=0:3, y domain=0:300,
                xmin=0, xmax=3, ymin=0, ymax=300,
                samples=100,
            ]
                  
            \addplot[thick, solid]{\curvea} node[inner sep=2, fill=white, rotate=-10, pos=0.982] {\tiny $T=100~\unit{K}$};
        
            \addplot[thick, solid]{\curveb} node[inner sep=2, fill=white, rotate=-10, pos=0.993] {\tiny $T=300~\unit{K}$};
        
            \addplot[thick, solid]{\curvec} node[inner sep=2, fill=white, rotate=-10, pos=0.998] {\tiny $T=500~\unit{K}$};
        
          \end{axis}
        \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\lipsum[4]

\end{document}

There are downsides with this approach though. First, you'd have to build your document with --shell-escape flag. Second, the document will be processed multiple times, so its compiles slower. Third, sometimes Tikz fails to recognize that it's necessary to rebuild the pictures (e.g. when you change the preamble), which requires deleting them manually.

The result: enter image description here

2
  • Great solution, works like a charm ;) However, external processing is related to the drawbacks you've indicated, even though this does not bother me here :D
    – George
    Commented Aug 9, 2022 at 14:06
  • I hope you understand, that i have chosen egreg's answer as preferred answer, because no external processing is required.
    – George
    Commented Aug 9, 2022 at 14:11
0

If I understand right, you want to change locally in a document the font without changing globally the default fonts of LaTeX. You can use the command {\fontfamily{FiraSans-TLF}\selectfont yourtext} to change locally the font, or the more general command {\usefont{encoding}{family-name}{series}{shape}{size}\selectfont yourtext}. For your case something like {\usefont{FiraSans-TLF}{T1}{m}{up}\selectfont yourtexthere}. Please note that for the above to work you have to know the family name of a font which in not usually its official name. For reference you can have a look here: Latex2e unofficial reference manual

Here is an example:

\documentclass[12pt]{article}  
\usepackage[a4paper,body={180mm,247mm},left=10mm,top=20mm]{geometry}  
\usepackage[T1]{fontenc}  
\usepackage[english]{babel}  
\begin{document}  
    This sentence is written with \LaTeX default font  
    
    {\sffamily This sentence is written with \LaTeX default Sans Serif  font}   
    
    {\fontfamily{FiraSans-TLF}\selectfont This sentence is written with   Fira Sans font}  
    
    {\usefont{T1}{FiraSans-TLF}{bb}{it} \textit{This sentence is written   with Fira Sans font}}  
    
    This sentence is written again with \LaTeX default font  
\end{document}  
1
  • Thanks a lot, but it is not just the basic fonts - for which I'm aware of how to do this - it is about changing the math font!
    – George
    Commented Aug 9, 2022 at 14:02

You must log in to answer this question.

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