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.

I want to use math mode in lstlisting. How do I do that? How to put tab in this mode?

\begin{document}

\begin{lstlisting}[frame=single]                % Start your code-block
module main()
enumerated States {$S_0$,$S_1$,$S_2$,$S_3$}
integer c;
initial : c = 0 and States = $S_0$;
endmodule
\end{lstlisting}

enter image description here

\end{document}
share|improve this question
1  
See 4.14 Escaping to LATEX of the documentation of listings. –  Ulrike Fischer Jul 18 '12 at 10:36
add comment

1 Answer 1

up vote 4 down vote accepted

The escapeinside option allows you to define how to escape from verbaitm. Here's an example:

\begin{lstlisting}[escapeinside={(*}{*)}]
  if foo
  list= { (*$S_1,S_2,S_3$*) }
\end{lstlisting}

I've used escapeinside to define an opening (* and closing *) escape commands. Things between (* and *) will be typeset normally. Obviously, if (* or *) are common in whatever language you're using, you should pick something else.

In fact, if all you want to do is escape maths, you can use the mathescape option:

\begin{lstlisting}[mathescape=true]
  if foo
  list= { $S_1,S_2,S_3$ }
\end{lstlisting}
share|improve this answer
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.