
Я всегда размещаю окружение float в конце абзаца, где оно упоминается впервые.
Но у меня проблема, когда абзац разбит на две страницы (начинается внизу первой страницы и заканчивается вверху второй страницы). Плавающий элемент можно было бы легко разместить вверху первой страницы, потому что на него ссылаются напрямую в первом предложении абзаца, который находится на первой странице, даже если плавающий элемент находится и на этой странице. Но Latex помещает плавающий элемент на вторую страницу, в результате чего получается полупустая первая страница (из-за отсутствующего плавающего элемента) и полупустая вторая страница (потому что плавающий элемент теперь единственное, что есть на этой странице).
Есть ли более простой способ, чем вручную перемещать среду float в исходном тексте перед абзацем (со всеми проблемами, когда вышеупомянутое условие больше не выполняется, например, при вставке дополнительного текста)?
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}