5

Does anyone know how to write math symbols such as sums and integrals inside a listing?

\begin{lstlisting}

vl(i)= \sum\limits_{i=1}^N{Al(i,j)*xl(j)}  ;     i = 1,3

\end{lstlisting}

UPDATE: Minimal example

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[backgroundcolor=\color{blue!40},numbers=none,language=XML,mathescape=true]
!-----------------------------------------------------

 $vl(i)=\sum\limits_{i=1}^N{Al(i,j)*xl(j)} ;\quad i = 1,3$

!-----------------------------------------------------
\end{lstlisting}
\end{document}

enter image description here

karlkoeller
  • 124,410
kostas
  • 247

1 Answers1

9

Use mathescape

Code

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[mathescape]
$vl(i)= \sum\limits_{i=1}^N{Al(i,j)*xl(j)} ;\quad i = 1,3$
\end{lstlisting}
\end{document} 

Output

enter image description here


EDIT

To avoid blank lines in the background, you can try something with mdframed:

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[framemethod=TikZ]{mdframed}

\begin{document}
\begin{mdframed}[backgroundcolor=blue!40]
\begin{lstlisting}[numbers=none,language=XML,mathescape=true]
!-----------------------------------------------------

 $vl(i)=\sum\limits_{i=1}^N{Al(i,j)*xl(j)} ;\quad i = 1,3$

!-----------------------------------------------------
\end{lstlisting}
\end{mdframed}
\end{document} 

Output:

enter image description here

karlkoeller
  • 124,410
  • Keep in mind, that colored backgrounds might be an issue, see http://tex.stackexchange.com/questions/10958/listings-with-background-color-and-mathescape-broken – Ronny Dec 09 '13 at 20:49
  • Ok thanks but this creates a problem.It cuts the background color at the beginning of equations and then restores.Take a look – kostas Dec 09 '13 at 20:53
  • @kostas I've edited the answer. – karlkoeller Dec 09 '13 at 21:43