Flotar después del párrafo en la misma página

Flotar después del párrafo en la misma página

Siempre pongo mis entornos flotantes al final del párrafo donde se hace referencia a ellos por primera vez.

Pero tengo un problema cuando el párrafo se divide en dos páginas (comenzando en la parte inferior de la primera página y terminando en la parte superior de la segunda). El flotador podría colocarse fácilmente en la parte superior de la primera página, porque se hace referencia a él directamente en la primera oración del párrafo que está en la primera página, incluso si el flotador también está en esta página. Pero Latex coloca el flotador en la segunda página, lo que da como resultado una primera página medio vacía (debido a que ahora falta el flotador) y una segunda página medio vacía (porque el flotador ahora es lo único en esta página).

¿Existe una manera más fácil que mover manualmente el entorno flotante en la fuente antes del párrafo (con todos los problemas cuando la condición mencionada anteriormente ya no se cumple, por ejemplo, cuando se inserta texto adicional)?

MWE (con el flotador en la segunda página)

\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 (con la carroza en la primera página)

\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}

Respuesta1

Para reutilizar el nombre de la macro, utilice \renewcommand la segunda vez.

\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}

información relacionada