同じページの段落の後にフロートする

同じページの段落の後にフロートする

私は常に、最初に参照された段落の最後にフロート環境を配置します。

しかし、段落が 2 ページに分割されている場合 (最初のページの下部から始まり、2 ページ目の上部で終了) に問題が発生します。フロートは、最初のページの上部に簡単に配置できます。フロートがこのページにあっても、最初のページにある段落の最初の文で直接参照されるためです。しかし、Latex はフロートを 2 ページ目に配置するため、最初のページは半分空になり (フロートがなくなったため)、2 ページ目も半分空になります (フロートがこのページにある唯一のものになったため)。

ソース内のフロート環境を段落の前に手動で移動するよりも簡単な方法はありますか (上記の条件が満たされなくなった場合、たとえば追加のテキストが挿入された場合など、すべての問題が発生します)?

MWE (2 ページ目にフロートあり)

\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

マクロ名を再利用するには、2 回目に \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}

関連情報