在同一页面的段落后浮动

在同一页面的段落后浮动

我总是将浮动环境放在首次引用它的段落末尾。

但是,当段落被分成两页时(从第一页的底部开始,在第二页的顶部结束),我遇到了一个问题。浮动元素可以很容易地放在第一页的顶部,因为它直接在第一页的段落的第一句话中引用,即使浮动元素也在这一页上。但是 Latex 将浮动元素放在第二页上,导致第一页有一半是空的(因为现在缺少浮动元素),第二页也有一半是空的(因为浮动元素现在是这一页上唯一的东西)。

有没有比手动移动段落前源中的浮动环境更简单的方法(当上述条件不再成立时会出现所有问题,例如插入附加文本时)?

MWE(浮动在第二页)

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null
\vfill

\lipsum[1-3]


In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
  \SetKwInOut{Output}{Output}

\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine

First Line\;
First Line\;
First Line\;
First Line\;
First Line\;


\Return{result}\;
\end{algorithm}

\end{document}

MWE(在第一页上有浮动内容)

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null
\vfill

\lipsum[1-3]



\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
  \SetKwInOut{Output}{Output}

\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine

First Line\;
First Line\;
First Line\;
First Line\;
First Line\;


\Return{result}\;
\end{algorithm}
In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.

\end{document}

答案1

要重新使用宏名,请第二次使用 \renewcommand。

\documentclass{article}

\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}

\begin{document}
\null\vfill
\lipsum[1-3]

\newcommand{\InsertAlgorithmHere}{%
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
 \SetKwInOut{Output}{Output}
\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine
First Line\;
First Line\;
First Line\;
First Line\;
First Line\;
\Return{result}\;
\end{algorithm}}

In the first sentence I reference the float~\ref{alg:Algo}.\InsertAlgorithmHere
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph. 
After the first sentence there are many more sentences and the float ist put after the whole paragraph.

\end{document}

相关内容