# How can I tell lstlisting to use sans-serif when escaping to math mode?

Title says it all, an example follows:

\lstset{
mathescape=true,
basicstyle = \sffamily
}

\begin{lstlisting}
func($x_0$)
\end{lstlisting}


Where and how can I set the math font used here to be sans-serif italic?

-

\documentclass[a4paper]{article}
\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}  {sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}  {sans}{OMS}{cmbrs}{m}{n}

\usepackage{listings}
\lstnewenvironment{sflisting}[1][]
{\lstset{#1}\mathversion{sans}}{}

\begin{document}

\lstset{
mathescape=true,
basicstyle = \sffamily
}

\begin{sflisting}[caption=Function]
func($x_0$)
\end{sflisting}
\end{document}


This uses the CMBright fonts, which are mostly compatible with CM Sans.

-
Looks like the right approach, but using it the caption and label doesn't work anymore, see here. "Package Listings Warning: Text dropped after begin of listing on input line 26." –  neo Jun 21 '11 at 0:26
@neo: define the environment like this: \lstnewenvironment{sflisting}[2] {\mathversion{sans},\lstset{caption=\lstname#1,label=#2}}{} and use it like: \begin{sflisting}{<caption>}{<label>}...\end{sflisting}. –  Gonzalo Medina Jun 21 '11 at 1:53
@neo: see edited answer –  egreg Jun 21 '11 at 8:06
Thanks, captions etc. now work, but the actual math font in the listing is quite pixelated when zoomed in. The version from Herbert's answer looks smooth. Why is that? –  neo Jun 21 '11 at 10:24
You probably don't have the Type1 version of the CMBright fonts, which however is in TeX Live and I believe also in MiKTeX (hfbright should be the name of the package to install). It depends on your system. What answer to choose depends mostly on the math contents of your listings. –  egreg Jun 21 '11 at 10:31
\documentclass[a4paper]{article}
\usepackage{sansmath}
\usepackage{listings}
\lstnewenvironment{sflisting}[1][]
{\lstset{mathescape,basicstyle=\sffamily,#1}\sansmath}
{}
\begin{document}

\begin{sflisting}[caption=Function]
func($\frac1x \mathrm{d}x$)
\end{sflisting}

\end{document}


-
Works quite good, although sansmath doesn't use sans greek letters which I need. The documentation says to use [eulergreek] to get those in sans, but then unfortunately greek letters are always non-italic, maybe a bug? –  neo Jun 21 '11 at 10:39
@neo: with option [eulergreek] they are upright, compare the \beta symbol with and without the option. –  Herbert Jun 21 '11 at 11:10