參考:Latex-算法偽代碼
參考:帶豎線 algorithm 包(大量代碼可參考)
舉例一:
\usepackage[linesnumbered,ruled]{algorithm2e} \begin{algorithm}[H] \DontPrintSemicolon \SetAlgoLined \KwIn {$n \geq 0 \vee x \neq 0$} \KwOut {$y = x^n$} initialization\; $y \gets 1$\; \eIf {$n < 0$}{ $X \gets 1 / x$ \; $N \gets -n$ \; }{ $X \gets x$ \; $N \gets n$ \; } \While{$N$ is even}{ \eIf {$n < 0$}{ $X \gets X \times X$ \; $N \gets N / 2$ \; }{ $X \gets x$ \; $N \gets n$ \; } } \caption{Calculate $y = x^n$} \end{algorithm}
顯示效果:
\SetKwData{Or}{\textbf{or}}:自定義關鍵字加粗,也可以直接寫
舉例二:
\begin{algorithm} \caption{Improved alpha-shape} \label{algorithm:alpha_shape} \SetKwData{In}{\textbf{in}}\SetKwData{To}{to} \DontPrintSemicolon \SetAlgoLined \KwIn {$points, per$} \KwOut {$concaveHull$} \Begin{ \If{$len(points) < 4$}{ \Return{$convexHull(points)$} } $triangularMesh \gets DelaunayTriangulation(points)$ \; % $edgePoints \gets []$ \; % $circumRList \gets []$ \; \For{$(i_a, i_b, i_c) \in triangularMesh.vertices$}{ $circumR \gets circumradius(a,b,c)$ \; $edgePointsAppend(edgePoints, points[[i_a, i_b,i_c]])$ \; $circumRAppend(circumRList, points[[i_a, i_b, i_c]])$ \; } $circumRList \gets sorted(circumRList)$ \; $index \gets round(per \times len(circumRList)$ \; $threshold \gets circumRList[index]$ \; % $deletedEdgePoints \gets []$ \; \For{$i \gets 0$ \To $len(circumRList)$}{ \If{$circumRList[i] > threshold$}{ $deletedEdgePoints.append(edgePoints.pop(i))$ \; } } $keptPolys \gets cascaded\_union(edgePoints)$ \; $deletedPolys \gets cascaded\_union(deletedEdgePoints)$ \; $concaveHull \gets Polygon()$ \; \If{$type(deletedPolys) = MultiPolygon$}{ % $areaPolys \gets []$ \; \For{$poly \in deletedPolys$}{ $areaPolys.append(area(poly))$ \; } $index_1, area_1 \gets getIndexArea(areaPolys, 1)$ \; $index_2, area_2 \gets getIndexArea(areaPolys, 2)$ \; \If{$\frac{area_1}{area_2} > 3.0$}{ $deletedPoly \gets deletedPolys.pop(index_1)$ \; } } $concaveHull \gets keptPolys \cup deletedPolys$ \; \Return{$concaveHull$}} \end{algorithm}
顯示效果:
舉例三:
\usepackage[ruled,vlined,linesnumbered,ruled]{algorithm2e} \begin{algorithm}[!ht] \caption{Alias names Detection} \label{algorithm:alias_names} \SetKwData{Or}{\textbf{or}} \DontPrintSemicolon %\SetAlgoLined \KwIn {$polygonDict, p_1, p_2$} \KwOut {$aliasNames$} \Begin{ % $aliasNames \gets []$\; \For{$(name_1, poly_1) \in polygonDict.items()$}{ \For{$(name_2, poly_2) \in polygonDict.items()$}{ \If{$name_1 \neq name_2$}{ $poly_1 \gets Polygon(poly_1)$ \; $poly_2 \gets Polygon(poly_2)$ \; \If{$poly_1 \cap poly_2 \neq \phi$}{ $polyArea_1 \gets area(poly_1)$ \; $polyArea_2 \gets area(poly_2)$ \; $intersectArea \gets area(poly_1 \cap poly_2)$ \; $unionArea \gets area(poly_1 \cup poly_2)$ \; $r_1 \gets \frac{intersectArea}{unionArea}$ \; $r_2 \gets \frac{intersectArea}{polyArea_1}$ \; $r_3 \gets \frac{intersectArea}{polyArea_2}$ \; \If{$r_1 > p_1$ \Or $r_2 > p_2$ \Or $r_3 > p_2$}{ $aliasNames.append([name_1, name_2])$ } } } } } \Return{$aliasNames$}} \end{algorithm}
顯示效果:
增加行間距:\vspace{0.3cm}
\begin{algorithm}[!ht] \caption{Alias names Detection} \label{algorithm:alias_names} \SetKwData{Or}{\textbf{or}} \DontPrintSemicolon %\SetAlgoLined \vspace{0.3cm} \KwIn {$polygonDict, p_1, p_2$} \KwOut {$aliasNames$} \Begin{...} \end{algorithm}
效果顯示:
