![我在 LaTeX 文档中的自定义命令中使用 \if 条件。我可以知道如何在上传的图片中执行 if 语句吗?](https://linux22.com/image/431128/%E6%88%91%E5%9C%A8%20LaTeX%20%E6%96%87%E6%A1%A3%E4%B8%AD%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E5%91%BD%E4%BB%A4%E4%B8%AD%E4%BD%BF%E7%94%A8%20%5Cif%20%E6%9D%A1%E4%BB%B6%E3%80%82%E6%88%91%E5%8F%AF%E4%BB%A5%E7%9F%A5%E9%81%93%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%8A%E4%BC%A0%E7%9A%84%E5%9B%BE%E7%89%87%E4%B8%AD%E6%89%A7%E8%A1%8C%20if%20%E8%AF%AD%E5%8F%A5%E5%90%97%EF%BC%9F.png)
\documentclass[a4paper,11pt]{article}
\usepackage{siunitx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{textcomp}
\usepackage{mathpazo}
\usepackage{eulervm}
\usepackage[utf8]{inputenc}
\usepackage{algorithmic}
\usepackage{ifthen}
\usepackage{amsmath}
\DeclareUnicodeCharacter{2212}{\textminus}% requires a Unicode capable editor
\sisetup{
detect-mode,
detect-family,
detect-inline-family=math,}
\begin{document}
\begin{algorithm}
\caption{PPO}
\begin{algorithmic}[1]
\For {$i=1,2,\ldots ,m$}
\EndFor
\For {$i=1,2,\ldots,m$}
\State $forbidden\_set\leftarrow\theta$
\State ${u}\leftarrow\ {p}[i]$
\State ${v\leftarrow\ {get}\_original\_node(u)}$
\For {each node $s$ that adjacent to $u$}
\IF{$c[$s$]!\= -1$}
\State ${v\leftarrow\ {get}\_original\_node(s)}$
\State ${a\leftarrow\ Max}\rbra\_original\_node(u)}$
\ENDIF
\State Run policy $\pi_{\theta_{old}}$ in environment for $T$ time steps
\State Compute advantage estimates $\hat{A}_{1},\ldots,\hat{A}_{T}$
\EndFor
\State Optimize surrogate $L$ wrt. $\theta$, with $K$ epochs and minibatch size $M\leq NT$
\State $\theta_{old}\leftarrow\theta$
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
您不应该algorithmic
一起加载algpseudocode
:它们彼此不兼容。
您正在使用前一个包的语法,因此请坚持使用它\If{...}...\EndIf
。
我还对代码做了一些更改,请仔细检查。我删除了\rbra
没有出现在任何地方的代码;修复它。
\documentclass[a4paper,11pt]{article}
\usepackage{siunitx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{textcomp}
\usepackage{mathpazo}
\usepackage{eulervm}
\usepackage[utf8]{inputenc}
%\usepackage{algorithmic} %%%% NOT along algpseudocode
\usepackage{ifthen}
\usepackage{amsmath}
\DeclareUnicodeCharacter{2212}{\textminus}% requires a Unicode capable editor
\sisetup{
detect-mode,
detect-family,
detect-inline-family=math,}
\begin{document}
\begin{algorithm}
\caption{PPO}
\begin{algorithmic}[1]
\For {$i=1,2,\dots ,m$}
\EndFor
\For {$i=1,2,\dots,m$}
\State $\mathit{forbidden\_set}\leftarrow\theta$
\State $u\leftarrow {p}[i]$
\State $v\leftarrow \mathit{get\_original\_node}(u)$
\For {each node $s$ that adjacent to $u$}
\If{$c[s]\neq -1$}
\State $v\leftarrow \mathit{get\_original\_node}(s)$
\State $a\leftarrow \mathit{Max\_original\_node}(u)$
\EndIf
\State Run policy $\pi_{\theta_{\mathrm{old}}}$ in environment for $T$ time steps
\State Compute advantage estimates $\hat{A}_{1},\dots,\hat{A}_{T}$
\EndFor
\State Optimize surrogate $L$ wrt. $\theta$, with $K$
epochs and minibatch size $M\leq\nobreak NT$
\State $\theta_{\mathrm{old}}\leftarrow\theta$
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
为了修复长行 15,您可以这样做(等到您确定文本已是最终形式)
\State Optimize surrogate $L$ wrt. $\theta$, with $K$\hspace*{\fill}\linebreak
\hspace*{3em}epochs and minibatch size $M\leq NT$
这将产生