有没有什么办法可以减少两种算法之间的差距?
以下是屏幕截图:
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{algorithm}
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\tikzmark{start}\lIf{$a>b$}{\KwRet{$a$\tikzmark{stop}}}\;
\KwRet{$b$}
\begin{tikzpicture}[remember picture, overlay]
\draw[red,thick] ([yshift=0.5ex]pic cs:start) -- ([yshift=0.5ex]pic cs:stop);
\end{tikzpicture}%
\end{algorithm}
\begin{algorithm}
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\tikzmark{start}\lIf{$a>b$}{\KwRet{$a$\tikzmark{stop}}}\;
\KwRet{$b$}
\begin{tikzpicture}[remember picture, overlay]
\draw[red,thick] ([yshift=0.5ex]pic cs:start) -- ([yshift=0.5ex]pic cs:stop);
\end{tikzpicture}%
\end{algorithm}
\end{document}
答案1
通常,当你放置一个时,algorithm
它会被视为一个浮动元素,并且浮动元素之间的间距取决于在如何改变图形/表格和文本之间的间距?但是,在某些情况下,让多个浮点数一起移动(总是)会很方便。支持方式如下:请将所有 salgorithm
作为非浮动块(使用[H]
浮点说明符)放在另一个浮点数(如figure
)内。
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\begin{document}
\begin{figure}
\begin{algorithm}[H]
\caption{An algorithm}
A step in the algorithm
\end{algorithm}
% Additional spacing can be inserted here (like \bigskip, for example)
\begin{algorithm}[H]
\caption{A different algorithm}
Another step in this different algorithm
\end{algorithm}
\end{figure}
\end{document}
根据您的个人喜好,可以插入额外的间距参数。