비머의 목록 환경에 수학 기호를 삽입하는 방법은 무엇입니까?

비머의 목록 환경에 수학 기호를 삽입하는 방법은 무엇입니까?

여기에 이미지 설명을 입력하세요

비머 블록에 위의 알고리즘을 입력하고 싶은데 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}

여기에 이미지 설명을 입력하세요

관련 정보