有人知道如何将函数体添加到 Latex 算法伪代码中吗?非常感谢!
这是我的代码:
\usepackage[vlined, ruled, boxed]{algorithm2e}
\begin{algorithm}
\KwData{model directory}
\KwResult{multi-resolution model}
enter directory\;
geometrylist $\leftarrow$ search geometry files\;
$np$ $\leftarrow$ number of total processes\;
$ng$ $\leftarrow$ number of total geometry files\;
\eIf{$ng < np$ and $processid < ng$}{
GenSeparateLODFile(geometrylist[processid])\;
}{
sort geometrylist by file size\;
\While{i*processid $<$ ng}{
GenSeparateLODFile(geometrylist[processid*i])\;
$i$++\;
}
}
wait for all processes to end\;
\If{isLastProcess}{
assembly each LOD geometry\;
}
\caption{PLG}
\end{algorithm}
结果:
我想要的是:
答案1
你只需要创建块,只需使用类似下面的命令即可
\SetKwProg{Fn}{Function}{}{}
准备好可用的参数。然后,在你的算法中,你会说,例如,
\Fn{BuildOLD (directory)}{
<contents>
}
这是一个完整的示例,其中我还将使用的字体更改为无衬线字体并更改了规则的颜色,以便与所附图像相匹配以获得所需的结果;当然,这些更改是可选的,可以安全地删除。
\documentclass{article}
\usepackage[vlined,ruled]{algorithm2e}
\usepackage{xcolor}
%%% optional fonts and color configuration
\SetAlFnt{\sffamily}
\renewcommand\ArgSty{\normalfont\sffamily}
\renewcommand\KwSty[1]{\textnormal{\textbf{\sffamily#1}}\unskip}
\SetAlCapFnt{\normalfont\sffamily\large}
\renewcommand\AlCapNameFnt{\sffamily\large}
%%% vertical rules in cyan color
\makeatletter
\renewcommand{\algocf@Vline}[1]{% no vskip in between boxes but a strut to separate them,
\strut\par\nointerlineskip% then interblock space stay the same whatever is inside it
\algocf@push{\skiprule}% move to the right before the vertical rule
\hbox{\bgroup\color{cyan}\vrule\egroup%
\vtop{\algocf@push{\skiptext}%move the right after the rule
\vtop{\algocf@addskiptotal #1}\bgroup\color{cyan}\Hlne\egroup}}\vskip\skiphlne% inside the block
\algocf@pop{\skiprule}%\algocf@subskiptotal% restore indentation
\nointerlineskip}% no vskip after
%
\renewcommand{\algocf@Vsline}[1]{% no vskip in between boxes but a strut to separate them,
\strut\par\nointerlineskip% then interblock space stay the same whatever is inside it
\algocf@bblockcode%
\algocf@push{\skiprule}% move to the right before the vertical rule
\hbox{\bgroup\color{cyan}\vrule\egroup% the vertical rule
\vtop{\algocf@push{\skiptext}%move the right after the rule
\vtop{\algocf@addskiptotal #1}}}% inside the block
\algocf@pop{\skiprule}% restore indentation
\algocf@eblockcode%
}
%
\makeatother
%%% end of optional fonts and color configuration
\SetKwProg{Fn}{Function}{}{}
\begin{document}
\begin{algorithm}
\Fn{BuildOLD (directory)}{
enter directory\;
geometrylist $\leftarrow$ search geometry files\;
$np$ $\leftarrow$ number of total processes\;
$ng$ $\leftarrow$ number of total geometry files\;
\eIf{$ng < np$ and $processid < ng$}{
GenSeparateLODFile(geometrylist[processid])\;
}{
sort geometrylist by file size\;
\While{i*processid $<$ ng}{
GenSeparateLODFile(geometrylist[processid*i])\;
$i$++\;
}
}
wait for all processes to end\;
\If{isLastProcess}{
assembly each LOD geometry\;
}}
\caption{PLG}
\end{algorithm}
\end{document}
结果: