我对 Tex 还很陌生,我正尝试在 Overleaf 上使用 LaTeX 编写一种算法,但只使用了页面的左半部分,而其余部分则发送到下一行。
你能告诉我我做错了什么吗?
\documentclass{IEEEtran}
\usepackage{algorithmic}
\begin{document}
%\begin{algorithm}
%\caption{Algorithm for ...}
\textbf{Algorithm:GraphPaths(capital,Cryptocurrencies[])}
\begin{algorithmic}[1]
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\REQUIRE capital, Cryptocurrencies [];
\ENSURE Augmenting paths and total arbitrage
\\ \textit{Initialisation} :
\STATE Path = [];
\FOR {$i$ in $Currencies$ }
\WHILE{$Currencies_i.market[0]$\textless$Currencies_i.market[n]$:}
\STATE volume = min($market_i_0$, $market_i_n$, $capital$);
\STATE $market_i_0$, $market_i_n$-=volume;
\IF {capital\textgreater volume}
\STATE capital = capital-volume+volume\cdot($market_i_n.price$- $market_i_0.price$);
\ELSE capital+=volume\cdot($market_i_n$.price - $market_i_0$.price);
\STATE Path.add{$Currencies_i.market[0]$,$Currencies_i.market[n]$}
\IF {$market.volume$ $\leq$ 0 in $Currencies$:}
\STATE remove: $.market[x]$;
\ENDFOR
\RETURN {Path, capital}
\end{algorithmic}
%\end{algorithm}
\end{document}
答案1
问题源于算法中没有关闭结构。也就是说,\IF
s 应该有一个关闭的\ENDIF
,\WHILE
s 应该有一个关闭的\ENDWHILE
,\FOR
s 应该有一个关闭的\ENDFOR
。此外,您不能有形式为 的双下标a_b_c
;相反,您可能在 之后a_{b_c}
。最后,\cdot
是一个数学符号,因此只能在$
...内使用$
。
\documentclass{IEEEtran}
\usepackage{algorithmic}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\newcommand{\NONUMSTATE}{\item[]}
\newcommand{\VAR}{\textnormal}
\newcommand{\minusbecomes}{\mathrel{{-}{=}}}
\newcommand{\plusbecomes}{\mathrel{{+}{=}}}
\begin{document}
\noindent
\textbf{Algorithm: GraphPaths(capital,Cryptocurrencies [])}
\begin{algorithmic}[1]
\REQUIRE capital, Cryptocurrencies [];
\ENSURE Augmenting paths and total arbitrage
\NONUMSTATE \textit{Initialisation}:
\STATE Path = [];
\FOR {$i$ in $\VAR{Currencies}$}
\WHILE{$i.\VAR{market}[0] < .\VAR{market}[n]$}
\STATE $\VAR{volume} = \min(\VAR{market}_{i_0}, \VAR{market}_{i_n}, \VAR{capital})$;
\STATE $\VAR{market}_{i_0}, \VAR{market}_{i_n} \minusbecomes \VAR{volume}$;
\IF {$\VAR{capital} > \VAR{volume}$}
\STATE $\VAR{capital} \plusbecomes
\VAR{capital} - \VAR{volume} + \VAR{volume} \cdot (\VAR{market}_{i_n}.\VAR{price} - \VAR{market}_{i_0}.\VAR{price})$;
\ELSE
\STATE $\VAR{capital} \plusbecomes \VAR{volume} \cdot (\VAR{market}_{i_n}.\VAR{price} - \VAR{market}_{i_0}.\VAR{price})$;
\ENDIF
\STATE Path.add $i.\VAR{market}[0], i.\VAR{market}[n]$
\IF {$\VAR{market}.\VAR{volume} \leq 0$ in $\VAR{Currencies}$}
\STATE remove $.\VAR{market}[x]$;
\ENDIF
\ENDWHILE
\ENDFOR
\RETURN Path, capital
\end{algorithmic}
\end{document}
我还对您的编码风格进行了一些小改动,只是为了提供一致性。
答案2
我认为我的错误是使用键盘上的“-”符号,这似乎是在 LaTeX 中进行减法运算的错误方法。
我使用了 \textminus,它运行得很好,这确实是一个新手问题,我希望它能帮助任何面临同样问题的人。