1

I am having difficulty adding code fragment to my slides in beamer with listings package.

My code:

\documentclass{beamer}
%\usepackage[utf8]{inputenc}
\usetheme{Warsaw}
\usepackage{listings}

\title{My slies}
\author{Me }
\date{October 2020}

%slide number
\expandafter\def\expandafter\insertshorttitle\expandafter{%
  \insertshorttitle\hfill%
  \insertframenumber\,/\,\inserttotalframenumber}

\begin{document}

\begin{frame}{Slide 1}

 \begin{lstlisting}[basicstyle=\footnotesize]
def LosScore(y_pred, y_test):
  mse = []
  for i in range(len(y_test)):
     mse.append(np.mean(np.square((y_test[i]-y_pred[i]))))
return mse
\end{lstlisting}

\end{frame}

\end{document}

Error:

Runaway argument?

! Paragraph ended before \lst@next was complete.
<to be read again> 
                   \par 
l.27 \end{frame}
                
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
2
  • 5
    You need to pass the option fragile to the frame environment: \begin{frame}[fragile]{Slide 1}. – campa Oct 2 '20 at 12:46
  • Thanks, that works. – arilwan Oct 2 '20 at 14:11