算法与文本之间的空格

算法与文本之间的空格

我用algorithm2e它来写算法。我不知道如何在我的文本和algorithm标题后的上下环境之间留出一些空间(一行就足够了)。这是我的代码:

\documentclass[a4paper]{article}
\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}
\begin{document}
We can finally state the algorithm:
%space needed
\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
%space needed
\noindent Moreover some text:
\end{document}

我需要使用[H]而不是,[h]否则它会放在“此外”之后。

答案1

只需添加一个center环境:

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}
We can finally state the algorithm:
\begin{center}
\begin{algorithm}[H]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
\end{center}
Moreover some text:

\end{document}

或者“滥用”数学显示(这确保在算法之前不会发生分页)

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}
We can finally state the algorithm:
\[
\begin{minipage}{\displaywidth}
\begin{algorithm}[H]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
\end{minipage}
\]
Moreover some text:

\end{document}

在此处输入图片描述

另一方面,如果算法需要有标题,则可以将其设为浮动对象。使用交叉引用使文本与浮动的最终位置无关。

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}

We can finally state the algorithm, displayed as Algorithm~\ref{algo:toa}.

\begin{algorithm}[htp]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm\label{algo:toa}}
\end{algorithm}

Moreover some text:

\end{document}

答案2

将文本和算法之间的注释行用空行替换,并\SetAlgoSkip{bigskip}在算法之前或序言中使用。


解释

algorithm2e提供\SetAlgoSkip在之前添加空格算法之后。

有四种可能性:

  • \SetAlgoSkip{}没有额外的垂直跳跃
  • \SetAlgoSkip{smallskip}作为默认行为
  • \SetAlgoSkip{medskip}有更大的跳跃
  • \SetAlgoSkip{bigskip}有更大的跳跃

您还可以定义一个具有自定义跳过的命令并将其名称(不带\)传递给\SetAlgoSkip

例子

使用\SetAlgoSkip{bigskip}

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
  justification=justified,
  format=plain]{caption}
   
\SetAlgoSkip{bigskip} % <------ added

\begin{document}
We can finally state the algorithm:

\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
 }
\caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:
\end{document}

使用 bigskip


使用自定义间距(定义新命令):

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
  justification=justified,
  format=plain]{caption}
   
\newcommand{\SkipBeforeAndAfter}{\vspace{1cm}}  % <------ added

\begin{document}
We can finally state the algorithm:

\SetAlgoSkip{SkipBeforeAndAfter} % <------ added (without \)
\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
 }
\caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:
\end{document}

自定义间距

额外的

如果您想为算法之前和之后定义不同的空间,一种可能性是执行以下操作:

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
  justification=justified,
  format=plain]{caption}
   
\usepackage{etoolbox}% <---- needed for this solution
\providetoggle{beginFlag}
\settoggle{beginFlag}{true}
\newcommand{\SkipBeforeAndAfter}{%
    \iftoggle{beginFlag}{%
        \settoggle{beginFlag}{false}%
        \vspace{\baselineskip}% <---- initial space (before)
    }{%
        \settoggle{beginFlag}{true}%
        \vspace{3\baselineskip}% <---- final space (after)
    }%
}
    
\SetAlgoSkip{SkipBeforeAndAfter}

\begin{document}
We can finally state the algorithm:

\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
 }
\caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:
\end{document}

额外的

答案3

根据您究竟想要什么类型的空间,您有一些选择。

  1. 在文本和 之间留一个空行algorithm。这将插入\parskip

    We can finally state the algorithm:
    
    \begin{algorithm}[H]
      \While{$(k < n)$}{
        do something
      }
      \caption{Topology optimization algorithm}
    \end{algorithm}
    
    \noindent Moreover some text:
    
  2. \vspace{<len>}在文本和之间手动插入一个algorithm确保您处于垂直模式。也就是说,在两者之间也留一个空行,或者插入一个明确的\par

    \noindent
    We can finally state the algorithm:
    
    \vspace{\baselineskip}% Insert a blank line
    
    \begin{algorithm}[H]
      \While{$(k < n)$}{
        do something
      }
      \caption{Topology optimization algorithm}
    \end{algorithm}
    
    \vspace{\baselineskip}% Insert a blank line
    
    \noindent Moreover some text:
    
  3. 自动化(2)使用environ

    \usepackage{environ}
    
    \let\oldalgorithm\algorithm
    \let\endoldalgorithm\endalgorithm
    
    \RenewEnviron{algorithm}[1][htbp]{%
      \par\vspace{\baselineskip}
      \begin{oldalgorithm}[#1]
        \BODY
      \end{oldalgorithm}
      \par\vspace{\baselineskip}
      \ignorespacesafterend
    }
    

使用[H]空白行(或垂直跳过)可能会导致文本与algorithm页面/列分隔符分离。

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\usepackage{environ}

\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm

\RenewEnviron{algorithm}[1][htbp]{%
  \par\vspace{\baselineskip}
  \begin{oldalgorithm}[#1]
    \BODY
  \end{oldalgorithm}
  \par\vspace{\baselineskip}
  \ignorespacesafterend
}

\begin{document}

\noindent
We can finally state the algorithm:

\vspace{\baselineskip}% Insert a blank line

\begin{algorithm}[H]
  \While{$(k < n)$}{
    do something
  }
  \caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:

\end{document}

相关内容