[Tex/LaTex] How to insert mathcal symbol in lstlisting environment in beamer

beamermathcal

enter image description here

I want to type the above algorithm in beamer block, but I stuck inserting mathcal symbols. I tried $\mathcal{Q}$ and \mathcal{Q}, but both are failed to return expected symbol.

My MWE:

\documentclass{beamer}
\usepackage{listings}
\usetheme{Madrid}
%\lstset{language=Python}

\begin{document}
\begin{frame}[fragile]
\begin{block}{Algorithm}
\begin{lstlisting}
$\mathcal{Q}$
  for i in range(10):
      foo(arg1, arg2)
  bar = qux()
\end{lstlisting}
\end{block}
\end{frame}
\end{document}

Update

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{algpseudocode}
%this code is from: https://tex.stackexchange.com/a/353165/101651
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

\begin{document}
\begin{frame}[fragile]
    \begin{block}{Algorithm}
        \begin{algorithmic}
      Input: $\mathcal{Q}_{\text{init}}$, \mathcal{A}, and \textit{f}(c).
    Initialize: Obtain \writetilde{c_{i}} by solving frac{\delta f(c)}{\delta c_{i}}=0, for i\in\mathcal{N}.Set k = 1, \mathcal{B} = \mathcal{Q}_{init},\mathcal{u}_{i}=\gamma_{ub}(\mathcal{Q}_{init} and \mathcal{l}_{1} = \gamma_\left\{ lb}(\]mathcal{Q}_{init}.
      Check the feasibility of problem (17) with given \writetilde{c}.
      if feasible then
      c_{0} = \writetilde{c};
      else
      while u_{k} - l_{k} > \epsilon do
      Branching:
      \begin{itemize}
        \item Set \mathcal{Q}_{k} = \mathcal{Q}, where \mathcal{Q} satisfies \gamma_\left\{ lb}(\mathcal{Q} = l_{k}.
          \item Split \mathcal{Q} into \mathcal{Q}_{\rm{I}} and \mathcal{Q}_{\rm{II}}, along one of its longest edges.
          \item Update \mathcal{B}_{k+1} = (\mathcal{B}_{k}\{\mathcal{Q}_{k}}) \union (\mathcal{Q}_{\rm{I}}, \mathcal{Q}_{\rm{II}}.
      \end{itemize}
      Bounding:
      \begin{itemize}
        \item Update \mathcal{u}_{k+1} = \min_{\mathcal{Q}\in\mathcal{B}_{k+1}{\gamma_{ub}(\mathcal{Q})}
        \item Update \mathcal{l}_{k+1} = \min_{\mathcal{Q}\in\mathcal{B}_{k+1}{\gamma_{lb}(\mathcal{Q})}
      \end{itemize}
      Set k=k+1;
      end while
      Set c_{0} = c_{min};
      end if
      Output: c_{0}.
        \end{algorithmic}
    \end{block}
\end{frame}
\end{document}

I typed the above Algorithm with numerous errors. I typed this type of algorithm in LaTeX at first. So, if some one give me the expected code editing this code to get the result like this algorithm, then it is very helpful to me.

Best Answer

The problem is that it is a verbatim environment, so everything is verbatim. To make it not verbatim you have to use the option escapeinside:

\lstset{escapeinside={@(}{)@}}

so that any code between @(...)@ is escaped back to LaTeX.

Best practice is to use more than one character for escaping to avoid ambiguity. Also try to choose a combination that will not appear in your code. In python, for example, using :(...): would be a bad idea because of the syntax of the for loop.

enter image description here

\documentclass{beamer}
\usepackage{listings}
\usetheme{Madrid}
%\lstset{language=Python}
\lstset{escapeinside={@(}{)@}}

\begin{document}
\begin{frame}[fragile]
\begin{block}{Algorithm}
\begin{lstlisting}
@($\mathcal{Q}$)@
  for i in range(10):
      foo(arg1, arg2)
  bar = qux()
\end{lstlisting}
\end{block}
\end{frame}
\end{document}