latex - Latex 算法 - IF-ELSE - IF 在开始之前显示结束 ELSE
问题描述
正如您在我的乳胶中看到的那样,它显示在 If 语句之后结束,该语句应该在 Else 之后结束。我尝试删除 IF 的花括号,但它不起作用。
\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
\usepackage[pdftex]{graphicx}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage[linesnumbered,ruled,vlined] {algorithm2e}
\usepackage{nomencl}
\usepackage{algpseudocode}
\usepackage{algorithm}
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\begin{algorithm}
\caption{PEC, \texttt{PEC}}
\SetAlgoLined
\DontPrintSemicolon
\textbf{Input:} I, P, C.
\textbf{Output:} Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$
//Initialization
\quad \quad \quad $PS^* \gets \emptyset$
\For {each, $i \in I$} {
// Calculate
$PS^{temp}_{i} \gets P$
$PS^{loss}_{i} \gets C)$
\If { $ C_{i}- P_{i} > 0$}{
$PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
}
\Else {
$PS_{i} \gets 0$
}
}
return ($PS^*$)
\label{alg:PoEG}
\end{algorithm}
\end{document}
这个问题的输出如下,正如你看到的突出显示的end,我需要消除它。
解决方案
由于两个主要问题,您的代码无法编译:
您不得使用冲突的选项多次加载 graphicsx 包。如果你的 tex 发行版自石器时代以来至少更新过一次,那么最好在没有选项的情况下加载它,让 latex 自己确定必要的驱动程序。
您不得为算法加载冲突的包。决定要使用哪一个并加载它。Latex 非常清楚地为您提供有关已定义命令的错误消息,不要忽略错误!!!!!!
...然后附加的有一点不便end
:只需使用\eIf
宏而不是\If
如果你有一个 in-else 语句
最后是一个建议:如果你不手动弄乱\quad
s 而是使用适当的关键字,事情会自动对齐
\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
%\usepackage[pdftex]{graphicx}
%\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage{algpseudocode}
%\usepackage{algorithm}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{nomencl}
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\begin{algorithm}
\caption{Proof of Energy Generation, \texttt{PoEG}}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\DontPrintSemicolon
\Input{Prosumer Set, $\textit{I}=\{{\textit{I}_{1},\textit{I}_{2},...,\textit{I}_{n}}$\},\linebreak
Predicted energy production, $\textit{P}= \{{\textit{P}_{1},\textit{P}_{2},...,\textit{P}_{n}}$\}, \linebreak
Predicted energy consumption, $\textit{C}= \{{\textit{C}_{1},\textit{C}_{2},...,\textit{C}_{n}}.$\},\linebreak
Distance from DS, $\textit{D}= \{{\textit{d}_{1},\textit{d}_{2},...,\textit{d}_{n}}$\}.}
\Output{Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$ }
//Initialization
\quad \quad \quad $PS^* \gets \emptyset$
\For {each prosumers, $i \in I$} {
// Calculate proof score for energy balance and loss,
$PS^{temp}_{i} \gets \log(1/e^{C_i-P_i})$
$PS^{loss}_{i} \gets \log(e^{E_{d_i}})$
\eIf { $ C_{i}- P_{i} > 0$}{
$PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
}{
$PS_{i} \gets 0$
}
$PS^* \gets$ $PS^* \cup \{PS_{i}\}$
}
return ($PS^*$)
\label{alg:PoEG}
\end{algorithm}
\end{document}
推荐阅读
- c++ - 此代码编译正常但文件未创建?请指出错误
- google-cloud-platform - 转移存储桶的所有权和计费
- javascript - DevTools 无法解析 SourceMap:chrome-extension
- c# - .Net Core 3 中是否有 StringEnumConverter 的替代品?
- java - HHH10001002:使用 Hibernate 内置连接池(不用于生产!)
- rest - 是否有将多个 API 调用组合成一个 HTTP 请求的标准方法?
- mongodb - 有没有办法在 MongoDB 聚合期间将字符串(格式:“1,5,20,22”)转换为整数数组( [1,5, 20, 22] )?
- asp.net-core - asp.net core中登录过期时如何捕获进程
- sql - sql - 某些特定记录出现的增量
- javascript - 包含一组有限的特殊字符的正则表达式模式