Beamer の lstlisting 環境に数学記号を挿入するにはどうすればいいですか?

Beamer の lstlisting 環境に数学記号を挿入するにはどうすればいいですか?

ここに画像の説明を入力してください

上記のアルゴリズムを Beamer ブロックに入力したいのですが、 を挿入できません。と をmathcal symbols試しましたが、どちらも期待されるシンボルを返すことができませんでした。 $\mathcal{Q}$\mathcal{Q}

私の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}

アップデート

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

上記のアルゴリズムを多数のエラーを伴って入力しました。最初、このタイプのアルゴリズムを LaTeX で入力しました。そのため、このコードを編集してこのアルゴリズムのような結果を得るための予想されるコードを誰かが教えてくれると、非常に助かります。

答え1

問題は、それが逐語的な環境であるということです。すべて逐語的に。ない逐語的にオプションを使用する必要がありますescapeinside:

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

間にあるすべてのコードは@(...)@LaTeX にエスケープされます。

あいまいさを避けるために、エスケープには複数の文字を使用するのがベストプラクティスです。また、コードに表示されない組み合わせを選択するようにしてください。たとえば、Python では、ループ:(...):の構文のため、 を使用することはお勧めできませんfor

ここに画像の説明を入力してください

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

答え2

OP はアルゴリズムの例を見たいとのことなので...

\Inputのコードはゲルノットの答え

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

ここに画像の説明を入力してください

関連情報