首页 > 解决方案 > 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,我需要消除它。

在此处输入图像描述

标签: latex

解决方案


由于两个主要问题,您的代码无法编译:

  • 您不得使用冲突的选项多次加载 graphicsx 包。如果你的 tex 发行版自石器时代以来至少更新过一次,那么最好在没有选项的情况下加载它,让 latex 自己确定必要的驱动程序。

  • 您不得为算法加载冲突的包。决定要使用哪一个并加载它。Latex 非常清楚地为您提供有关已定义命令的错误消息,不要忽略错误!!!!!!


...然后附加的有一点不便end:只需使用\eIf宏而不是\If如果你有一个 in-else 语句

最后是一个建议:如果你不手动弄乱\quads 而是使用适当的关键字,事情会自动对齐

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

在此处输入图像描述


推荐阅读