当我编译时出现以下错误:
Syntax error; Undefined control sequence \EndIf
代码:
\usepackage{algorithm2e}
\begin{algorithm}[h]
\label{FecEncodeAndSendFECUnits}
\caption{Function for Receiving and FEC-decoding FEC-encoded Units of Data}
\KwIn{}
%\KwOut{codeRate}
nrOfPacketsReceived = 0 HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL=0 \While{$true$}
currentPacket = ListenToSocketForNewPkt () $ nrOfPacketsReceived++ $ $ currentFecUnit =ExtractValueOfPktHeader(currentPacket, FEC_UNIT_GLOBAL) $ $ currentFecUnitModulo$ = $currentFecUnit$ \% NR\_OF\_PARALLELL\_FEC\_UNITS\_GLOBAL $ $ currentSymbolNr$ = ExtractValueOfPktHeader($currentPacket$, SYMBOL\_NR\_GLOBAL) $ packetForCurrentFecUnitGlobal[$currentFecUnitModulo$][$currentSymbolNr$] = ExtractDataOfPkt ($currentPacket$) \If { $ currentFecUnit$ > $HIGHEST_FEC_UNIT_NR_DECODED_GLOBAL$ OR
( currentFecUnit == 0 AND nrOfPacketsReceived == 1 ) }
ExtractPktHeaderAndPutIntoGlobalFecUnitHeaderValues ()
\EndIf
\If {currentFecUnit > HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL }
lastFecUnitModulo = (currentFecUnit -1 ) \% NR\_OF\_PARALLELL\_FEC\_UNITS\_GLOBAL DecodeFECUnit (lastFecUnitModulo, nrOfSrcSymbolsGlobal[lastFecUnitModulo], nrOfTotalSymbolsGlobal[lastFecUnitModulo], symbolSizeGlobal[lastFecUnitModulo], rateOfRedundantPkts[lastFecUnitModulo]) \\
$ HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL++$ \
\EndIf
\EndWhile
\end{algorithm}
我该如何解决这个问题?
答案1
没有\EndIf
nor \EndWhile
;您必须使用括号来指定范围:
\If{<condition>}{<instructions>}
\While{<condition>}{<instructions>}
因此,就您而言,您可以使用(我不确定是否\\
在某些时候插入行更改命令):
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[h]
\label{FecEncodeAndSendFECUnits}
\caption{Function for Receiving and FEC-decoding FEC-encoded Units of Data}
\KwIn{}
%\KwOut{codeRate}
nrOfPacketsReceived = 0 HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL=0 \\
\While{$true$}{
currentPacket = ListenToSocketForNewPkt () $ nrOfPacketsReceived++ $ $ currentFecUnit =ExtractValueOfPktHeader(currentPacket, FEC_UNIT_GLOBAL) $ $ currentFecUnitModulo$ = $currentFecUnit$ \% NR\_OF\_PARALLELL\_FEC\_UNITS\_GLOBAL $ $ currentSymbolNr$ = ExtractValueOfPktHeader($currentPacket$, SYMBOL\_NR\_GLOBAL) $ packetForCurrentFecUnitGlobal[$currentFecUnitModulo$][$currentSymbolNr$] = ExtractDataOfPkt ($currentPacket$) \\
\If{ $ currentFecUnit$ > $HIGHEST_FEC_UNIT_NR_DECODED_GLOBAL$ OR
( currentFecUnit == 0 AND nrOfPacketsReceived == 1 ) }{
ExtractPktHeaderAndPutIntoGlobalFecUnitHeaderValues ()
}
\If {currentFecUnit > HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL }{
lastFecUnitModulo = (currentFecUnit -1 ) \% NR\_OF\_PARALLELL\_FEC\_UNITS\_GLOBAL DecodeFECUnit (lastFecUnitModulo, nrOfSrcSymbolsGlobal[lastFecUnitModulo], nrOfTotalSymbolsGlobal[lastFecUnitModulo], symbolSizeGlobal[lastFecUnitModulo], rateOfRedundantPkts[lastFecUnitModulo]) \\
$ HIGHEST\_FEC\_UNIT\_NR\_DECODED\_GLOBAL++$ \
}
}
\end{algorithm}
\end{document}
附注:algorithmicx
这个包可能会引起您的兴趣;它具有强大的功能并且可高度定制。
答案2
您正在将包algorithm2e
( \KwIn
) 与包混合使用algorithmicx
。后者使用
\If{<body>}
<then block>
\EndIf
% also with else branches
\While{<condition>}
<body>
\EndWhile
而包algorithm2e
提供了
\If{<condition>}{<then block>}
\While{<condition>}{<body>}
以及许多其他形式。
决定要使用哪个包并阅读其文档。