¿Cómo insertar un símbolo matemático en el entorno lstlisting en Beamer?

¿Cómo insertar un símbolo matemático en el entorno lstlisting en Beamer?

ingrese la descripción de la imagen aquí

Quiero escribir el algoritmo anterior en el bloque Beamer, pero me quedé sin insertar mathcal symbols. Lo intenté $\mathcal{Q}$y \mathcal{Q}, pero ninguno de los dos devolvió el símbolo esperado.

Mi 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}

Actualizar

\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}

Escribí el algoritmo anterior con numerosos errores. Al principio escribí este tipo de algoritmo en LaTeX. Entonces, si alguien me da el código esperado editando este código para obtener el resultado como este algoritmo, entonces será de gran ayuda para mí.

Respuesta1

El problema es que es un entorno textual, por lo quetodoes palabra por palabra. Para hacerlonotextualmente tienes que usar la opción escapeinside:

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

para que cualquier código intermedio @(...)@se escape a LaTeX.

La mejor práctica es utilizar más de un carácter para escapar para evitar ambigüedades. Intente también elegir una combinación que no aparecerá en su código. En Python, por ejemplo, usar :(...):sería una mala idea debido a la sintaxis del forbucle.

ingrese la descripción de la imagen aquí

\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}

Respuesta2

Dado que al OP le gustaría ver un ejemplo de un algoritmo...

El código \Inputproviene deuna respuesta de gernot.

\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{inn}}$
            \For{$i=0$ to $10$}
            \State do something with arg1 and arg2
            \State $bar \leftarrow qux()$
            \EndFor
        \end{algorithmic}
    \end{block}
\end{frame}
\end{document}

ingrese la descripción de la imagen aquí

información relacionada