[Tex/LaTex] Use math font for TikZ arrow labels

beamerfontslabelsmath-modetikz-styles

I am trying to make arrow labels look like they are written in math mode. This is what I have:
enter image description here

This is what I want enter image description here,

I tried to use different fonts but without success. This is my code:

    \begin{tikzpicture}[node distance=2cm,inner sep=2pt,minimum size=0.5mm, bend angle=45]                                                                                                                                          
  \tikzstyle{place} = [ circle,draw=black,fill=black,thick ]                                                                                                                                                                    
  \tikzstyle{surd} = [circle]                                                                                                                                                                                                   
  \tikzstyle{pre} = [ <-,shorten <=2pt,shorten >=2pt, >=stealth', semithick]                                                                                                                                                    
  \tikzstyle{every node} = [font=\normalsize\itshape]                                                                                                                                                                           

  \node[place] (first) {};                                                                                                                                                                                                      
  \node[place] (second) [right=of first] {}                                                                                                                                                                                     
    edge [pre,bend right]  node [above] {$a$} (first)                                                                                                                                                                           
    edge [pre,bend left]  node [below] {$b$} (first);                                                                                                                                                                           
  \node[place] (third) [right=of second] {}                                                                                                                                                                                     
    edge [pre]  node [above] {$c$} (second);                                                                                                                                                                                    
  \node[surd] (surd)  [right=of third] {\Large$\surd$}                                                                                                                                                                          
    edge [pre] node [above] {$d$} (third);                                                                                                                                                                                      
\end{tikzpicture} 

Best Answer

With the Beamer class you may want to use:

\usefonttheme[onlymath]{serif}

to render in serif font only the math. If one does not want to type every time the $ to enter in math mode, it is possible to create a style for that:

\tikzset{math mode/.style={%
    execute at begin node=$,%
    execute at end node=$,%
  }
} 

Then, by grouping all nodes in a scope:

\begin{scope}[every node/.style={math mode}]
...
\end{scope}

the style will be applied "locally".

An example with the math mode style active:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\usefonttheme[onlymath]{serif}

\begin{document}
\begin{frame}{title}
Some text in sans serif font. Math, like:
\begin{equation}
x+y=100
\end{equation}
is rendered in serif font.
\begin{center}
    \begin{tikzpicture}[node distance=2cm,inner sep=2pt,minimum size=0.5mm, bend angle=45]                                                                                                                                          
  \tikzset{place/.style={circle,draw=black,fill=black,thick}}
  \tikzset{surd/.style={circle}}                   
  \tikzset{pre/.style={ <-,shorten <=2pt,shorten >=2pt, >=stealth', semithick}} 
  % to avoid using every time dollars for math mode
  \tikzset{math mode/.style={%
      execute at begin node=$,%
      execute at end node=$,%
    }
  }

  \node[place] (first) {};
  % using a scope to not apply the new math mode definition to all nodes
  % as it might not be the desired behaviour
  \begin{scope}[every node/.style={math mode}]
  \node[place] (second) [right=of first] {}
    edge [pre,bend right]  node [above] {a} (first)
    edge [pre,bend left]  node [below] {b} (first);
  \node[place] (third) [right=of second] {}
      edge [pre]  node [above] {c} (second);
  \node[surd] (surd)  [right=of third] {\Large\surd}
    edge [pre] node [above] {d} (third);
  \end{scope}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

A complete example without the style math mode:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\usefonttheme[onlymath]{serif}

\begin{document}
\begin{frame}{title}
Some text in sans serif font. Math, like:
\begin{equation}
x+y=100
\end{equation}
is rendered in serif font.
\begin{center}
    \begin{tikzpicture}[node distance=2cm,inner sep=2pt,minimum size=0.5mm, bend angle=45]                                                                                                                                          
  \tikzset{place/.style={circle,draw=black,fill=black,thick}}
  \tikzset{surd/.style={circle}}                   
  \tikzset{pre/.style={ <-,shorten <=2pt,shorten >=2pt, >=stealth', semithick}}
  %\tikzset{every node/.append style={font=\normalsize\itshape}} %useless in this case 

  \node[place] (first) {};
  \node[place] (second) [right=of first] {}
    edge [pre,bend right]  node [above] {$a$} (first)
    edge [pre,bend left]  node [below] {$b$} (first);
  \node[place] (third) [right=of second] {}
      edge [pre]  node [above] {$c$} (second);
  \node[surd] (surd)  [right=of third] {\Large$\surd$}
    edge [pre] node [above] {$d$} (third);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

The result:

enter image description here

Related Question