我想在 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
来自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}