Latex algorithm


  方式一  

需要包含的

\usepackage[noend]{algpseudocode}

\usepackage{algorithmicx,algorithm}

源碼

\begin{algorithm}[t]
\caption{algorithm caption} %算法的名字
\hspace*{0.02in} {\bf Input:} %算法的輸入, \hspace*{0.02in}用來控制位置,同時利用 \\ 進行換行
input parameters A, B, C\\
\hspace*{0.02in} {\bf Output:} %算法的結果輸出
output result
\begin{algorithmic}[1]
\State some description % \State 后寫一般語句
\For{condition} % For 語句,需要和EndFor對應
  \State ...
  \If{condition} % If 語句,需要和EndIf對應
    \State ...
  \Else
    \State ...
  \EndIf
\EndFor
\While{condition} % While語句,需要和EndWhile對應
  \State ...
\EndWhile
\State \Return result
\end{algorithmic}
\end{algorithm}

  結果截圖

 注意

1. 關鍵字的大小寫問題,否則會出現 Undefined control sequence.

2. 控制流要前后對應。如果有 While,但沒有 EndWhile,否則會出現 Some blocks are not closed。

  方式二  

需要包含的

\usepackage[ruled]{algorithm2e}

源碼

\begin{algorithm}[H]
\caption{algorithm caption}%算法名字
\LinesNumbered %要求顯示行號
\KwIn{input parameters A, B, C}%輸入參數
\KwOut{output result}%輸出
some description\; %\;用於換行
\For{condition}{
  only if\;
  \If{condition}{
    1\;
  }
}
\While{not at end of this document}{
  if and else\;
  \eIf{condition}{
    1\;
  }{
    2\;
  }
}
\ForEach{condition}{
  \If{condition}{
    1\;
  }
}
\end{algorithm}

結果截圖

  方式三  

需要包含的

\usepackage[ruled,vlined]{algorithm2e}

源碼

同方式二

結果截圖

 如果有幫助,順手點個贊哦!我會好好努力的!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM