# How to put equation within listing caption?

I'm trying to do something like this:

\begin{lstlisting}[language=Python, caption=Here is an equation $y=2x$.]
def y(x):
return 2*x
\end{lstlisting}


But I get an error due to the equation in the caption. Is there a way to do it?

• Try to put the caption inside brackets, caption={...} – StefanH Sep 23 at 17:54
• Worked, thanks! – sean Sep 24 at 9:34