
Sempre coloco meus ambientes flutuantes no final do parágrafo onde foram referenciados pela primeira vez.
Mas tenho um problema quando o parágrafo é dividido em duas páginas (começando na parte inferior da primeira página e terminando no topo da segunda página). O float pode ser colocado facilmente no topo da primeira página, pois é referenciado diretamente na primeira frase do parágrafo que está na primeira página, mesmo que o float também esteja nesta página. Mas o Latex coloca o float na segunda página, resultando em uma primeira página meio vazia (por causa do float agora ausente) e uma segunda página meio vazia (porque o float agora é a única coisa nesta página).
Existe uma maneira mais fácil do que mover manualmente o ambiente float na fonte antes do parágrafo (com todos os problemas quando a condição acima mencionada não é mais válida, por exemplo, quando texto adicional é inserido)?
MWE (com o float na 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 (com o float na primeira 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}
Responder1
Para reutilizar o nome da macro, use \renewcommand pela 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}