Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted
\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.

share|improve this answer
    
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
show 1 more comment
\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}

enter image description here

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.