How to change font family LaTeX plot?

1,009

You can use the sansmath package to get sans-serif math. If you want to limit this to the tikzpicture (which I assumed), you can use etoolbox and

\AtBeginEnvironment{tikzpicture}{\sansmath}
\AtEndEnvironment{tikzpicture}{\unsansmath}

to set/unset this at the beginning and end of tikzpicture.

\documentclass[margin=5pt]{standalone}   
\usepackage{tikz,pgfplots,pgfplotstable}
\usepackage{helvet,sansmath,etoolbox}
\renewcommand{\rmdefault}{phv}
\AtBeginEnvironment{tikzpicture}{\sansmath}
\AtEndEnvironment{tikzpicture}{\unsansmath}

\pgfplotstableread[col sep=semicolon]{
name;S1;S2
john white;912;800
alan ket;484;400
halen koo;309;250
daren kalig;291;280
arif butt;192;150
james seal;142;120
 }\loadedtable
\begin{document} 
$x + y = z$
\begin{tikzpicture}
    \begin{axis}[
            enlarge x limits=0.2,
            xbar,nodes near coords,bar width=16pt,bar shift=0pt,
            symbolic y coords={john white,alan ket,halen koo,daren kalig,arif butt,james seal},
        ytick={john white,alan ket,halen koo,daren kalig,arif butt,james seal},
        %x coord trafo/.code={\pgfmathparse{-x}},
        xticklabels={0,1000,500,0,500,1000},
    ]
    \addplot[nodes near coords align={left},
                draw=blue,fill=blue!25,text=blue]
        table[y=name,x expr={-\thisrow{S1}},point meta={-x}] \loadedtable;

    \addplot table[y=name,x expr={\thisrow{S2}}] \loadedtable;
\end{axis}
\end{tikzpicture}
$a^2 + b^2 = c^2$
\end{document}

enter image description here

Share:
1,009

Related videos on Youtube

user3573959
Author by

user3573959

Updated on August 01, 2022

Comments

  • user3573959
    user3573959 over 1 year

    I want to change the font family of the bar plot to Helvetica fonts.

    I have tried using the following code but it only changes axis fonts and not the data labels inside the plot:

    \documentclass[margin=5pt]{standalone}   
    \usepackage{tikz,pgfplots,pgfplotstable}
    \usepackage{helvet}
    \renewcommand{\rmdefault}{phv}
    
    \pgfplotstableread[col sep=semicolon]{
    name;S1;S2
    john white;912;800
    alan ket;484;400
    halen koo;309;250
    daren kalig;291;280
    arif butt;192;150
    james seal;142;120
     }\loadedtable
    \begin{document} 
    \begin{tikzpicture}
        \begin{axis}[
                enlarge x limits=0.2,
                xbar,nodes near coords,bar width=16pt,bar shift=0pt,
                symbolic y coords={john white,alan ket,halen koo,daren kalig,arif butt,james seal},
            ytick={john white,alan ket,halen koo,daren kalig,arif butt,james seal},
            %x coord trafo/.code={\pgfmathparse{-x}},
            xticklabels={0,1000,500,0,500,1000},
        ]
        \addplot[nodes near coords align={left},
                    draw=blue,fill=blue!25,text=blue]
            table[y=name,x expr={-\thisrow{S1}},point meta={-x}] \loadedtable;
    
        \addplot table[y=name,x expr={\thisrow{S2}}] \loadedtable;
    \end{axis}
    \end{tikzpicture}
     \end{document}
    

    Here is the plot:

    plot